From 7589a93539420751a0b5118932671c10b2c8ee4e Mon Sep 17 00:00:00 2001 From: q66 Date: Sat, 23 May 2015 20:33:08 +0100 Subject: [PATCH] add some traits to check range for range types including proper type inheritance --- octa/range.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--- octa/vector.h | 12 ++++----- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/octa/range.h b/octa/range.h index b757596..c871065 100644 --- a/octa/range.h +++ b/octa/range.h @@ -15,9 +15,9 @@ namespace octa { struct InputRangeTag {}; struct OutputRangeTag {}; - struct ForwardRangeTag {}; - struct BidirectionalRangeTag {}; - struct RandomAccessRangeTag {}; + struct ForwardRangeTag: InputRangeTag {}; + struct BidirectionalRangeTag: ForwardRangeTag {}; + struct RandomAccessRangeTag: BidirectionalRangeTag {}; struct FiniteRandomAccessRangeTag: RandomAccessRangeTag {}; template using RangeCategory = typename T::Category; @@ -25,6 +25,69 @@ namespace octa { template using RangeValue = typename T::ValType; template using RangeReference = typename T::RefType; + // is input range + + template, InputRangeTag + >::value> struct IsInputRange: False {}; + + template + struct IsInputRange: True {}; + + // is forward range + + template, ForwardRangeTag + >::value> struct IsForwardRange: False {}; + + template + struct IsForwardRange: True {}; + + // is bidirectional range + + template, BidirectionalRangeTag + >::value> struct IsBidirectionalRange: False {}; + + template + struct IsBidirectionalRange: True {}; + + // is random access range + + template, RandomAccessRangeTag + >::value> struct IsRandomAccessRange: False {}; + + template + struct IsRandomAccessRange: True {}; + + // is finite random access range + + template, FiniteRandomAccessRangeTag + >::value> struct IsFiniteRandomAccessRange: False {}; + + template + struct IsFiniteRandomAccessRange: True {}; + + // is infinite random access range + + template + struct IsInfiniteRandomAccessRange: IntegralConstant::value && !IsFiniteRandomAccessRange::value) + > {}; + + // is output range + + template, OutputRangeTag + >::value> struct IsOutputRange: False {}; + + template + struct IsOutputRange: True {}; + + // range iterator + template struct __OctaRangeIterator { __OctaRangeIterator(): p_range() {} diff --git a/octa/vector.h b/octa/vector.h index b2ae390..54ae1a3 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -31,9 +31,9 @@ namespace octa { } template - void ctor_from_range(R &range, EnableIf, FiniteRandomAccessRangeTag - >::value, bool> = true) { + void ctor_from_range(R &range, EnableIf + ::value, bool + > = true) { RangeSize len = range.length(); reserve(len); p_len = len; @@ -44,9 +44,9 @@ namespace octa { } template - void ctor_from_range(R &range, EnableIf, FiniteRandomAccessRangeTag - >::value, bool> = true) { + void ctor_from_range(R &range, EnableIf + ::value, bool + > = true) { size_t i = 0; for (; !range.empty(); range.pop_first()) { reserve(i + 1);