diff --git a/octa/array.h b/octa/array.h index f77974a..aec94b2 100644 --- a/octa/array.h +++ b/octa/array.h @@ -14,23 +14,23 @@ namespace octa { -template +template struct Array { - using Size = size_t; - using Difference = ptrdiff_t; + using Size = octa::Size; + using Difference = octa::Ptrdiff; using Value = T; using Reference = T &; using ConstReference = const T &; using Pointer = T *; using ConstPointer = const T *; - using Range = PointerRange; - using ConstRange = PointerRange; + using Range = octa::PointerRange; + using ConstRange = octa::PointerRange; - T &operator[](size_t i) { return p_buf[i]; } - const T &operator[](size_t i) const { return p_buf[i]; } + T &operator[](Size i) { return p_buf[i]; } + const T &operator[](Size i) const { return p_buf[i]; } - T &at(size_t i) { return p_buf[i]; } - const T &at(size_t i) const { return p_buf[i]; } + T &at(Size i) { return p_buf[i]; } + const T &at(Size i) const { return p_buf[i]; } T &front() { return p_buf[0]; } const T &front() const { return p_buf[0]; } @@ -38,12 +38,12 @@ struct Array { T &back() { return p_buf[(N > 0) ? (N - 1) : 0]; } const T &back() const { return p_buf[(N > 0) ? (N - 1) : 0]; } - size_t size() const { return N; } + Size size() const { return N; } bool empty() const { return N == 0; } - bool in_range(size_t idx) { return idx < N; } - bool in_range(int idx) { return idx >= 0 && size_t(idx) < N; } + 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) { return ptr >= &p_buf[0] && ptr < &p_buf[N]; } @@ -52,10 +52,10 @@ struct Array { const T *data() const { return p_buf; } Range each() { - return octa::PointerRange(p_buf, p_buf + N); + return Range(p_buf, p_buf + N); } ConstRange each() const { - return octa::PointerRange(p_buf, p_buf + N); + return ConstRange(p_buf, p_buf + N); } void swap(Array &v) { diff --git a/octa/atomic.h b/octa/atomic.h index bfb345f..467a17c 100644 --- a/octa/atomic.h +++ b/octa/atomic.h @@ -115,7 +115,7 @@ namespace detail { __atomic_signal_fence(to_gcc_order(ord)); } - static inline bool atomic_is_lock_free(size_t size) { + static inline bool atomic_is_lock_free(octa::Size size) { /* return __atomic_is_lock_free(size, 0); cannot be used on some platforms */ return size <= sizeof(void *); } @@ -201,13 +201,13 @@ namespace detail { } template - struct SkipAmt { static constexpr size_t value = 1; }; + struct SkipAmt { static constexpr octa::Size value = 1; }; template - struct SkipAmt { static constexpr size_t value = sizeof(T); }; + struct SkipAmt { static constexpr octa::Size value = sizeof(T); }; template struct SkipAmt {}; - template struct SkipAmt {}; + template struct SkipAmt {}; template static inline T atomic_fetch_add(volatile AtomicBase *a, @@ -450,494 +450,528 @@ namespace detail { }; } - template - struct Atomic: octa::detail::Atomic { - using Base = octa::detail::Atomic; +template +struct Atomic: octa::detail::Atomic { + using Base = octa::detail::Atomic; - Atomic() = default; + Atomic() = default; - constexpr Atomic(T v): Base(v) {} + constexpr Atomic(T v): Base(v) {} - T operator=(T v) volatile { - Base::store(v); return v; - } - - T operator=(T v) { - Base::store(v); return v; - } - }; - - template - struct Atomic: octa::detail::Atomic { - using Base = octa::detail::Atomic; - - Atomic() = default; - - constexpr Atomic(T *v): Base(v) {} - - T *operator=(T *v) volatile { - Base::store(v); return v; - } - - T *operator=(T *v) { - Base::store(v); return v; - } - - T *fetch_add(ptrdiff_t op, MemoryOrder ord = MemoryOrder::seq_cst) - volatile { - return octa::detail::atomic_fetch_add(&this->p_a, op, ord); - } - - T *fetch_add(ptrdiff_t op, MemoryOrder ord = MemoryOrder::seq_cst) { - return octa::detail::atomic_fetch_add(&this->p_a, op, ord); - } - - T *fetch_sub(ptrdiff_t op, MemoryOrder ord = MemoryOrder::seq_cst) - volatile { - return octa::detail::atomic_fetch_sub(&this->p_a, op, ord); - } - - T *fetch_sub(ptrdiff_t op, MemoryOrder ord = MemoryOrder::seq_cst) { - return octa::detail::atomic_fetch_sub(&this->p_a, op, ord); - } - - - T *operator++(int) volatile { return fetch_add(1); } - T *operator++(int) { return fetch_add(1); } - T *operator--(int) volatile { return fetch_sub(1); } - T *operator--(int) { return fetch_sub(1); } - T *operator++( ) volatile { return fetch_add(1) + 1; } - T *operator++( ) { return fetch_add(1) + 1; } - T *operator--( ) volatile { return fetch_sub(1) - 1; } - T *operator--( ) { return fetch_sub(1) - 1; } - - T *operator+=(ptrdiff_t op) volatile { return fetch_add(op) + op; } - T *operator+=(ptrdiff_t op) { return fetch_add(op) + op; } - T *operator-=(ptrdiff_t op) volatile { return fetch_sub(op) - op; } - T *operator-=(ptrdiff_t op) { return fetch_sub(op) - op; } - }; - - template - inline bool atomic_is_lock_free(const volatile Atomic *a) { - return a->is_lock_free(); + T operator=(T v) volatile { + Base::store(v); return v; } - template - inline bool atomic_is_lock_free(const Atomic *a) { - return a->is_lock_free(); + T operator=(T v) { + Base::store(v); return v; + } +}; + +template +struct Atomic: octa::detail::Atomic { + using Base = octa::detail::Atomic; + + Atomic() = default; + + constexpr Atomic(T *v): Base(v) {} + + T *operator=(T *v) volatile { + Base::store(v); return v; } - template - inline void atomic_init(volatile Atomic *a, T v) { - octa::detail::atomic_init(&a->p_a, v); + T *operator=(T *v) { + Base::store(v); return v; } - template - inline void atomic_init(Atomic *a, T v) { - octa::detail::atomic_init(&a->p_a, v); + T *fetch_add(octa::Ptrdiff op, MemoryOrder ord = MemoryOrder::seq_cst) + volatile { + return octa::detail::atomic_fetch_add(&this->p_a, op, ord); } - template - inline void atomic_store(volatile Atomic *a, T v) { - a->store(v); + T *fetch_add(octa::Ptrdiff op, MemoryOrder ord = MemoryOrder::seq_cst) { + return octa::detail::atomic_fetch_add(&this->p_a, op, ord); } - template - inline void atomic_store(Atomic *a, T v) { - a->store(v); + T *fetch_sub(octa::Ptrdiff op, MemoryOrder ord = MemoryOrder::seq_cst) + volatile { + return octa::detail::atomic_fetch_sub(&this->p_a, op, ord); } - template - inline void atomic_store_explicit(volatile Atomic *a, T v, - MemoryOrder ord) { - a->store(v, ord); + T *fetch_sub(octa::Ptrdiff op, MemoryOrder ord = MemoryOrder::seq_cst) { + return octa::detail::atomic_fetch_sub(&this->p_a, op, ord); } - template - inline void atomic_store_explicit(Atomic *a, T v, - MemoryOrder ord) { - a->store(v, ord); - } - template - inline T atomic_load(const volatile Atomic *a) { - return a->load(); - } + T *operator++(int) volatile { return fetch_add(1); } + T *operator++(int) { return fetch_add(1); } + T *operator--(int) volatile { return fetch_sub(1); } + T *operator--(int) { return fetch_sub(1); } + T *operator++( ) volatile { return fetch_add(1) + 1; } + T *operator++( ) { return fetch_add(1) + 1; } + T *operator--( ) volatile { return fetch_sub(1) - 1; } + T *operator--( ) { return fetch_sub(1) - 1; } - template - inline T atomic_load(const Atomic *a) { - return a->load(); - } + T *operator+=(octa::Ptrdiff op) volatile { return fetch_add(op) + op; } + T *operator+=(octa::Ptrdiff op) { return fetch_add(op) + op; } + T *operator-=(octa::Ptrdiff op) volatile { return fetch_sub(op) - op; } + T *operator-=(octa::Ptrdiff op) { return fetch_sub(op) - op; } +}; - template - inline T atomic_load_explicit(const volatile Atomic *a, +template +inline bool atomic_is_lock_free(const volatile Atomic *a) { + return a->is_lock_free(); +} + +template +inline bool atomic_is_lock_free(const Atomic *a) { + return a->is_lock_free(); +} + +template +inline void atomic_init(volatile Atomic *a, T v) { + octa::detail::atomic_init(&a->p_a, v); +} + +template +inline void atomic_init(Atomic *a, T v) { + octa::detail::atomic_init(&a->p_a, v); +} + +template +inline void atomic_store(volatile Atomic *a, T v) { + a->store(v); +} + +template +inline void atomic_store(Atomic *a, T v) { + a->store(v); +} + +template +inline void atomic_store_explicit(volatile Atomic *a, T v, + MemoryOrder ord) { + a->store(v, ord); +} + +template +inline void atomic_store_explicit(Atomic *a, T v, + MemoryOrder ord) { + a->store(v, ord); +} + +template +inline T atomic_load(const volatile Atomic *a) { + return a->load(); +} + +template +inline T atomic_load(const Atomic *a) { + return a->load(); +} + +template +inline T atomic_load_explicit(const volatile Atomic *a, + MemoryOrder ord) { + return a->load(ord); +} + +template +inline T atomic_load_explicit(const Atomic *a, MemoryOrder ord) { + return a->load(ord); +} + +template +inline T atomic_exchange(volatile Atomic *a, T v) { + return a->exchange(v); +} + +template +inline T atomic_exchange(Atomic *a, T v) { + return a->exchange(v); +} + +template +inline T atomic_exchange_explicit(volatile Atomic *a, T v, + MemoryOrder ord) { + return a->exchange(v, ord); +} + +template +inline T atomic_exchange_explicit(Atomic *a, T v, MemoryOrder ord) { - return a->load(ord); + return a->exchange(v, ord); +} + +template +inline bool atomic_compare_exchange_weak(volatile Atomic *a, + T *e, T v) { + return a->compare_exchange_weak(*e, v); +} + +template +inline bool atomic_compare_exchange_weak(Atomic *a, T *e, T v) { + return a->compare_exchange_weak(*e, v); +} + +template +inline bool atomic_compare_exchange_strong(volatile Atomic *a, + T *e, T v) { + return a->compare_exchange_strong(*e, v); +} + +template +inline bool atomic_compare_exchange_strong(Atomic *a, T *e, T v) { + return a->compare_exchange_strong(*e, v); +} + +template +inline bool atomic_compare_exchange_weak_explicit(volatile Atomic *a, + T *e, T v, + MemoryOrder s, + MemoryOrder f) { + return a->compare_exchange_weak(*e, v, s, f); +} + +template +inline bool atomic_compare_exchange_weak_explicit(Atomic *a, T *e, + T v, + MemoryOrder s, + MemoryOrder f) { + return a->compare_exchange_weak(*e, v, s, f); +} + +template +inline bool atomic_compare_exchange_strong_explicit(volatile Atomic *a, + T *e, T v, + MemoryOrder s, + MemoryOrder f) { + return a->compare_exchange_strong(*e, v, s, f); +} + +template +inline bool atomic_compare_exchange_strong_explicit(Atomic *a, T *e, + T v, + MemoryOrder s, + MemoryOrder f) { + return a->compare_exchange_strong(*e, v, s, f); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_add(volatile Atomic *a, T op) { + return a->fetch_add(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_add(Atomic *a, T op) { + return a->fetch_add(op); +} + +template +inline T *atomic_fetch_add(volatile Atomic *a, octa::Ptrdiff op) { + return a->fetch_add(op); +} + +template +inline T *atomic_fetch_add(Atomic *a, octa::Ptrdiff op) { + return a->fetch_add(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_add_explicit(volatile Atomic *a, T op, + MemoryOrder ord) { + return a->fetch_add(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_add_explicit(Atomic *a, T op, MemoryOrder ord) { + return a->fetch_add(op, ord); +} + +template +inline T *atomic_fetch_add_explicit(volatile Atomic *a, + octa::Ptrdiff op, MemoryOrder ord) { + return a->fetch_add(op, ord); +} + +template +inline T *atomic_fetch_add_explicit(Atomic *a, octa::Ptrdiff op, + MemoryOrder ord) { + return a->fetch_add(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_sub(volatile Atomic *a, T op) { + return a->fetch_sub(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_sub(Atomic *a, T op) { + return a->fetch_sub(op); +} + +template +inline T *atomic_fetch_sub(volatile Atomic *a, octa::Ptrdiff op) { + return a->fetch_sub(op); +} + +template +inline T *atomic_fetch_sub(Atomic *a, octa::Ptrdiff op) { + return a->fetch_sub(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_sub_explicit(volatile Atomic *a, T op, + MemoryOrder ord) { + return a->fetch_sub(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_sub_explicit(Atomic *a, T op, MemoryOrder ord) { + return a->fetch_sub(op, ord); +} + +template +inline T *atomic_fetch_sub_explicit(volatile Atomic *a, + octa::Ptrdiff op, MemoryOrder ord) { + return a->fetch_sub(op, ord); +} + +template +inline T *atomic_fetch_sub_explicit(Atomic *a, octa::Ptrdiff op, + MemoryOrder ord) { + return a->fetch_sub(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_and(volatile Atomic *a, T op) { + return a->fetch_and(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_and(Atomic *a, T op) { + return a->fetch_and(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_and_explicit(volatile Atomic *a, T op, + MemoryOrder ord) { + return a->fetch_and(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_and_explicit(Atomic *a, T op, MemoryOrder ord) { + return a->fetch_and(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_or(volatile Atomic *a, T op) { + return a->fetch_or(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_or(Atomic *a, T op) { + return a->fetch_or(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_or_explicit(volatile Atomic *a, T op, + MemoryOrder ord) { + return a->fetch_or(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_or_explicit(Atomic *a, T op, MemoryOrder ord) { + return a->fetch_or(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_xor(volatile Atomic *a, T op) { + return a->fetch_xor(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_xor(Atomic *a, T op) { + return a->fetch_xor(op); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_xor_explicit(volatile Atomic *a, T op, + MemoryOrder ord) { + return a->fetch_xor(op, ord); +} + +template +inline octa::EnableIf::value && + !octa::IsSame::value, T> +atomic_fetch_xor_explicit(Atomic *a, T op, MemoryOrder ord) { + return a->fetch_xor(op, ord); +} + +struct AtomicFlag { + octa::detail::AtomicBase p_a; + + AtomicFlag() = default; + + AtomicFlag(bool b): p_a(b) {} + + AtomicFlag(const AtomicFlag &) = delete; + + AtomicFlag &operator=(const AtomicFlag &) = delete; + AtomicFlag &operator=(const AtomicFlag &) volatile = delete; + + bool test_and_set(MemoryOrder ord = MemoryOrder::seq_cst) volatile { + return octa::detail::atomic_exchange(&p_a, true, ord); } - template - inline T atomic_load_explicit(const Atomic *a, MemoryOrder ord) { - return a->load(ord); + bool test_and_set(MemoryOrder ord = MemoryOrder::seq_cst) { + return octa::detail::atomic_exchange(&p_a, true, ord); } - template - inline T atomic_exchange(volatile Atomic *a, T v) { - return a->exchange(v); + void clear(MemoryOrder ord = MemoryOrder::seq_cst) volatile { + octa::detail::atomic_store(&p_a, false, ord); } - template - inline T atomic_exchange(Atomic *a, T v) { - return a->exchange(v); + void clear(MemoryOrder ord = MemoryOrder::seq_cst) { + octa::detail::atomic_store(&p_a, false, ord); } +}; - template - inline T atomic_exchange_explicit(volatile Atomic *a, T v, - MemoryOrder ord) { - return a->exchange(v, ord); - } +inline bool atomic_flag_test_and_set(volatile AtomicFlag *a) { + return a->test_and_set(); +} - template - inline T atomic_exchange_explicit(Atomic *a, T v, +inline bool atomic_flag_test_and_set(AtomicFlag *a) { + return a->test_and_set(); +} + +inline bool atomic_flag_test_and_set_explicit(volatile AtomicFlag *a, + MemoryOrder ord) { + return a->test_and_set(ord); +} + +inline bool atomic_flag_test_and_set_explicit(AtomicFlag *a, + MemoryOrder ord) { + return a->test_and_set(ord); +} + +inline void atomic_flag_clear(volatile AtomicFlag *a) { + a->clear(); +} + +inline void atomic_flag_clear(AtomicFlag *a) { + a->clear(); +} + +inline void atomic_flag_clear_explicit(volatile AtomicFlag *a, MemoryOrder ord) { - return a->exchange(v, ord); - } + a->clear(ord); +} - template - inline bool atomic_compare_exchange_weak(volatile Atomic *a, - T *e, T v) { - return a->compare_exchange_weak(*e, v); - } +inline void atomic_flag_clear_explicit(AtomicFlag *a, MemoryOrder ord) { + a->clear(ord); +} - template - inline bool atomic_compare_exchange_weak(Atomic *a, T *e, T v) { - return a->compare_exchange_weak(*e, v); - } +inline void atomic_thread_fence(MemoryOrder ord) { + octa::detail::atomic_thread_fence(ord); +} - template - inline bool atomic_compare_exchange_strong(volatile Atomic *a, - T *e, T v) { - return a->compare_exchange_strong(*e, v); - } +inline void atomic_signal_fence(MemoryOrder ord) { + octa::detail::atomic_signal_fence(ord); +} - template - inline bool atomic_compare_exchange_strong(Atomic *a, T *e, T v) { - return a->compare_exchange_strong(*e, v); - } +using AtomicBool = Atomic; +using AtomicChar = Atomic; +using AtomicShort = Atomic; +using AtomicInt = Atomic; +using AtomicLong = Atomic; +using AtomicSchar = Atomic; +using AtomicUchar = Atomic; +using AtomicUshort = Atomic; +using AtomicUint = Atomic; +using AtomicUlong = Atomic; +using AtomicLlong = Atomic; +using AtomicUllong = Atomic; - template - inline bool atomic_compare_exchange_weak_explicit(volatile Atomic *a, - T *e, T v, - MemoryOrder s, - MemoryOrder f) { - return a->compare_exchange_weak(*e, v, s, f); - } +using AtomicChar16 = Atomic; +using AtomicChar32 = Atomic; +using AtomicWchar = Atomic; - template - inline bool atomic_compare_exchange_weak_explicit(Atomic *a, T *e, - T v, - MemoryOrder s, - MemoryOrder f) { - return a->compare_exchange_weak(*e, v, s, f); - } +using AtomicPtrdiff = Atomic; +using AtomicSize = Atomic; - template - inline bool atomic_compare_exchange_strong_explicit(volatile Atomic *a, - T *e, T v, - MemoryOrder s, - MemoryOrder f) { - return a->compare_exchange_strong(*e, v, s, f); - } +using AtomicIntmax = Atomic; +using AtomicUintmax = Atomic; - template - inline bool atomic_compare_exchange_strong_explicit(Atomic *a, T *e, - T v, - MemoryOrder s, - MemoryOrder f) { - return a->compare_exchange_strong(*e, v, s, f); - } +using AtomicIntptr = Atomic; +using AtomicUintptr = Atomic; - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_add(volatile Atomic *a, T op) { - return a->fetch_add(op); - } +using AtomicInt8 = Atomic; +using AtomicInt16 = Atomic; +using AtomicInt32 = Atomic; +using AtomicInt64 = Atomic; - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_add(Atomic *a, T op) { - return a->fetch_add(op); - } +using AtomicUint8 = Atomic; +using AtomicUint16 = Atomic; +using AtomicUint32 = Atomic; +using AtomicUint64 = Atomic; - template - inline T *atomic_fetch_add(volatile Atomic *a, ptrdiff_t op) { - return a->fetch_add(op); - } +using AtomicIntLeast8 = Atomic; +using AtomicIntLeast16 = Atomic; +using AtomicIntLeast32 = Atomic; +using AtomicIntLeast64 = Atomic; - template - inline T *atomic_fetch_add(Atomic *a, ptrdiff_t op) { - return a->fetch_add(op); - } +using AtomicUintLeast8 = Atomic; +using AtomicUintLeast16 = Atomic; +using AtomicUintLeast32 = Atomic; +using AtomicUintLeast64 = Atomic; - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_add_explicit(volatile Atomic *a, T op, - MemoryOrder ord) { - return a->fetch_add(op, ord); - } +using AtomicIntFast8 = Atomic; +using AtomicIntFast16 = Atomic; +using AtomicIntFast32 = Atomic; +using AtomicIntFast64 = Atomic; - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_add_explicit(Atomic *a, T op, MemoryOrder ord) { - return a->fetch_add(op, ord); - } - - template - inline T *atomic_fetch_add_explicit(volatile Atomic *a, - ptrdiff_t op, MemoryOrder ord) { - return a->fetch_add(op, ord); - } - - template - inline T *atomic_fetch_add_explicit(Atomic *a, ptrdiff_t op, - MemoryOrder ord) { - return a->fetch_add(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_sub(volatile Atomic *a, T op) { - return a->fetch_sub(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_sub(Atomic *a, T op) { - return a->fetch_sub(op); - } - - template - inline T *atomic_fetch_sub(volatile Atomic *a, ptrdiff_t op) { - return a->fetch_sub(op); - } - - template - inline T *atomic_fetch_sub(Atomic *a, ptrdiff_t op) { - return a->fetch_sub(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_sub_explicit(volatile Atomic *a, T op, - MemoryOrder ord) { - return a->fetch_sub(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_sub_explicit(Atomic *a, T op, MemoryOrder ord) { - return a->fetch_sub(op, ord); - } - - template - inline T *atomic_fetch_sub_explicit(volatile Atomic *a, - ptrdiff_t op, MemoryOrder ord) { - return a->fetch_sub(op, ord); - } - - template - inline T *atomic_fetch_sub_explicit(Atomic *a, ptrdiff_t op, - MemoryOrder ord) { - return a->fetch_sub(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_and(volatile Atomic *a, T op) { - return a->fetch_and(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_and(Atomic *a, T op) { - return a->fetch_and(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_and_explicit(volatile Atomic *a, T op, - MemoryOrder ord) { - return a->fetch_and(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_and_explicit(Atomic *a, T op, MemoryOrder ord) { - return a->fetch_and(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_or(volatile Atomic *a, T op) { - return a->fetch_or(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_or(Atomic *a, T op) { - return a->fetch_or(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_or_explicit(volatile Atomic *a, T op, - MemoryOrder ord) { - return a->fetch_or(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_or_explicit(Atomic *a, T op, MemoryOrder ord) { - return a->fetch_or(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_xor(volatile Atomic *a, T op) { - return a->fetch_xor(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_xor(Atomic *a, T op) { - return a->fetch_xor(op); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_xor_explicit(volatile Atomic *a, T op, - MemoryOrder ord) { - return a->fetch_xor(op, ord); - } - - template - inline octa::EnableIf::value && - !octa::IsSame::value, T> - atomic_fetch_xor_explicit(Atomic *a, T op, MemoryOrder ord) { - return a->fetch_xor(op, ord); - } - - struct AtomicFlag { - octa::detail::AtomicBase p_a; - - AtomicFlag() = default; - - AtomicFlag(bool b): p_a(b) {} - - AtomicFlag(const AtomicFlag &) = delete; - - AtomicFlag &operator=(const AtomicFlag &) = delete; - AtomicFlag &operator=(const AtomicFlag &) volatile = delete; - - bool test_and_set(MemoryOrder ord = MemoryOrder::seq_cst) volatile { - return octa::detail::atomic_exchange(&p_a, true, ord); - } - - bool test_and_set(MemoryOrder ord = MemoryOrder::seq_cst) { - return octa::detail::atomic_exchange(&p_a, true, ord); - } - - void clear(MemoryOrder ord = MemoryOrder::seq_cst) volatile { - octa::detail::atomic_store(&p_a, false, ord); - } - - void clear(MemoryOrder ord = MemoryOrder::seq_cst) { - octa::detail::atomic_store(&p_a, false, ord); - } - }; - - inline bool atomic_flag_test_and_set(volatile AtomicFlag *a) { - return a->test_and_set(); - } - - inline bool atomic_flag_test_and_set(AtomicFlag *a) { - return a->test_and_set(); - } - - inline bool atomic_flag_test_and_set_explicit(volatile AtomicFlag *a, - MemoryOrder ord) { - return a->test_and_set(ord); - } - - inline bool atomic_flag_test_and_set_explicit(AtomicFlag *a, - MemoryOrder ord) { - return a->test_and_set(ord); - } - - inline void atomic_flag_clear(volatile AtomicFlag *a) { - a->clear(); - } - - inline void atomic_flag_clear(AtomicFlag *a) { - a->clear(); - } - - inline void atomic_flag_clear_explicit(volatile AtomicFlag *a, - MemoryOrder ord) { - a->clear(ord); - } - - inline void atomic_flag_clear_explicit(AtomicFlag *a, MemoryOrder ord) { - a->clear(ord); - } - - inline void atomic_thread_fence(MemoryOrder ord) { - octa::detail::atomic_thread_fence(ord); - } - - inline void atomic_signal_fence(MemoryOrder ord) { - octa::detail::atomic_signal_fence(ord); - } - - using AtomicBool = Atomic; - using AtomicChar = Atomic; - using AtomicSchar = Atomic; - using AtomicUchar = Atomic; - using AtomicShort = Atomic; - using AtomicUshort = Atomic; - using AtomicInt = Atomic; - using AtomicUint = Atomic; - using AtomicLong = Atomic; - using AtomicUlong = Atomic; - using AtomicLlong = Atomic; - using AtomicUllong = Atomic; - - using AtomicChar16 = Atomic; - using AtomicChar32 = Atomic; - using AtomicWchar = Atomic; - - using AtomicIntptr = Atomic; - using AtomicUintptr = Atomic; - using AtomicSize = Atomic; - using AtomicPtrdiff = Atomic; +using AtomicUintFast8 = Atomic; +using AtomicUintFast16 = Atomic; +using AtomicUintFast32 = Atomic; +using AtomicUintFast64 = Atomic; #define ATOMIC_FLAG_INIT {false} #define ATOMIC_VAR_INIT(v) {v} diff --git a/octa/functional.h b/octa/functional.h index 211283f..6b0794d 100644 --- a/octa/functional.h +++ b/octa/functional.h @@ -98,10 +98,10 @@ template struct Hash; namespace detail { template struct HashBase { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - return size_t(v); + octa::Size operator()(T v) const { + return octa::Size(v); } }; } @@ -110,37 +110,39 @@ namespace detail { OCTA_HASH_BASIC(bool) OCTA_HASH_BASIC(char) -OCTA_HASH_BASIC(schar) -OCTA_HASH_BASIC(uchar) -OCTA_HASH_BASIC(char16_t) -OCTA_HASH_BASIC(char32_t) -OCTA_HASH_BASIC(wchar_t) OCTA_HASH_BASIC(short) -OCTA_HASH_BASIC(ushort) OCTA_HASH_BASIC(int) -OCTA_HASH_BASIC(uint) OCTA_HASH_BASIC(long) -OCTA_HASH_BASIC(ulong) + +OCTA_HASH_BASIC(octa::schar) +OCTA_HASH_BASIC(octa::uchar) +OCTA_HASH_BASIC(octa::ushort) +OCTA_HASH_BASIC(octa::uint) +OCTA_HASH_BASIC(octa::ulong) + +OCTA_HASH_BASIC(octa::Char16) +OCTA_HASH_BASIC(octa::Char32) +OCTA_HASH_BASIC(octa::Wchar) #undef OCTA_HASH_BASIC namespace detail { - static inline size_t mem_hash(const void *p, size_t l) { - const uchar *d = (const uchar *)p; - size_t h = 5381; - for (size_t i = 0; i < l; ++i) h = ((h << 5) + h) ^ d[i]; + static inline Size mem_hash(const void *p, octa::Size l) { + const octa::uchar *d = (const octa::uchar *)p; + octa::Size h = 5381; + for (Size i = 0; i < l; ++i) h = ((h << 5) + h) ^ d[i]; return h; } - template + template struct ScalarHash; template struct ScalarHash { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - union { T v; size_t h; } u; + octa::Size operator()(T v) const { + union { T v; octa::Size h; } u; u.h = 0; u.v = v; return u.h; @@ -149,10 +151,10 @@ namespace detail { template struct ScalarHash { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - union { T v; size_t h; } u; + octa::Size operator()(T v) const { + union { T v; octa::Size h; } u; u.v = v; return u.h; } @@ -160,10 +162,10 @@ namespace detail { template struct ScalarHash { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - union { T v; struct { size_t h1, h2; }; } u; + octa::Size operator()(T v) const { + union { T v; struct { octa::Size h1, h2; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } @@ -171,10 +173,10 @@ namespace detail { template struct ScalarHash { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - union { T v; struct { size_t h1, h2, h3; }; } u; + octa::Size operator()(T v) const { + union { T v; struct { octa::Size h1, h2, h3; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } @@ -182,49 +184,49 @@ namespace detail { template struct ScalarHash { using Argument = T; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T v) const { - union { T v; struct { size_t h1, h2, h3, h4; }; } u; + octa::Size operator()(T v) const { + union { T v; struct { octa::Size h1, h2, h3, h4; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } }; } /* namespace detail */ -template<> struct Hash: octa::detail::ScalarHash {}; -template<> struct Hash: octa::detail::ScalarHash {}; +template<> struct Hash: octa::detail::ScalarHash {}; +template<> struct Hash: octa::detail::ScalarHash {}; template<> struct Hash: octa::detail::ScalarHash { - size_t operator()(float v) const { + octa::Size operator()(float v) const { if (v == 0) return 0; return octa::detail::ScalarHash::operator()(v); } }; template<> struct Hash: octa::detail::ScalarHash { - size_t operator()(double v) const { + octa::Size operator()(double v) const { if (v == 0) return 0; return octa::detail::ScalarHash::operator()(v); } }; -template<> struct Hash: octa::detail::ScalarHash { - size_t operator()(ldouble v) const { +template<> struct Hash: octa::detail::ScalarHash { + octa::Size operator()(octa::ldouble v) const { if (v == 0) return 0; #ifdef __i386__ - union { ldouble v; struct { size_t h1, h2, h3, h4; }; } u; + union { octa::ldouble v; struct { octa::Size h1, h2, h3, h4; }; } u; u.h1 = u.h2 = u.h3 = u.h4 = 0; u.v = v; return (u.h1 ^ u.h2 ^ u.h3 ^ u.h4); #else #ifdef __x86_64__ - union { ldouble v; struct { size_t h1, h2; }; } u; + union { octa::ldouble v; struct { octa::Size h1, h2; }; } u; u.h1 = u.h2 = 0; u.v = v; return (u.h1 ^ u.h2); #else - return octa::detail::ScalarHash::operator()(v); + return octa::detail::ScalarHash::operator()(v); #endif #endif } @@ -232,10 +234,10 @@ template<> struct Hash: octa::detail::ScalarHash { template struct Hash { using Argument = T *; - using Result = size_t; + using Result = octa::Size; - size_t operator()(T *v) const { - union { T *v; size_t h; } u; + octa::Size operator()(T *v) const { + union { T *v; octa::Size h; } u; u.v = v; return octa::detail::mem_hash((const void *)&u, sizeof(u)); } @@ -591,8 +593,8 @@ namespace detail { template struct Function: octa::detail::FunctionBase { - Function( ) { init_empty(); } - Function(Nullptr) { init_empty(); } + Function( ) { init_empty(); } + Function(octa::Nullptr) { init_empty(); } Function(Function &&f) { init_empty(); @@ -619,7 +621,7 @@ struct Function: octa::detail::FunctionBase { Function(octa::AllocatorArg, const A &) { init_empty(); } template - Function(octa::AllocatorArg, const A &, Nullptr) { init_empty(); } + Function(octa::AllocatorArg, const A &, octa::Nullptr) { init_empty(); } template Function(octa::AllocatorArg, const A &, Function &&f) { @@ -739,16 +741,16 @@ private: }; template -bool operator==(Nullptr, const Function &rhs) { return !rhs; } +bool operator==(octa::Nullptr, const Function &rhs) { return !rhs; } template -bool operator==(const Function &lhs, Nullptr) { return !lhs; } +bool operator==(const Function &lhs, octa::Nullptr) { return !lhs; } template -bool operator!=(Nullptr, const Function &rhs) { return rhs; } +bool operator!=(octa::Nullptr, const Function &rhs) { return rhs; } template -bool operator!=(const Function &lhs, Nullptr) { return lhs; } +bool operator!=(const Function &lhs, octa::Nullptr) { return lhs; } namespace detail { template diff --git a/octa/initializer_list.h b/octa/initializer_list.h index 4457671..75c832f 100644 --- a/octa/initializer_list.h +++ b/octa/initializer_list.h @@ -17,13 +17,13 @@ namespace std { template class initializer_list { const T *p_buf; - size_t p_len; + octa::Size p_len; - initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {} + initializer_list(const T *v, octa::Size n): p_buf(v), p_len(n) {} public: initializer_list(): p_buf(nullptr), p_len(0) {} - size_t size() const { return p_len; } + octa::Size size() const { return p_len; } const T *begin() const { return p_buf; } const T *end() const { return p_buf + p_len; } diff --git a/octa/memory.h b/octa/memory.h index da8e9a4..3842f91 100644 --- a/octa/memory.h +++ b/octa/memory.h @@ -68,7 +68,7 @@ namespace detail { template::value> struct PointerDifferenceBase { - using Type = ptrdiff_t; + using Type = octa::Ptrdiff; }; template struct PointerDifferenceBase { @@ -82,7 +82,7 @@ namespace detail { template struct PointerDifferenceType { - using Type = ptrdiff_t; + using Type = octa::Ptrdiff; }; template @@ -284,7 +284,7 @@ public: static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } - constexpr Box(Nullptr): p_stor(nullptr, D()) { + constexpr Box(octa::Nullptr): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } @@ -330,7 +330,7 @@ public: return *this; } - Box &operator=(Nullptr) { + Box &operator=(octa::Nullptr) { reset(); return *this; } @@ -403,7 +403,7 @@ public: static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } - constexpr Box(Nullptr): p_stor(nullptr, D()) { + constexpr Box(octa::Nullptr): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } @@ -421,7 +421,7 @@ public: > d, octa::EnableIf::value, Nat> = Nat()): p_stor(p, d) {} - Box(Nullptr, octa::Conditional::value, + Box(octa::Nullptr, octa::Conditional::value, D, AddLvalueReference > d): p_stor(nullptr, d) {} @@ -433,7 +433,7 @@ public: "rvalue deleter cannot be a ref"); } - Box(Nullptr, octa::RemoveReference &&d): + Box(octa::Nullptr, octa::RemoveReference &&d): p_stor(nullptr, octa::move(d)) { static_assert(!octa::IsReference::value, "rvalue deleter cannot be a ref"); @@ -468,14 +468,14 @@ public: return *this; } - Box &operator=(Nullptr) { + Box &operator=(octa::Nullptr) { reset(); return *this; } ~Box() { reset(); } - octa::AddLvalueReference operator[](size_t idx) const { + octa::AddLvalueReference operator[](octa::Size idx) const { return p_stor.p_ptr[idx]; } @@ -502,7 +502,7 @@ public: if (tmp) p_stor.get_deleter()(tmp); } - void reset(Nullptr) { + void reset(octa::Nullptr) { Pointer tmp = p_stor.p_ptr; p_stor.p_ptr = nullptr; if (tmp) p_stor.get_deleter()(tmp); @@ -529,7 +529,7 @@ namespace detail { using BoxUnknownSize = octa::Box; }; - template struct BoxIf { + template struct BoxIf { using BoxKnownSize = void; }; } @@ -540,7 +540,7 @@ typename octa::detail::BoxIf::Box make_box(A &&...args) { } template -typename octa::detail::BoxIf::BoxUnknownSize make_box(size_t n) { +typename octa::detail::BoxIf::BoxUnknownSize make_box(octa::Size n) { return Box(new octa::RemoveExtent[n]()); } @@ -568,8 +568,8 @@ template<> struct Allocator { }; template struct Allocator { - using Size = size_t; - using Difference = ptrdiff_t; + using Size = octa::Size; + using Difference = octa::Ptrdiff; using Value = T; using Reference = T &; using ConstReference = const T &; @@ -588,10 +588,10 @@ template struct Allocator { Size max_size() const { return Size(~0) / sizeof(T); } Pointer allocate(Size n, Allocator::ConstPointer = nullptr) { - return (Pointer) ::new uchar[n * sizeof(T)]; + return (Pointer) ::new octa::uchar[n * sizeof(T)]; } - void deallocate(Pointer p, Size) { ::delete[] (uchar *) p; } + void deallocate(Pointer p, Size) { ::delete[] (octa::uchar *) p; } template void construct(U *p, A &&...args) { @@ -602,8 +602,8 @@ template struct Allocator { }; template struct Allocator { - using Size = size_t; - using Difference = ptrdiff_t; + using Size = octa::Size; + using Difference = octa::Ptrdiff; using Value = const T; using Reference = const T &; using ConstReference = const T &; @@ -619,10 +619,10 @@ template struct Allocator { Size max_size() const { return Size(~0) / sizeof(T); } Pointer allocate(Size n, Allocator::ConstPointer = nullptr) { - return (Pointer) ::new uchar[n * sizeof(T)]; + return (Pointer) ::new octa::uchar[n * sizeof(T)]; } - void deallocate(Pointer p, Size) { ::delete[] (uchar *) p; } + void deallocate(Pointer p, Size) { ::delete[] (octa::uchar *) p; } template void construct(U *p, A &&...args) { diff --git a/octa/new.h b/octa/new.h index 490ec2e..9204b2e 100644 --- a/octa/new.h +++ b/octa/new.h @@ -6,11 +6,11 @@ #ifndef OCTA_NEW_H #define OCTA_NEW_H -#include - #ifndef OCTA_ALLOW_CXXSTD -inline void *operator new (size_t, void *p) { return p; } -inline void *operator new [](size_t, void *p) { return p; } +#include "octa/types.h" + +inline void *operator new (octa::Size, void *p) { return p; } +inline void *operator new [](octa::Size, void *p) { return p; } inline void operator delete (void *, void *) {} inline void operator delete[](void *, void *) {} #else diff --git a/octa/range.h b/octa/range.h index 0ef8018..fe6ed42 100644 --- a/octa/range.h +++ b/octa/range.h @@ -285,7 +285,7 @@ namespace detail { } template struct InputRange { using Category = C; using Size = S; @@ -325,8 +325,8 @@ template struct OutputRange { using Category = OutputRangeTag; using Size = S; @@ -541,7 +541,7 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> PointerRange(const PointerRange &v): p_beg(v.p_beg), p_end(v.p_end) {} PointerRange(T *beg, T *end): p_beg(beg), p_end(end) {} - PointerRange(T *beg, size_t n): p_beg(beg), p_end(beg + n) {} + PointerRange(T *beg, octa::Size n): p_beg(beg), p_end(beg + n) {} PointerRange &operator=(const PointerRange &v) { p_beg = v.p_beg; @@ -561,8 +561,8 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> --p_beg; return true; } - size_t pop_front_n(size_t n) { - size_t olen = p_end - p_beg; + octa::Size pop_front_n(octa::Size n) { + octa::Size olen = p_end - p_beg; p_beg += n; if (p_beg > p_end) { p_beg = p_end; @@ -571,7 +571,7 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> return n; } - size_t push_front_n(size_t n) { + octa::Size push_front_n(octa::Size n) { p_beg -= n; return true; } @@ -581,7 +581,7 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> return p_beg == range.p_beg; } - ptrdiff_t distance_front(const PointerRange &range) const { + octa::Ptrdiff distance_front(const PointerRange &range) const { return range.p_beg - p_beg; } @@ -595,8 +595,8 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> ++p_end; return true; } - size_t pop_back_n(size_t n) { - size_t olen = p_end - p_beg; + octa::Size pop_back_n(octa::Size n) { + octa::Size olen = p_end - p_beg; p_end -= n; if (p_end < p_beg) { p_end = p_beg; @@ -605,7 +605,7 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> return n; } - size_t push_back_n(size_t n) { + octa::Size push_back_n(octa::Size n) { p_end += n; return true; } @@ -615,18 +615,18 @@ struct PointerRange: InputRange, FiniteRandomAccessRangeTag, T> return p_end == range.p_end; } - ptrdiff_t distance_back(const PointerRange &range) const { + octa::Ptrdiff distance_back(const PointerRange &range) const { return range.p_end - p_end; } /* satisfy FiniteRandomAccessRange */ - size_t size() const { return p_end - p_beg; } + octa::Size size() const { return p_end - p_beg; } - PointerRange slice(size_t start, size_t end) const { + PointerRange slice(octa::Size start, octa::Size end) const { return PointerRange(p_beg + start, p_beg + end); } - T &operator[](size_t i) const { return p_beg[i]; } + T &operator[](octa::Size i) const { return p_beg[i]; } /* satisfy OutputRange */ void put(const T &v) { @@ -864,7 +864,7 @@ auto each(const T &r) -> decltype(r.each()) { return r.each(); } -template +template PointerRange each(T (&array)[N]) { return PointerRange(array, N); } diff --git a/octa/string.h b/octa/string.h index ffe71d2..243ec18 100644 --- a/octa/string.h +++ b/octa/string.h @@ -14,7 +14,7 @@ #include "octa/vector.h" namespace octa { -static constexpr size_t npos = -1; +static constexpr octa::Size npos = -1; template> class StringBase { @@ -25,15 +25,15 @@ class StringBase { } public: - using Size = size_t; - using Difference = ptrdiff_t; + using Size = octa::Size; + using Difference = octa::Ptrdiff; using Value = T; using Reference = T &; using ConstReference = const T &; using Pointer = T *; using ConstPointer = const T *; - using Range = PointerRange; - using ConstRange = PointerRange; + using Range = octa::PointerRange; + using ConstRange = octa::PointerRange; using Allocator = A; StringBase(const A &a = A()): p_buf(1, '\0', a) {} @@ -45,7 +45,7 @@ public: StringBase(StringBase &&s, const A &a): p_buf(octa::move(s.p_buf), a) {} - StringBase(const StringBase &s, size_t pos, size_t len = npos, + StringBase(const StringBase &s, octa::Size pos, octa::Size len = npos, const A &a = A()): p_buf(s.p_buf.each().slice(pos, (len == npos) ? s.p_buf.size() : (pos + len)), a) { @@ -76,21 +76,21 @@ public: return *this; } - void resize(size_t n, T v = T()) { + void resize(octa::Size n, T v = T()) { p_buf.pop(); p_buf.resize(n, v); terminate(); } - void reserve(size_t n) { + void reserve(octa::Size n) { p_buf.reserve(n + 1); } - T &operator[](size_t i) { return p_buf[i]; } - const T &operator[](size_t i) const { return p_buf[i]; } + T &operator[](octa::Size i) { return p_buf[i]; } + const T &operator[](octa::Size i) const { return p_buf[i]; } - T &at(size_t i) { return p_buf[i]; } - const T &at(size_t i) const { return p_buf[i]; } + T &at(octa::Size i) { return p_buf[i]; } + const T &at(octa::Size i) const { return p_buf[i]; } T &front() { return p_buf[0]; } const T &front() const { return p_buf[0]; }; @@ -101,11 +101,11 @@ public: T *data() { return p_buf.data(); } const T *data() const { return p_buf.data(); } - size_t size() const { + octa::Size size() const { return p_buf.size() - 1; } - size_t capacity() const { + octa::Size capacity() const { return p_buf.capacity() - 1; } @@ -122,9 +122,9 @@ public: return *this; } - StringBase &append(const StringBase &s, size_t idx, size_t len) { + StringBase &append(const StringBase &s, octa::Size idx, octa::Size len) { p_buf.pop(); - p_buf.insert_range(p_buf.size(), octa::PointerRange(&s[idx], + p_buf.insert_range(p_buf.size(), Range(&s[idx], (len == npos) ? (s.size() - idx) : len)); terminate(); return *this; @@ -137,9 +137,9 @@ public: return *this; } - StringBase &append(size_t n, T c) { + StringBase &append(octa::Size n, T c) { p_buf.pop(); - for (size_t i = 0; i < n; ++i) p_buf.push(c); + for (octa::Size i = 0; i < n; ++i) p_buf.push(c); p_buf.push('\0'); return *this; } @@ -281,7 +281,7 @@ namespace detail { n = 0; *(s->data()) = '\0'; } - *(((size_t *)s) + 1) = n + 1; + *(((octa::Size *)s) + 1) = n + 1; } } @@ -316,14 +316,15 @@ template<> struct ToString { \ OCTA_TOSTR_NUM(int, "%d") OCTA_TOSTR_NUM(int &, "%d") -OCTA_TOSTR_NUM(uint, "%u") OCTA_TOSTR_NUM(long, "%ld") -OCTA_TOSTR_NUM(ulong, "%lu") -OCTA_TOSTR_NUM(llong, "%lld") -OCTA_TOSTR_NUM(ullong, "%llu") OCTA_TOSTR_NUM(float, "%f") OCTA_TOSTR_NUM(double, "%f") -OCTA_TOSTR_NUM(ldouble, "%Lf") + +OCTA_TOSTR_NUM(octa::uint, "%u") +OCTA_TOSTR_NUM(octa::ulong, "%lu") +OCTA_TOSTR_NUM(octa::llong, "%lld") +OCTA_TOSTR_NUM(octa::ullong, "%llu") +OCTA_TOSTR_NUM(octa::ldouble, "%Lf") #undef OCTA_TOSTR_NUM diff --git a/octa/type_traits.h b/octa/type_traits.h index 5e4a0be..034f3af 100644 --- a/octa/type_traits.h +++ b/octa/type_traits.h @@ -80,8 +80,8 @@ namespace detail { /* is null pointer */ namespace detail { - template struct IsNullPointerBase : False {}; - template< > struct IsNullPointerBase: True {}; + template struct IsNullPointerBase : False {}; + template< > struct IsNullPointerBase: True {}; } template struct IsNullPointer: @@ -94,20 +94,21 @@ namespace detail { template<> struct IsIntegralBase: True {}; template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase: True {}; - template<> struct IsIntegralBase< wchar_t>: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; + template<> struct IsIntegralBase: True {}; } template @@ -118,9 +119,10 @@ struct IsIntegral: octa::detail::IsIntegralBase> {}; namespace detail { template struct IsFloatingPointBase: False {}; - template<> struct IsFloatingPointBase: True {}; - template<> struct IsFloatingPointBase: True {}; - template<> struct IsFloatingPointBase: True {}; + template<> struct IsFloatingPointBase: True {}; + template<> struct IsFloatingPointBase: True {}; + + template<> struct IsFloatingPointBase: True {}; } template @@ -128,9 +130,9 @@ struct IsFloatingPoint: octa::detail::IsFloatingPointBase> {}; /* is array */ -template struct IsArray : False {}; -template struct IsArray: True {}; -template struct IsArray: True {}; +template struct IsArray : False {}; +template struct IsArray: True {}; +template struct IsArray: True {}; /* is pointer */ @@ -412,11 +414,11 @@ namespace detail { > {}; /* array types are default constructible if their element type is */ - template + template struct CtibleCore: Ctible> {}; /* otherwise array types are not constructible by this syntax */ - template + template struct CtibleCore: False {}; /* incomplete array types are not constructible */ @@ -646,30 +648,30 @@ template struct IsSame: True {}; /* extent */ -template -struct Extent: IntegralConstant {}; +template +struct Extent: IntegralConstant {}; template -struct Extent: IntegralConstant {}; +struct Extent: IntegralConstant {}; -template -struct Extent: IntegralConstant::value> {}; +template +struct Extent: IntegralConstant::value> {}; -template -struct Extent: IntegralConstant {}; +template +struct Extent: IntegralConstant {}; -template -struct Extent: IntegralConstant::value> {}; +template +struct Extent: IntegralConstant::value> {}; /* rank */ -template struct Rank: IntegralConstant {}; +template struct Rank: IntegralConstant {}; template -struct Rank: IntegralConstant::value + 1> {}; +struct Rank: IntegralConstant::value + 1> {}; -template -struct Rank: IntegralConstant::value + 1> {}; +template +struct Rank: IntegralConstant::value + 1> {}; /* remove const, volatile, cv */ @@ -825,7 +827,7 @@ namespace detail { struct RemoveExtentBase { using Type = T; }; template struct RemoveExtentBase { using Type = T; }; - template + template struct RemoveExtentBase { using Type = T; }; } @@ -841,7 +843,7 @@ namespace detail { using Type = RemoveAllExtentsBase; }; - template struct RemoveAllExtentsBase { + template struct RemoveAllExtentsBase { using Type = RemoveAllExtentsBase; }; } @@ -866,27 +868,27 @@ namespace detail { ~TlNat() = delete; }; - using Stypes = TypeList>>>>; + TypeList>>>>; - using Utypes = TypeList>>>>; + using Utypes = TypeList>>>>; - template + template struct TypeFindFirst; - template + template struct TypeFindFirst, N, true> { using Type = T; }; - template + template struct TypeFindFirst, N, false> { using Type = typename TypeFindFirst::Type; }; @@ -947,28 +949,30 @@ namespace detail { }; template<> struct MakeSigned {}; - template<> struct MakeSigned { using Type = schar; }; - template<> struct MakeSigned { using Type = schar; }; template<> struct MakeSigned { using Type = short; }; - template<> struct MakeSigned { using Type = short; }; template<> struct MakeSigned { using Type = int; }; - template<> struct MakeSigned { using Type = int; }; template<> struct MakeSigned { using Type = long; }; - template<> struct MakeSigned { using Type = long; }; - template<> struct MakeSigned { using Type = llong; }; - template<> struct MakeSigned { using Type = llong; }; + + template<> struct MakeSigned { using Type = octa::schar; }; + template<> struct MakeSigned { using Type = octa::schar; }; + template<> struct MakeSigned { using Type = short; }; + template<> struct MakeSigned { using Type = int; }; + template<> struct MakeSigned { using Type = long; }; + template<> struct MakeSigned { using Type = octa::llong; }; + template<> struct MakeSigned { using Type = octa::llong; }; template<> struct MakeUnsigned {}; - template<> struct MakeUnsigned { using Type = uchar; }; - template<> struct MakeUnsigned { using Type = uchar; }; - template<> struct MakeUnsigned { using Type = ushort; }; - template<> struct MakeUnsigned { using Type = ushort; }; - template<> struct MakeUnsigned { using Type = uint; }; - template<> struct MakeUnsigned { using Type = uint; }; - template<> struct MakeUnsigned { using Type = ulong; }; - template<> struct MakeUnsigned { using Type = ulong; }; - template<> struct MakeUnsigned { using Type = ullong; }; - template<> struct MakeUnsigned { using Type = ullong; }; + template<> struct MakeUnsigned { using Type = octa::ushort; }; + template<> struct MakeUnsigned { using Type = octa::uint; }; + template<> struct MakeUnsigned { using Type = octa::ulong; }; + + template<> struct MakeUnsigned { using Type = octa::uchar; }; + template<> struct MakeUnsigned { using Type = octa::uchar; }; + template<> struct MakeUnsigned { using Type = octa::ushort; }; + template<> struct MakeUnsigned { using Type = octa::uint; }; + template<> struct MakeUnsigned { using Type = octa::ulong; }; + template<> struct MakeUnsigned { using Type = octa::ullong; }; + template<> struct MakeUnsigned { using Type = octa::ullong; }; template struct MakeSignedBase { using Type = typename ApplyCv::Type; /* aligned storage */ namespace detail { - template struct AlignedTest { + template struct AlignedTest { union type { - uchar data[N]; + octa::uchar data[N]; octa::MaxAlign align; }; }; - template struct AlignedStorageBase { + template struct AlignedStorageBase { struct type { - alignas(A) uchar data[N]; + alignas(A) octa::uchar data[N]; }; }; } -template::Type) > using AlignedStorage = typename octa::detail::AlignedStorageBase::Type; /* aligned union */ namespace detail { - template struct AlignMax; + template struct AlignMax; - template struct AlignMax { - static constexpr size_t value = N; + template struct AlignMax { + static constexpr octa::Size value = N; }; - template struct AlignMax { - static constexpr size_t value = (N1 > N2) ? N1 : N2; + template struct AlignMax { + static constexpr octa::Size value = (N1 > N2) ? N1 : N2; }; - template + template struct AlignMax { - static constexpr size_t value + static constexpr octa::Size value = AlignMax::value, N...>::value; }; - template struct AlignedUnionBase { - static constexpr size_t alignment_value + template struct AlignedUnionBase { + static constexpr octa::Size alignment_value = AlignMax::value; struct type { - alignas(alignment_value) uchar data[AlignMax::value]; }; }; } /* namespace detail */ -template +template using AlignedUnion = typename octa::detail::AlignedUnionBase::Type; /* underlying type */ diff --git a/octa/types.h b/octa/types.h index e147ec7..7287fc4 100644 --- a/octa/types.h +++ b/octa/types.h @@ -23,6 +23,12 @@ using llong = long long; using ldouble = long double; +/* keywords in c++, but aliased */ + +using Wchar = wchar_t; +using Char16 = char16_t; +using Char32 = char32_t; + /* nullptr type */ using Nullptr = decltype(nullptr); diff --git a/octa/utility.h b/octa/utility.h index e6b927c..fb7a579 100644 --- a/octa/utility.h +++ b/octa/utility.h @@ -74,8 +74,8 @@ template void swap(T &a, T &b) { octa::detail::swap(a, b); } -template void swap(T (&a)[N], T (&b)[N]) { - for (size_t i = 0; i < N; ++i) { +template void swap(T (&a)[N], T (&b)[N]) { + for (octa::Size i = 0; i < N; ++i) { octa::swap(a[i], b[i]); } } diff --git a/octa/vector.h b/octa/vector.h index 7024e59..379c82e 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -62,12 +62,12 @@ class Vector { using VecPair = octa::detail::VectorPair; VecPair p_buf; - size_t p_len, p_cap; + octa::Size p_len, p_cap; - void insert_base(size_t idx, size_t n) { + void insert_base(octa::Size idx, octa::Size n) { if (p_len + n > p_cap) reserve(p_len + n); p_len += n; - for (size_t i = p_len - 1; i > idx + n - 1; --i) { + for (octa::Size i = p_len - 1; i > idx + n - 1; --i) { p_buf.p_ptr[i] = octa::move(p_buf.p_ptr[i - n]); } } @@ -79,7 +79,7 @@ class Vector { octa::RangeSize l = range.size(); reserve(l); p_len = l; - for (size_t i = 0; !range.empty(); range.pop_front()) { + for (octa::Size i = 0; !range.empty(); range.pop_front()) { octa::allocator_construct(p_buf.get_alloc(), &p_buf.p_ptr[i], range.front()); ++i; @@ -90,7 +90,7 @@ class Vector { void ctor_from_range(R &range, EnableIf< !octa::IsFiniteRandomAccessRange::value, bool > = true) { - size_t i = 0; + octa::Size i = 0; for (; !range.empty(); range.pop_front()) { reserve(i + 1); octa::allocator_construct(p_buf.get_alloc(), @@ -114,20 +114,20 @@ class Vector { } public: - using Size = size_t; - using Difference = ptrdiff_t; + using Size = octa::Size; + using Difference = octa::Ptrdiff; using Value = T; using Reference = T &; using ConstReference = const T &; using Pointer = T *; using ConstPointer = const T *; - using Range = PointerRange; - using ConstRange = PointerRange; + using Range = octa::PointerRange; + using ConstRange = octa::PointerRange; using Allocator = A; Vector(const A &a = A()): p_buf(nullptr, a), p_len(0), p_cap(0) {} - explicit Vector(size_t n, const T &val = T(), + explicit Vector(Size n, const T &val = T(), const A &al = A()): Vector(al) { p_buf.p_ptr = octa::allocator_allocate(p_buf.get_alloc(), n); p_len = p_cap = n; @@ -183,10 +183,10 @@ public: } Vector(InitializerList v, const A &a = A()): Vector(a) { - size_t l = v.end() - v.begin(); + Size l = v.end() - v.begin(); const T *ptr = v.begin(); reserve(l); - for (size_t i = 0; i < l; ++i) + for (Size i = 0; i < l; ++i) octa::allocator_construct(p_buf.get_alloc(), &p_buf.p_ptr[i], ptr[i]); p_len = l; @@ -232,7 +232,7 @@ public: Vector &operator=(InitializerList il) { clear(); - size_t ilen = il.end() - il.begin(); + Size ilen = il.end() - il.begin(); reserve(ilen); if (octa::IsPod()) { memcpy(p_buf.p_ptr, il.begin(), ilen); @@ -254,12 +254,12 @@ public: ctor_from_range(range); } - void resize(size_t n, const T &v = T()) { - size_t l = p_len; + void resize(Size n, const T &v = T()) { + Size l = p_len; reserve(n); p_len = n; if (octa::IsPod()) { - for (size_t i = l; i < p_len; ++i) { + for (Size i = l; i < p_len; ++i) { p_buf.p_ptr[i] = T(v); } } else { @@ -270,11 +270,11 @@ public: } } - void reserve(size_t n) { + void reserve(Size n) { if (n <= p_cap) return; - size_t oc = p_cap; + Size oc = p_cap; if (!oc) { - p_cap = octa::max(n, size_t(8)); + p_cap = octa::max(n, Size(8)); } else { while (p_cap < n) p_cap *= 2; } @@ -297,11 +297,11 @@ public: p_buf.p_ptr = tmp; } - T &operator[](size_t i) { return p_buf.p_ptr[i]; } - const T &operator[](size_t i) const { return p_buf.p_ptr[i]; } + T &operator[](Size i) { return p_buf.p_ptr[i]; } + const T &operator[](Size i) const { return p_buf.p_ptr[i]; } - T &at(size_t i) { return p_buf.p_ptr[i]; } - const T &at(size_t i) const { return p_buf.p_ptr[i]; } + T &at(Size i) { return p_buf.p_ptr[i]; } + const T &at(Size i) const { return p_buf.p_ptr[i]; } T &push(const T &v) { if (p_len == p_cap) reserve(p_len + 1); @@ -342,13 +342,13 @@ public: T *data() { return p_buf.p_ptr; } const T *data() const { return p_buf.p_ptr; } - size_t size() const { return p_len; } - size_t capacity() const { return p_cap; } + Size size() const { return p_len; } + Size capacity() const { return p_cap; } bool empty() const { return (p_len == 0); } - bool in_range(size_t idx) { return idx < p_len; } - bool in_range(int idx) { return idx >= 0 && size_t(idx) < p_len; } + 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) { return ptr >= p_buf.p_ptr && ptr < &p_buf.p_ptr[p_len]; } @@ -360,38 +360,38 @@ public: return r; } - T *insert(size_t idx, T &&v) { + T *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_t idx, const T &v) { + T *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_t idx, size_t n, const T &v) { + T *insert(Size idx, Size n, const T &v) { insert_base(idx, n); - for (size_t i = 0; i < n; ++i) { + for (Size i = 0; i < n; ++i) { p_buf.p_ptr[idx + i] = v; } return &p_buf.p_ptr[idx]; } template - T *insert_range(size_t idx, U range) { - size_t l = range.size(); + T *insert_range(Size idx, U range) { + Size l = range.size(); insert_base(idx, l); - for (size_t i = 0; i < l; ++i) { + for (Size i = 0; i < l; ++i) { p_buf.p_ptr[idx + i] = range.front(); range.pop_front(); } return &p_buf.p_ptr[idx]; } - T *insert(size_t idx, InitializerList il) { + T *insert(Size idx, InitializerList il) { return insert_range(idx, octa::each(il)); } diff --git a/src/new.cpp b/src/new.cpp index 51e8262..72e8fdf 100644 --- a/src/new.cpp +++ b/src/new.cpp @@ -5,13 +5,15 @@ #include -void *operator new(size_t size) { +#include "octa/types.h" + +void *operator new(octa::Size size) { void *p = malloc(size); if (!p) abort(); return p; } -void *operator new[](size_t size) { +void *operator new[](octa::Size size) { void *p = malloc(size); if (!p) abort(); return p;