From b079d3b51d17486924d5208838f4828bb554c4cb Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 11 Jun 2015 00:08:56 +0100 Subject: [PATCH] use the pointer/reference typedefs + redefine them with allocator traits --- octa/array.h | 6 +++--- octa/memory.h | 2 +- octa/string.h | 18 +++++++++--------- octa/vector.h | 46 +++++++++++++++++++++++----------------------- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/octa/array.h b/octa/array.h index 8df439d..b634ea9 100644 --- a/octa/array.h +++ b/octa/array.h @@ -44,12 +44,12 @@ struct Array { bool in_range(Size idx) { return idx < N; } bool in_range(int idx) { return idx >= 0 && Size(idx) < N; } - bool in_range(const T *ptr) { + bool in_range(ConstPointer ptr) { return ptr >= &p_buf[0] && ptr < &p_buf[N]; } - T *data() { return p_buf; } - const T *data() const { return p_buf; } + Pointer data() { return p_buf; } + ConstPointer data() const { return p_buf; } Range each() { return Range(p_buf, p_buf + N); diff --git a/octa/memory.h b/octa/memory.h index 3842f91..3f659f7 100644 --- a/octa/memory.h +++ b/octa/memory.h @@ -728,7 +728,7 @@ using AllocatorValue = typename AllocatorType::Value; template using AllocatorPointer = typename octa::detail::PointerType< - AllocatorValue, AllocatorType + AllocatorValue, AllocatorType >::Type; template diff --git a/octa/string.h b/octa/string.h index 81b6d10..6118fff 100644 --- a/octa/string.h +++ b/octa/string.h @@ -145,8 +145,8 @@ public: using Value = T; using Reference = T &; using ConstReference = const T &; - using Pointer = T *; - using ConstPointer = const T *; + using Pointer = octa::AllocatorPointer; + using ConstPointer = octa::AllocatorConstPointer; using Range = octa::StringRangeBase; using ConstRange = octa::StringRangeBase; using Allocator = A; @@ -168,10 +168,10 @@ public: } /* TODO: traits for utf-16/utf-32 string lengths, for now assume char */ - StringBase(const T *v, const A &a = A()): + StringBase(ConstPointer v, const A &a = A()): p_buf(ConstRange(v, strlen(v) + 1), a) {} - StringBase(const T *v, Size n, const A &a = A()): + StringBase(ConstPointer v, Size n, const A &a = A()): p_buf(ConstRange(v, n), a) {} template StringBase(R range, const A &a = A()): @@ -189,7 +189,7 @@ public: p_buf.operator=(octa::move(v)); return *this; } - StringBase &operator=(const T *v) { + StringBase &operator=(ConstPointer v) { p_buf = ConstRange(v, strlen(v) + 1); return *this; } @@ -221,8 +221,8 @@ public: T &back() { return p_buf[size() - 1]; } const T &back() const { return p_buf[size() - 1]; } - T *data() { return p_buf.data(); } - const T *data() const { return p_buf.data(); } + Pointer data() { return p_buf.data(); } + ConstPointer data() const { return p_buf.data(); } octa::Size size() const { return p_buf.size() - 1; @@ -253,7 +253,7 @@ public: return *this; } - StringBase &append(const T *s) { + StringBase &append(ConstPointer s) { p_buf.pop(); p_buf.insert_range(p_buf.size(), ConstRange(s, strlen(s) + 1)); @@ -278,7 +278,7 @@ public: StringBase &operator+=(const StringBase &s) { return append(s); } - StringBase &operator+=(const T *s) { + StringBase &operator+=(ConstPointer s) { return append(s); } StringBase &operator+=(T c) { diff --git a/octa/vector.h b/octa/vector.h index 76218bb..d7bf4fa 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -123,8 +123,8 @@ public: using Value = T; using Reference = T &; using ConstReference = const T &; - using Pointer = T *; - using ConstPointer = const T *; + using Pointer = octa::AllocatorPointer; + using ConstPointer = octa::AllocatorConstPointer; using Range = octa::PointerRange; using ConstRange = octa::PointerRange; using Allocator = A; @@ -169,8 +169,8 @@ public: if (octa::IsPod()) { memcpy(p_buf.p_ptr, v.p_buf.p_ptr, p_len * sizeof(T)); } else { - T *cur = p_buf.p_ptr, *last = p_buf.p_ptr + p_len; - T *vbuf = v.p_buf.p_ptr; + Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len; + Pointer vbuf = v.p_buf.p_ptr; while (cur != last) { octa::allocator_construct(p_buf.get_alloc(), cur++, octa::move(*vbuf++)); @@ -186,7 +186,7 @@ public: v.p_len = v.p_cap = 0; } - Vector(const T *buf, Size n, const A &a = A()): Vector(a) { + Vector(ConstPointer buf, Size n, const A &a = A()): Vector(a) { reserve(n); if (octa::IsPod()) { memcpy(p_buf.p_ptr, buf, n * sizeof(T)); @@ -213,7 +213,7 @@ public: void clear() { if (p_len > 0 && !octa::IsPod()) { - T *cur = p_buf.p_ptr, *last = p_buf.p_ptr + p_len; + Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len; while (cur != last) octa::allocator_destroy(p_buf.get_alloc(), cur++); } @@ -246,8 +246,8 @@ public: if (octa::IsPod()) { memcpy(p_buf.p_ptr, il.begin(), ilen); } else { - T *tbuf = p_buf.p_ptr, *ibuf = il.begin(), - *last = il.end(); + Pointer tbuf = p_buf.p_ptr, ibuf = il.begin(), + last = il.end(); while (ibuf != last) { octa::allocator_construct(p_buf.get_alloc(), tbuf++, *ibuf++); @@ -273,8 +273,8 @@ public: p_buf.p_ptr[i] = T(v); } } else { - T *first = p_buf.p_ptr + l; - T *last = p_buf.p_ptr + p_len; + Pointer first = p_buf.p_ptr + l; + Pointer last = p_buf.p_ptr + p_len; while (first != last) octa::allocator_construct(p_buf.get_alloc(), first++, v); } @@ -288,13 +288,13 @@ public: } else { while (p_cap < n) p_cap *= 2; } - T *tmp = octa::allocator_allocate(p_buf.get_alloc(), p_cap); + Pointer tmp = octa::allocator_allocate(p_buf.get_alloc(), p_cap); if (oc > 0) { if (octa::IsPod()) { memcpy(tmp, p_buf.p_ptr, p_len * sizeof(T)); } else { - T *cur = p_buf.p_ptr, *tcur = tmp, - *last = tmp + p_len; + Pointer cur = p_buf.p_ptr, tcur = tmp, + last = tmp + p_len; while (tcur != last) { octa::allocator_construct(p_buf.get_alloc(), tcur++, octa::move(*cur)); @@ -349,8 +349,8 @@ public: T &back() { return p_buf.p_ptr[p_len - 1]; } const T &back() const { return p_buf.p_ptr[p_len - 1]; } - T *data() { return p_buf.p_ptr; } - const T *data() const { return p_buf.p_ptr; } + Pointer data() { return p_buf.p_ptr; } + ConstPointer data() const { return p_buf.p_ptr; } Size size() const { return p_len; } Size capacity() const { return p_cap; } @@ -359,30 +359,30 @@ public: bool in_range(Size idx) { return idx < p_len; } bool in_range(int idx) { return idx >= 0 && Size(idx) < p_len; } - bool in_range(const T *ptr) { + bool in_range(ConstPointer ptr) { return ptr >= p_buf.p_ptr && ptr < &p_buf.p_ptr[p_len]; } - T *disown() { - T *r = p_buf.p_ptr; + Pointer disown() { + Pointer r = p_buf.p_ptr; p_buf.p_ptr = nullptr; p_len = p_cap = 0; return r; } - T *insert(Size idx, T &&v) { + Pointer insert(Size idx, T &&v) { insert_base(idx, 1); p_buf.p_ptr[idx] = octa::move(v); return &p_buf.p_ptr[idx]; } - T *insert(Size idx, const T &v) { + Pointer insert(Size idx, const T &v) { insert_base(idx, 1); p_buf.p_ptr[idx] = v; return &p_buf.p_ptr[idx]; } - T *insert(Size idx, Size n, const T &v) { + Pointer insert(Size idx, Size n, const T &v) { insert_base(idx, n); for (Size i = 0; i < n; ++i) { p_buf.p_ptr[idx + i] = v; @@ -391,7 +391,7 @@ public: } template - T *insert_range(Size idx, U range) { + Pointer insert_range(Size idx, U range) { Size l = range.size(); insert_base(idx, l); for (Size i = 0; i < l; ++i) { @@ -401,7 +401,7 @@ public: return &p_buf.p_ptr[idx]; } - T *insert(Size idx, InitializerList il) { + Pointer insert(Size idx, InitializerList il) { return insert_range(idx, octa::each(il)); }