diff --git a/.gitignore b/.gitignore index 6bb74d77..a5bbabed 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ Makefile Makefile.in /missing +/preprocessor.hpp /stamp-h1 /test-driver /uri.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 08633272..3664382d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,8 @@ include (nc) include (search_libs) -find_package(RAGEL 6.9 REQUIRED) +find_package (PythonInterp 3 REQUIRED) +find_package (RAGEL 6.9 REQUIRED) ############################################################################### @@ -138,26 +139,26 @@ list ( algo/sort.hpp algo/sort.ipp alloc/fwd.hpp - alloc/affix.cpp - alloc/affix.hpp - alloc/aligned.hpp alloc/allocator.cpp alloc/allocator.hpp alloc/allocator.ipp alloc/arena.cpp alloc/arena.hpp alloc/arena.ipp - alloc/dynamic.hpp - alloc/fallback.cpp - alloc/fallback.hpp - alloc/linear.cpp - alloc/linear.hpp - alloc/malloc.cpp - alloc/malloc.hpp - alloc/null.cpp - alloc/null.hpp - alloc/stack.cpp - alloc/stack.hpp + alloc/raw/affix.cpp + alloc/raw/affix.hpp + alloc/raw/aligned.hpp + alloc/raw/dynamic.hpp + alloc/raw/fallback.cpp + alloc/raw/fallback.hpp + alloc/raw/linear.cpp + alloc/raw/linear.hpp + alloc/raw/malloc.cpp + alloc/raw/malloc.hpp + alloc/raw/null.cpp + alloc/raw/null.hpp + alloc/raw/stack.cpp + alloc/raw/stack.hpp annotation.hpp ascii.hpp backtrace.hpp @@ -187,6 +188,8 @@ list ( crypto/arc4.hpp crypto/ice.cpp crypto/ice.hpp + crypto/salsa.cpp + crypto/salsa.hpp crypto/tea.cpp crypto/tea.hpp crypto/xtea.cpp @@ -216,7 +219,6 @@ list ( geom/fwd.hpp geom/aabb.cpp geom/aabb.hpp - geom/aabb.ipp geom/cylinder.cpp geom/cylinder.hpp geom/ellipse.cpp @@ -232,7 +234,6 @@ list ( geom/rect.cpp geom/rect.hpp geom/sample.hpp - geom/sample.ipp geom/sphere.cpp geom/sphere.hpp geom/tri.cpp @@ -294,6 +295,8 @@ list ( io.hpp io.ipp iterator.hpp + job/queue.cpp + job/queue.hpp json/fwd.hpp json/except.cpp json/except.hpp @@ -303,6 +306,18 @@ list ( json/schema.hpp json/tree.cpp json/tree.hpp + json2/fwd.hpp + json2/event.hpp + json2/event.cpp + json2/except.hpp + json2/personality/base.cpp + json2/personality/base.hpp + json2/personality/jsonish.cpp + json2/personality/jsonish.hpp + json2/personality/rfc7519.cpp + json2/personality/rfc7519.hpp + json2/tree.cpp + json2/tree.hpp library.hpp log.cpp log.hpp @@ -318,6 +333,8 @@ list ( memory/deleter.cpp memory/deleter.hpp nocopy.hpp + parse.cpp + parse.hpp pascal.cpp pascal.hpp platform.hpp @@ -391,6 +408,8 @@ list ( types/traits.hpp uri.cpp uri.hpp + utf8.cpp + utf8.hpp variadic.cpp variadic.hpp vector.cpp @@ -399,11 +418,28 @@ list ( version.cpp version.hpp view.cpp - view.ipp view.hpp ) +##----------------------------------------------------------------------------- +## We shouldn't be building into the source directory, but I can't stand trying +## to coax CMake into behaving here any longer. Feel free to fix it. +add_custom_command ( +OUTPUT + "${CMAKE_CURRENT_SOURCE_DIR}/preprocessor.hpp" +COMMENT + "[preprocessor.py] preprocessor.hpp" +COMMAND + "${PYTHON_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/preprocessor.py" + "${CMAKE_CURRENT_SOURCE_DIR}/preprocessor.hpp" + 320 +DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/preprocessor.py" +) + + ############################################################################### add_library(cruft-util ${UTIL_FILES}) target_link_libraries (cruft-util PUBLIC ${LIBS}) @@ -457,9 +493,11 @@ if (TESTS) coord crypto/arc4 crypto/ice + crypto/salsa crypto/tea crypto/xtea crypto/xxtea + endian exe extent fixed @@ -484,13 +522,17 @@ if (TESTS) hton introspection iterator + job/queue json_types + json2/event maths matrix memory/deleter + parse point polynomial pool + preprocessor quaternion rand/buckets range @@ -504,8 +546,10 @@ if (TESTS) stringid strongdef tuple + traits typeidx uri + utf8 vector version view diff --git a/adapter.hpp b/adapter.hpp index a5b87a05..c7d1ec7c 100644 --- a/adapter.hpp +++ b/adapter.hpp @@ -30,20 +30,28 @@ namespace util::adapter { m_target (_target) { ; } - auto begin (void) { return m_target.begin (); } - auto end (void) { return m_target.end (); } + auto begin (void) & { return m_target.rbegin (); } + auto end (void) & { return m_target.rend (); } - auto begin (void) const { return m_target.begin (); } - auto end (void) const { return m_target.end (); } + auto begin (void) const& { return m_target.rbegin (); } + auto end (void) const& { return m_target.rend (); } - auto cbegin (void) { return m_target.cbegin (); } - auto cend (void) { return m_target.cend (); } + auto cbegin (void) const& { return m_target.crbegin (); } + auto cend (void) const& { return m_target.crend (); } private: T &m_target; }; + template + auto + make_reverse (Container &c) + { + return reverse { c }; + }; + + // adapt a container's range methods to return indices rather than iterators template struct indices { diff --git a/alloc/arena.hpp b/alloc/arena.hpp index ddfe9663..f9ca676a 100644 --- a/alloc/arena.hpp +++ b/alloc/arena.hpp @@ -23,6 +23,8 @@ #include namespace util::alloc { + /// wraps a block allocator with an interface suitable for allocating + /// individual objects. template class arena { public: diff --git a/alloc/fallback.cpp b/alloc/fallback.cpp deleted file mode 100644 index fa2fe52e..00000000 --- a/alloc/fallback.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "fallback.hpp" diff --git a/alloc/fwd.hpp b/alloc/fwd.hpp index d9e3ac57..cb3fd8a5 100644 --- a/alloc/fwd.hpp +++ b/alloc/fwd.hpp @@ -19,17 +19,20 @@ namespace util::alloc { - class affix; - class fallback; - class linear; - class malloc; - class null; - class stack; + namespace raw { + class affix; + class fallback; + class linear; + class malloc; + class null; + class stack; - class dynamic; + class dynamic; + + template + class aligned; + } - template - class aligned; template class arena; template class allocator; diff --git a/alloc/affix.cpp b/alloc/raw/affix.cpp similarity index 95% rename from alloc/affix.cpp rename to alloc/raw/affix.cpp index 26ebd72d..b9159357 100644 --- a/alloc/affix.cpp +++ b/alloc/raw/affix.cpp @@ -16,7 +16,8 @@ #include "affix.hpp" -using util::alloc::affix; +using util::alloc::raw::affix; /////////////////////////////////////////////////////////////////////////////// + diff --git a/alloc/affix.hpp b/alloc/raw/affix.hpp similarity index 62% rename from alloc/affix.hpp rename to alloc/raw/affix.hpp index a337280a..ef80effc 100644 --- a/alloc/affix.hpp +++ b/alloc/raw/affix.hpp @@ -14,13 +14,20 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_AFFIX_HPP -#define __UTIL_ALLOC_AFFIX_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP +#define CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP #include -namespace util::alloc { - template +namespace util::alloc::raw { + /// a raw memory allocator which initialises an instance of PrefixT + /// immediately before each allocation and, if specified, an instance + /// of SuffixT after the allocation. + /// + /// uses an instance of ParentT to actually perform the bulk allocations. + /// + /// useful for sentinels, reference counts, etc. + template class affix { void* allocate (size_t bytes); void* allocate (size_t bytes, size_t align); @@ -28,8 +35,8 @@ namespace util::alloc { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); - void* base (void); - const void* base (void) const; + void* begin (void); + const void* begin (void) const; size_t offset (const void*) const; }; diff --git a/alloc/aligned.hpp b/alloc/raw/aligned.hpp similarity index 80% rename from alloc/aligned.hpp rename to alloc/raw/aligned.hpp index 2cd034d0..683ec81f 100644 --- a/alloc/aligned.hpp +++ b/alloc/raw/aligned.hpp @@ -14,17 +14,22 @@ * Copyright 2016 Danny Robson */ -#ifndef __CRUFT_UTIL_ALLOC_ALIGNED_HPP -#define __CRUFT_UTIL_ALLOC_ALIGNED_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP +#define CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP -namespace util::alloc { +#include +#include + +#include "../../debug.hpp" + +namespace util::alloc::raw { /// wraps a child allocator and enforces a fixed alignment template class aligned { public: /////////////////////////////////////////////////////////////////////// template - aligned (size_t _alignment, Args &&...args): + aligned (std::size_t _alignment, Args &&...args): m_successor (std::forward (args)...), m_alignment (_alignment) { ; } @@ -32,14 +37,14 @@ namespace util::alloc { /////////////////////////////////////////////////////////////////////// auto - allocate (size_t bytes) + allocate (std::size_t bytes) { return m_successor.allocate (bytes, m_alignment); } //--------------------------------------------------------------------- auto - allocate (size_t bytes, size_t alignment) + allocate (std::size_t bytes, std::size_t alignment) { (void)alignment; CHECK_EQ (alignment, m_alignment); @@ -49,7 +54,7 @@ namespace util::alloc { //--------------------------------------------------------------------- auto - deallocate (void *ptr, size_t bytes) + deallocate (void *ptr, std::size_t bytes) { return m_successor.deallocate (ptr, bytes); } @@ -57,7 +62,7 @@ namespace util::alloc { //--------------------------------------------------------------------- auto - deallocate (void *ptr, size_t bytes, size_t alignment) + deallocate (void *ptr, std::size_t bytes, std::size_t alignment) { (void)alignment; CHECK_EQ (alignment, m_alignment); @@ -66,8 +71,8 @@ namespace util::alloc { /////////////////////////////////////////////////////////////////////// - auto base (void) { return m_successor.base (); } - auto base (void) const { return m_successor.base (); } + auto begin (void) { return m_successor.begin (); } + auto begin (void) const { return m_successor.begin (); } //--------------------------------------------------------------------- @@ -90,7 +95,7 @@ namespace util::alloc { private: ChildT m_successor; - size_t m_alignment; + std::size_t m_alignment; }; } diff --git a/alloc/dynamic.hpp b/alloc/raw/dynamic.hpp similarity index 91% rename from alloc/dynamic.hpp rename to alloc/raw/dynamic.hpp index 62b1ce5f..b7bd5418 100644 --- a/alloc/dynamic.hpp +++ b/alloc/raw/dynamic.hpp @@ -14,13 +14,13 @@ * Copyright 2016 Danny Robson */ -#ifndef __UTIL_ALLOC_DYNAMIC_HPP -#define __UTIL_ALLOC_DYNAMIC_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP +#define CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP #include #include -namespace util::alloc { +namespace util::alloc::raw { // wraps an allocator given at construction time, forwarding all calls to // the inner object. used to allow virtual dispatch of the non-virtual // allocator interface. @@ -57,8 +57,8 @@ namespace util::alloc { auto deallocate (void *ptr, size_t bytes, size_t alignment) { return m_child->deallocate (ptr, bytes, alignment); } //--------------------------------------------------------------------- - auto base (void) { return m_child->base (); } - auto base (void) const { return m_child->base (); } + auto begin (void) { return m_child->begin (); } + auto begin (void) const { return m_child->begin (); } auto offset (const void *ptr) const { return m_child->offset (ptr); } @@ -93,8 +93,8 @@ namespace util::alloc { virtual void deallocate (void *ptr, size_t bytes) = 0; virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0; - virtual void* base (void) = 0; - virtual const void* base (void) const = 0; + virtual void* begin (void) = 0; + virtual const void* begin (void) const = 0; virtual size_t offset (const void*) const = 0; virtual void reset (void) = 0; @@ -133,12 +133,12 @@ namespace util::alloc { { m_target.deallocate (ptr, bytes, alignment); } const void* - base (void) const override - { return m_target.base (); } + begin (void) const override + { return m_target.begin (); } void* - base (void) override - { return m_target.base (); } + begin (void) override + { return m_target.begin (); } size_t offset (const void *ptr) const override diff --git a/alloc/raw/fallback.cpp b/alloc/raw/fallback.cpp new file mode 100644 index 00000000..1c1762f0 --- /dev/null +++ b/alloc/raw/fallback.cpp @@ -0,0 +1,17 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2017 Danny Robson + */ + +#include "./fallback.hpp" diff --git a/alloc/fallback.hpp b/alloc/raw/fallback.hpp similarity index 63% rename from alloc/fallback.hpp rename to alloc/raw/fallback.hpp index e4a2e875..22ec0b32 100644 --- a/alloc/fallback.hpp +++ b/alloc/raw/fallback.hpp @@ -11,25 +11,33 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2017 Danny Robson */ -#ifndef __UTIL_ALLOC_FALLBACK_HPP -#define __UTIL_ALLOC_FALLBACK_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP +#define CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP #include +#include -namespace util::alloc { - template +namespace util::alloc::raw { + /// A raw memory allocator that allocates memory series of child + /// allocators, preferring earlier allocators. + template class fallback { public: - fallback (A&, B&); + fallback (ChildT &..._children): + m_children (_children...) + { ; } void* allocate (size_t bytes); void* allocate (size_t bytes, size_t align); void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); + + private: + std::tuple m_children; }; } diff --git a/alloc/linear.cpp b/alloc/raw/linear.cpp similarity index 95% rename from alloc/linear.cpp rename to alloc/raw/linear.cpp index 07f03416..801aa9fb 100644 --- a/alloc/linear.cpp +++ b/alloc/raw/linear.cpp @@ -16,10 +16,10 @@ #include "linear.hpp" -#include "../pointer.hpp" -#include "../debug.hpp" +#include "../../pointer.hpp" +#include "../../debug.hpp" -using util::alloc::linear; +using util::alloc::raw::linear; /////////////////////////////////////////////////////////////////////////////// @@ -30,7 +30,6 @@ linear::linear (void *begin, void *end): { CHECK_NEZ (begin); CHECK_NEZ (end); - CHECK_LE (begin, end); } @@ -78,7 +77,7 @@ linear::deallocate (void *ptr, size_t bytes, size_t alignment) //----------------------------------------------------------------------------- void* -linear::base (void) +linear::begin (void) { return m_begin; } @@ -86,7 +85,7 @@ linear::base (void) //----------------------------------------------------------------------------- const void* -linear::base (void) const +linear::begin (void) const { return m_begin; } diff --git a/alloc/linear.hpp b/alloc/raw/linear.hpp similarity index 76% rename from alloc/linear.hpp rename to alloc/raw/linear.hpp index 07c4d82e..82f496da 100644 --- a/alloc/linear.hpp +++ b/alloc/raw/linear.hpp @@ -14,12 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_LINEAR_HPP -#define __UTIL_ALLOC_LINEAR_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_LINEAR_HPP +#define CRUFT_UTIL_ALLOC_RAW_LINEAR_HPP #include +#include -namespace util::alloc { +namespace util::alloc::raw { // allocate progressively across a buffer without concern for deallocation. // deallocation is a noop; the only way to free allocations is via reset. class linear { @@ -31,14 +32,25 @@ namespace util::alloc { linear (void *begin, void *end); + template + linear (T &&view): + linear (std::begin (view), std::end (view)) + { ; } + void* allocate (size_t bytes); void* allocate (size_t bytes, size_t alignment); void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t alignment); - void* base (void); - const void* base (void) const; + void* begin (void); + void* end (void); + void* cursor (void); + + const void* begin (void) const; + const void* end (void) const; + const void* cursor (void) const; + size_t offset (const void*) const; void reset (void); diff --git a/alloc/malloc.cpp b/alloc/raw/malloc.cpp similarity index 96% rename from alloc/malloc.cpp rename to alloc/raw/malloc.cpp index 072b1892..86eb5238 100644 --- a/alloc/malloc.cpp +++ b/alloc/raw/malloc.cpp @@ -16,11 +16,11 @@ #include "malloc.hpp" -#include "../debug.hpp" +#include "../../debug.hpp" #include -using util::alloc::malloc; +using util::alloc::raw::malloc; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/malloc.hpp b/alloc/raw/malloc.hpp similarity index 88% rename from alloc/malloc.hpp rename to alloc/raw/malloc.hpp index 8137ce80..cdaaf93a 100644 --- a/alloc/malloc.hpp +++ b/alloc/raw/malloc.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_MALLOC_HPP -#define __UTIL_ALLOC_MALLOC_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP +#define CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP #include -namespace util::alloc { +namespace util::alloc::raw { class malloc { public: void* allocate (size_t bytes); @@ -32,4 +32,4 @@ namespace util::alloc { } -#endif +#endif \ No newline at end of file diff --git a/alloc/null.cpp b/alloc/raw/null.cpp similarity index 96% rename from alloc/null.cpp rename to alloc/raw/null.cpp index 4dc80c02..cc3ce234 100644 --- a/alloc/null.cpp +++ b/alloc/raw/null.cpp @@ -17,11 +17,11 @@ #include "null.hpp" -#include "../debug.hpp" +#include "../../debug.hpp" #include -using util::alloc::null; +using util::alloc::raw::null; /////////////////////////////////////////////////////////////////////////////// @@ -68,7 +68,7 @@ null::deallocate (void *ptr, size_t bytes, size_t align) /////////////////////////////////////////////////////////////////////////////// void* -null::base (void) +null::begin (void) { return nullptr; } @@ -76,7 +76,7 @@ null::base (void) //----------------------------------------------------------------------------- const void* -null::base (void) const +null::begin (void) const { return nullptr; } diff --git a/alloc/null.hpp b/alloc/raw/null.hpp similarity index 88% rename from alloc/null.hpp rename to alloc/raw/null.hpp index 162b7800..9003c424 100644 --- a/alloc/null.hpp +++ b/alloc/raw/null.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_NULL_HPP -#define __UTIL_ALLOC_NULL_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP +#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP #include -namespace util::alloc { +namespace util::alloc::raw { // allocator that always fails, throwing bad_alloc. deallocate will // succeed with nullptr as with delete, but is undefined with other values // (it is likely to at least assert). @@ -35,8 +35,8 @@ namespace util::alloc { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); - void* base (void); - const void* base (void) const; + void* begin (void); + const void* begin (void) const; size_t offset (const void*) const; void reset (void); diff --git a/alloc/stack.cpp b/alloc/raw/stack.cpp similarity index 96% rename from alloc/stack.cpp rename to alloc/raw/stack.cpp index 95f8a7c2..d525c906 100644 --- a/alloc/stack.cpp +++ b/alloc/raw/stack.cpp @@ -16,11 +16,11 @@ #include "stack.hpp" -#include "../debug.hpp" -#include "../pointer.hpp" -#include "../cast.hpp" +#include "../../debug.hpp" +#include "../../pointer.hpp" +#include "../../cast.hpp" -using util::alloc::stack; +using util::alloc::raw::stack; /////////////////////////////////////////////////////////////////////////////// @@ -113,7 +113,7 @@ stack::deallocate (void *_ptr, size_t bytes, size_t alignment) //----------------------------------------------------------------------------- void* -stack::base (void) +stack::begin (void) { return m_begin; } @@ -121,7 +121,7 @@ stack::base (void) //----------------------------------------------------------------------------- const void* -stack::base (void) const +stack::begin (void) const { return m_begin; } diff --git a/alloc/stack.hpp b/alloc/raw/stack.hpp similarity index 89% rename from alloc/stack.hpp rename to alloc/raw/stack.hpp index 272ba54a..11288ce9 100644 --- a/alloc/stack.hpp +++ b/alloc/raw/stack.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_STACK_HPP -#define __UTIL_ALLOC_STACK_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP +#define CRUFT_UTIL_ALLOC_RAW_STACK_HPP #include -namespace util::alloc { +namespace util::alloc::raw { // allocate memory from a buffer in a stacklike manner. deallocation that // is not correctly ordered has undefined (read 'bad') results. class stack { @@ -38,8 +38,8 @@ namespace util::alloc { void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t alignment); - void* base (void); - const void* base (void) const; + void* begin (void); + const void* begin (void) const; size_t offset (const void*) const; void reset (void); diff --git a/backtrace_execinfo.cpp b/backtrace_execinfo.cpp index 0c93ecf8..c7d4b2f8 100644 --- a/backtrace_execinfo.cpp +++ b/backtrace_execinfo.cpp @@ -60,7 +60,7 @@ addr2line (const void *addr) ); // inefficient to copy from vector to string, but it's not a high priority path - auto data = util::slurp (stream.get ()); + auto data = util::slurp (stream.get ()); return std::string (data.cbegin (), data.cend ()); #else diff --git a/coord/base.hpp b/coord/base.hpp index 81065d44..c2ff6ced 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -11,14 +11,15 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2012-2016 Danny Robson + * Copyright 2012-2017 Danny Robson */ -#ifndef __UTIL_COORD_BASE_HPP -#define __UTIL_COORD_BASE_HPP +#ifndef CRUFT_UTIL_COORD_BASE_HPP +#define CRUFT_UTIL_COORD_BASE_HPP #include "fwd.hpp" +#include "ops.hpp" #include "init.hpp" #include "traits.hpp" #include "../maths.hpp" @@ -37,7 +38,7 @@ namespace util::coord { // parameters are others (eg, vector2f). ie, it does not make sense to // allow redim, or type changing on some types so they just aren't exposed. template < - size_t S, + std::size_t S, typename T, typename SelfT > @@ -47,17 +48,29 @@ namespace util::coord { static_assert (sizeof (init) == S * sizeof (T)); using value_type = T; - static constexpr size_t dimension = S; - static constexpr size_t elements = S; + static constexpr std::size_t dimension = S; + static constexpr std::size_t elements = S; + /// returns the number of elements we contain static constexpr auto size (void) { return S; } // constructors using init::init; + + /// constructs, but does not initialise, the data. + /// + /// used to avoid unnecessary initialisation in many situations where + /// we have arrays of these types that are about to be overwritten. it + /// is a very important performance optimisation. base () = default; - constexpr explicit base (T val) - { std::fill (begin (), end (), val); } + /// constructs an instance where all elements are initialised to `val'. + constexpr explicit + base (T fill) + { + for (decltype(S) i = 0; i < S; ++i) + this->data[i] = fill; + } constexpr base (const base &rhs) = default; base& operator= (const base &rhs) = default; @@ -85,7 +98,7 @@ namespace util::coord { /////////////////////////////////////////////////////////////////////// // conversions - template