From d9d02c9024c14a7d0e8e84e65600959164858187 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Mon, 5 May 2025 18:19:34 +0200 Subject: [PATCH] Started porting newest version of MxTasking. --- .../ealanos/include/ealanos/util/ecpp/LICENSE | 21 + .../include/ealanos/util/ecpp/static_vector.h | 960 + repos/ealanos/include/ealanos/util/json.hpp | 20842 ++++++++++++++++ .../ealanos/include/ealanos/util/tsl/LICENSE | 21 + .../ealanos/util/tsl/robin_growth_policy.h | 415 + .../include/ealanos/util/tsl/robin_hash.h | 1589 ++ .../include/ealanos/util/tsl/robin_map.h | 815 + .../include/ealanos/util/tsl/robin_set.h | 668 + repos/ealanos/lib/mk/mxtasking.mk | 28 + repos/ealanos/src/lib/mx/LICENSE | 22 + repos/ealanos/src/lib/mx/README.md | 33 + repos/ealanos/src/lib/mx/io/network/config.h | 9 + .../ealanos/src/lib/mx/io/network/server.cpp | 153 + repos/ealanos/src/lib/mx/io/network/server.d | 662 + repos/ealanos/src/lib/mx/io/network/server.h | 76 + .../src/lib/mx/memory/alignment_helper.h | 39 + repos/ealanos/src/lib/mx/memory/config.h | 32 + .../lib/mx/memory/dynamic_size_allocator.cpp | 331 + .../lib/mx/memory/dynamic_size_allocator.d | 446 + .../lib/mx/memory/dynamic_size_allocator.h | 186 + .../src/lib/mx/memory/fixed_size_allocator.h | 396 + repos/ealanos/src/lib/mx/memory/global_heap.h | 60 + .../mx/memory/reclamation/epoch_manager.cpp | 155 + .../lib/mx/memory/reclamation/epoch_manager.d | 641 + .../lib/mx/memory/reclamation/epoch_manager.h | 183 + .../src/lib/mx/memory/reclamation/epoch_t.h | 5 + repos/ealanos/src/lib/mx/memory/tagged_ptr.h | 103 + .../lib/mx/memory/task_allocator_interface.h | 70 + .../worker_local_dynamic_size_allocator.cpp | 433 + .../worker_local_dynamic_size_allocator.d | 521 + .../worker_local_dynamic_size_allocator.h | 345 + repos/ealanos/src/lib/mx/queue/bound_mpmc.h | 179 + .../src/lib/mx/queue/dynamic_ringpuffer.h | 39 + repos/ealanos/src/lib/mx/queue/list.h | 149 + repos/ealanos/src/lib/mx/queue/mpsc.h | 165 + .../ealanos/src/lib/mx/queue/priority_queue.h | 49 + .../ealanos/src/lib/mx/resource/annotation.h | 146 + repos/ealanos/src/lib/mx/resource/builder.cpp | 80 + repos/ealanos/src/lib/mx/resource/builder.d | 617 + repos/ealanos/src/lib/mx/resource/builder.h | 175 + repos/ealanos/src/lib/mx/resource/ptr.h | 99 + .../src/lib/mx/resource/resource_interface.h | 187 + .../mx/synchronization/memory_transaction.h | 81 + .../lib/mx/synchronization/optimistic_lock.h | 92 + .../lib/mx/synchronization/primitive_matrix.h | 68 + .../src/lib/mx/synchronization/rw_spinlock.h | 294 + .../src/lib/mx/synchronization/spinlock.h | 59 + .../lib/mx/synchronization/synchronization.h | 60 + repos/ealanos/src/lib/mx/system/builtin.h | 28 + repos/ealanos/src/lib/mx/system/cache.h | 203 + repos/ealanos/src/lib/mx/system/cpu.h | 51 + repos/ealanos/src/lib/mx/system/cpuid.h | 27 + repos/ealanos/src/lib/mx/system/environment.h | 38 + repos/ealanos/src/lib/mx/system/rdtscp.h | 35 + repos/ealanos/src/lib/mx/system/thread.h | 18 + repos/ealanos/src/lib/mx/tasking/README.md | 43 + repos/ealanos/src/lib/mx/tasking/annotation.h | 139 + repos/ealanos/src/lib/mx/tasking/config.h | 73 + .../src/lib/mx/tasking/dataflow/annotation.h | 97 + .../lib/mx/tasking/dataflow/barrier_task.h | 44 + .../mx/tasking/dataflow/finalize_counter.h | 40 + .../src/lib/mx/tasking/dataflow/graph.h | 994 + .../src/lib/mx/tasking/dataflow/node.h | 134 + .../src/lib/mx/tasking/dataflow/pipeline.h | 30 + .../src/lib/mx/tasking/dataflow/producer.h | 19 + .../src/lib/mx/tasking/dataflow/task_node.h | 149 + .../src/lib/mx/tasking/dataflow/token.h | 58 + .../lib/mx/tasking/dataflow/token_generator.h | 18 + repos/ealanos/src/lib/mx/tasking/load.h | 26 + .../src/lib/mx/tasking/prefetch_descriptor.h | 296 + .../src/lib/mx/tasking/prefetch_distance.h | 39 + .../src/lib/mx/tasking/prefetch_slot.cpp | 59 + .../src/lib/mx/tasking/prefetch_slot.d | 197 + .../src/lib/mx/tasking/prefetch_slot.h | 924 + repos/ealanos/src/lib/mx/tasking/priority.h | 11 + .../mx/tasking/profiling/idle_profiler.cpp | 110 + .../lib/mx/tasking/profiling/idle_profiler.d | 641 + .../lib/mx/tasking/profiling/idle_profiler.h | 71 + .../lib/mx/tasking/profiling/task_counter.h | 171 + .../lib/mx/tasking/profiling/task_tracer.cpp | 150 + .../lib/mx/tasking/profiling/task_tracer.d | 161 + .../lib/mx/tasking/profiling/task_tracer.h | 141 + .../src/lib/mx/tasking/profiling/time.cpp | 69 + .../src/lib/mx/tasking/profiling/time.d | 110 + .../src/lib/mx/tasking/profiling/time.h | 131 + repos/ealanos/src/lib/mx/tasking/runtime.h | 567 + .../ealanos/src/lib/mx/tasking/scheduler.cpp | 308 + repos/ealanos/src/lib/mx/tasking/scheduler.d | 641 + repos/ealanos/src/lib/mx/tasking/scheduler.h | 282 + .../src/lib/mx/tasking/shared_task_queue.h | 32 + repos/ealanos/src/lib/mx/tasking/task.cpp | 43 + repos/ealanos/src/lib/mx/tasking/task.d | 641 + repos/ealanos/src/lib/mx/tasking/task.h | 330 + .../ealanos/src/lib/mx/tasking/task_buffer.h | 342 + .../src/lib/mx/tasking/task_cycle_sampler.h | 127 + .../mx/tasking/task_execution_time_history.h | 92 + repos/ealanos/src/lib/mx/tasking/task_pool.h | 107 + .../src/lib/mx/tasking/task_pool_occupancy.h | 79 + .../ealanos/src/lib/mx/tasking/task_queues.h | 168 + .../ealanos/src/lib/mx/tasking/task_squad.cpp | 42 + repos/ealanos/src/lib/mx/tasking/task_squad.d | 641 + repos/ealanos/src/lib/mx/tasking/task_squad.h | 41 + repos/ealanos/src/lib/mx/tasking/task_stack.h | 162 + repos/ealanos/src/lib/mx/tasking/worker.cpp | 348 + repos/ealanos/src/lib/mx/tasking/worker.d | 644 + repos/ealanos/src/lib/mx/tasking/worker.h | 151 + repos/ealanos/src/lib/mx/util/aligned_t.h | 65 + repos/ealanos/src/lib/mx/util/core_set.cpp | 71 + repos/ealanos/src/lib/mx/util/core_set.d | 218 + repos/ealanos/src/lib/mx/util/core_set.h | 155 + repos/ealanos/src/lib/mx/util/logger.h | 91 + repos/ealanos/src/lib/mx/util/maybe_atomic.h | 14 + repos/ealanos/src/lib/mx/util/random.cpp | 40 + repos/ealanos/src/lib/mx/util/random.d | 17 + repos/ealanos/src/lib/mx/util/random.h | 33 + .../src/lib/mx/util/reference_counter.h | 109 + repos/ealanos/src/lib/mx/util/split_counter.h | 46 + repos/ealanos/src/lib/mx/util/vector.h | 174 + 118 files changed, 45165 insertions(+) create mode 100644 repos/ealanos/include/ealanos/util/ecpp/LICENSE create mode 100644 repos/ealanos/include/ealanos/util/ecpp/static_vector.h create mode 100644 repos/ealanos/include/ealanos/util/json.hpp create mode 100644 repos/ealanos/include/ealanos/util/tsl/LICENSE create mode 100644 repos/ealanos/include/ealanos/util/tsl/robin_growth_policy.h create mode 100644 repos/ealanos/include/ealanos/util/tsl/robin_hash.h create mode 100644 repos/ealanos/include/ealanos/util/tsl/robin_map.h create mode 100644 repos/ealanos/include/ealanos/util/tsl/robin_set.h create mode 100644 repos/ealanos/lib/mk/mxtasking.mk create mode 100644 repos/ealanos/src/lib/mx/LICENSE create mode 100644 repos/ealanos/src/lib/mx/README.md create mode 100644 repos/ealanos/src/lib/mx/io/network/config.h create mode 100644 repos/ealanos/src/lib/mx/io/network/server.cpp create mode 100644 repos/ealanos/src/lib/mx/io/network/server.d create mode 100644 repos/ealanos/src/lib/mx/io/network/server.h create mode 100644 repos/ealanos/src/lib/mx/memory/alignment_helper.h create mode 100644 repos/ealanos/src/lib/mx/memory/config.h create mode 100644 repos/ealanos/src/lib/mx/memory/dynamic_size_allocator.cpp create mode 100644 repos/ealanos/src/lib/mx/memory/dynamic_size_allocator.d create mode 100644 repos/ealanos/src/lib/mx/memory/dynamic_size_allocator.h create mode 100644 repos/ealanos/src/lib/mx/memory/fixed_size_allocator.h create mode 100644 repos/ealanos/src/lib/mx/memory/global_heap.h create mode 100644 repos/ealanos/src/lib/mx/memory/reclamation/epoch_manager.cpp create mode 100644 repos/ealanos/src/lib/mx/memory/reclamation/epoch_manager.d create mode 100644 repos/ealanos/src/lib/mx/memory/reclamation/epoch_manager.h create mode 100644 repos/ealanos/src/lib/mx/memory/reclamation/epoch_t.h create mode 100644 repos/ealanos/src/lib/mx/memory/tagged_ptr.h create mode 100644 repos/ealanos/src/lib/mx/memory/task_allocator_interface.h create mode 100644 repos/ealanos/src/lib/mx/memory/worker_local_dynamic_size_allocator.cpp create mode 100644 repos/ealanos/src/lib/mx/memory/worker_local_dynamic_size_allocator.d create mode 100644 repos/ealanos/src/lib/mx/memory/worker_local_dynamic_size_allocator.h create mode 100644 repos/ealanos/src/lib/mx/queue/bound_mpmc.h create mode 100644 repos/ealanos/src/lib/mx/queue/dynamic_ringpuffer.h create mode 100644 repos/ealanos/src/lib/mx/queue/list.h create mode 100644 repos/ealanos/src/lib/mx/queue/mpsc.h create mode 100644 repos/ealanos/src/lib/mx/queue/priority_queue.h create mode 100644 repos/ealanos/src/lib/mx/resource/annotation.h create mode 100644 repos/ealanos/src/lib/mx/resource/builder.cpp create mode 100644 repos/ealanos/src/lib/mx/resource/builder.d create mode 100644 repos/ealanos/src/lib/mx/resource/builder.h create mode 100644 repos/ealanos/src/lib/mx/resource/ptr.h create mode 100644 repos/ealanos/src/lib/mx/resource/resource_interface.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/memory_transaction.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/optimistic_lock.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/primitive_matrix.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/rw_spinlock.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/spinlock.h create mode 100644 repos/ealanos/src/lib/mx/synchronization/synchronization.h create mode 100644 repos/ealanos/src/lib/mx/system/builtin.h create mode 100644 repos/ealanos/src/lib/mx/system/cache.h create mode 100644 repos/ealanos/src/lib/mx/system/cpu.h create mode 100644 repos/ealanos/src/lib/mx/system/cpuid.h create mode 100644 repos/ealanos/src/lib/mx/system/environment.h create mode 100644 repos/ealanos/src/lib/mx/system/rdtscp.h create mode 100644 repos/ealanos/src/lib/mx/system/thread.h create mode 100644 repos/ealanos/src/lib/mx/tasking/README.md create mode 100644 repos/ealanos/src/lib/mx/tasking/annotation.h create mode 100644 repos/ealanos/src/lib/mx/tasking/config.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/annotation.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/barrier_task.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/finalize_counter.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/graph.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/node.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/pipeline.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/producer.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/task_node.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/token.h create mode 100644 repos/ealanos/src/lib/mx/tasking/dataflow/token_generator.h create mode 100644 repos/ealanos/src/lib/mx/tasking/load.h create mode 100644 repos/ealanos/src/lib/mx/tasking/prefetch_descriptor.h create mode 100644 repos/ealanos/src/lib/mx/tasking/prefetch_distance.h create mode 100644 repos/ealanos/src/lib/mx/tasking/prefetch_slot.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/prefetch_slot.d create mode 100644 repos/ealanos/src/lib/mx/tasking/prefetch_slot.h create mode 100644 repos/ealanos/src/lib/mx/tasking/priority.h create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/idle_profiler.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/idle_profiler.d create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/idle_profiler.h create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/task_counter.h create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/task_tracer.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/task_tracer.d create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/task_tracer.h create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/time.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/time.d create mode 100644 repos/ealanos/src/lib/mx/tasking/profiling/time.h create mode 100644 repos/ealanos/src/lib/mx/tasking/runtime.h create mode 100644 repos/ealanos/src/lib/mx/tasking/scheduler.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/scheduler.d create mode 100644 repos/ealanos/src/lib/mx/tasking/scheduler.h create mode 100644 repos/ealanos/src/lib/mx/tasking/shared_task_queue.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/task.d create mode 100644 repos/ealanos/src/lib/mx/tasking/task.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_buffer.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_cycle_sampler.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_execution_time_history.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_pool.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_pool_occupancy.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_queues.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_squad.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/task_squad.d create mode 100644 repos/ealanos/src/lib/mx/tasking/task_squad.h create mode 100644 repos/ealanos/src/lib/mx/tasking/task_stack.h create mode 100644 repos/ealanos/src/lib/mx/tasking/worker.cpp create mode 100644 repos/ealanos/src/lib/mx/tasking/worker.d create mode 100644 repos/ealanos/src/lib/mx/tasking/worker.h create mode 100644 repos/ealanos/src/lib/mx/util/aligned_t.h create mode 100644 repos/ealanos/src/lib/mx/util/core_set.cpp create mode 100644 repos/ealanos/src/lib/mx/util/core_set.d create mode 100644 repos/ealanos/src/lib/mx/util/core_set.h create mode 100644 repos/ealanos/src/lib/mx/util/logger.h create mode 100644 repos/ealanos/src/lib/mx/util/maybe_atomic.h create mode 100644 repos/ealanos/src/lib/mx/util/random.cpp create mode 100644 repos/ealanos/src/lib/mx/util/random.d create mode 100644 repos/ealanos/src/lib/mx/util/random.h create mode 100644 repos/ealanos/src/lib/mx/util/reference_counter.h create mode 100644 repos/ealanos/src/lib/mx/util/split_counter.h create mode 100644 repos/ealanos/src/lib/mx/util/vector.h diff --git a/repos/ealanos/include/ealanos/util/ecpp/LICENSE b/repos/ealanos/include/ealanos/util/ecpp/LICENSE new file mode 100644 index 0000000000..1fce350f63 --- /dev/null +++ b/repos/ealanos/include/ealanos/util/ecpp/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Jan Macheta + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/repos/ealanos/include/ealanos/util/ecpp/static_vector.h b/repos/ealanos/include/ealanos/util/ecpp/static_vector.h new file mode 100644 index 0000000000..dad349c4f0 --- /dev/null +++ b/repos/ealanos/include/ealanos/util/ecpp/static_vector.h @@ -0,0 +1,960 @@ +#ifndef STATIC_VECTOR_HPP_ +#define STATIC_VECTOR_HPP_ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cpp_exceptions + #include + #define ECPP_STATIC_VECTOR_THROW(x) throw(x) +#else + #define ECPP_STATIC_VECTOR_THROW(x) std::abort(); +#endif + +namespace ecpp +{ + + /** + * @brief Implementation of standard vector with statically allocated pool + * The container uses underlying array to allocate required capacity. + * For detailed documentation see https://en.cppreference.com/w/cpp/container/vector + * The documentation below highlights only the differences. + */ + template class static_vector + { + public: + + using value_type = T; ///< The type of the elements + using storage_container_type = + std::array, + N>; ///< Type of underlying continuous static storage + using size_type = typename storage_container_type::size_type; ///< Unsigned integer type + ///< (usually std::size_t) + + using pointer = T *; ///< Pointer to element type + using const_pointer = T const *; ///< Const pointer to element type + + using reference = T &; ///< Reference to element type + using const_reference = T const &; ///< Const reference to element type + + using iterator = pointer; ///< Iterator to value_type type that meets + ///< LegacyRandomAccessIterator requirements + using const_iterator = const_pointer; ///< Iterator to const value_type type that meets + ///< LegacyRandomAccessIterator requirements + + using reverse_iterator = std::reverse_iterator; ///< Reverse iterator type + using const_reverse_iterator = + std::reverse_iterator; ///< Reverse const iterator type + + using difference_type = decltype(std::distance( + std::declval(), + std::declval())); ///< Signed integer type (usually std::ptrdiff_t) + + private: + + storage_container_type container; ///< element storage + size_type currentSize{0}; ///< current element count + + public: + + /// Default constructor. Constructs an empty container with a default-constructed + /// allocator + constexpr static_vector() noexcept = default; + + /** + * @brief Constructs the container with count copies of elements with value value + * @param[in] count the size of the container + * @param[in] value the value to initialize elements of the container with + * @note The method will throw LengthError if count > max_size() + */ + constexpr static_vector(size_type count, const_reference value) : currentSize(count) + { + if (count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + std::uninitialized_fill_n(begin(), count, value); + } + + /** + * @brief Constructs the container with count default-inserted instances of T. No copies + * are made + * @param[in] count the size of the container + * @note The method will throw LengthError if count > max_size() + */ + constexpr explicit static_vector(size_type count) + requires(std::is_default_constructible_v) + : currentSize(count) + { + if (count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + std::uninitialized_default_construct_n(begin(), count); + } + + /** + * @brief Constructs the container with the contents of the range [first, last) + * @param[in] first start of the range to copy the elements from + * @param[in] last end of the range to copy the elements from + * @note The method will throw LengthError if std::distance(first, last) is negative or, + * greater than max_size() + */ + template constexpr static_vector(InputIt first, InputIt last) + { + auto dist = std::distance(first, last); + auto count = size_type(dist); + if (dist < 0 || count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + currentSize = count; + std::uninitialized_copy(first, last, begin()); + } + + /** + * @brief Copy constructor. Constructs the container with the copy of the contents of + * other + * @param[in] other another container to be used as source to initialize the elements of + * the container with + */ + constexpr static_vector(static_vector const &other) : currentSize(other.currentSize) + { + std::uninitialized_copy(other.begin(), other.end(), begin()); + } + + /** + * @brief Move constructor. Constructs the container with the contents of other using + * move semantics. After the move, other is guaranteed to be empty() + * @param[in] other another container to be used as source to initialize the elements of + * the container with + */ + constexpr static_vector(static_vector &&other) noexcept + requires(std::movable) + : currentSize(std::move(other.currentSize)) + { + std::uninitialized_move(other.begin(), other.end(), begin()); + } + + /** + * @brief Constructs the container with the contents of the initializer list init + * @param[in] init initializer list to initialize the elements of the container with + * @note The method will throw LengthError if init.size() > max_size() + */ + constexpr static_vector(std::initializer_list init) + : static_vector(init.begin(), init.end()) + { + } + + /** + * @brief Destructs the vector. The destructors of the elements are called and the used + * storage is deallocated. + * @note if the elements are pointers, the pointed-to objects are not destroyed. + */ + constexpr ~static_vector() noexcept + { + if constexpr (!std::is_trivially_destructible_v) { clear(); } + } + + /** + * @brief Copy assignment operator. Replaces the contents with a copy of the contents of + * other + * @param[in] other another container to use as data source + * @return *this + */ + constexpr static_vector &operator=(static_vector const &other) + { + if (&other != this) { + if (!empty()) { clear(); } + currentSize = other.currentSize; + std::uninitialized_copy(other.begin(), other.end(), begin()); + } + return *this; + } + + /** + * @brief Move assignment operator. Replaces the contents with those of other using move + * semantics (i.e. the data in other is moved from other into this container). other is + * in a valid but unspecified state afterwards + * @param[in] other another container to use as data source + * @return *this + */ + constexpr static_vector &operator=(static_vector &&other) + { + if (&other != this) { + if (!empty()) { clear(); } + currentSize = std::move(other.currentSize); + std::uninitialized_move(other.begin(), other.end(), begin()); + } + return *this; + } + + /** + * @brief Replaces the contents with those identified by initializer list ilist + * @param[in] ilist initializer list to use as data source + * @return *this + * @note The method will throw LengthError if ilist.size() > max_size() + */ + constexpr static_vector &operator=(std::initializer_list ilist) + { + if (!empty()) { clear(); } + if (ilist.size() > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + currentSize = ilist.size(); + std::uninitialized_copy(ilist.begin(), ilist.end(), begin()); + return *this; + } + + /** + * @brief Replaces the contents with count copies of value value + * @param[in] count the new size of the container + * @param[in] value the value to initialize elements of the container with + * @note The method will throw LengthError if count > max_size() + */ + constexpr void assign(size_type count, T const &value) + { + if (!empty()) { clear(); } + if (count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + std::uninitialized_fill_n(begin(), count, value); + currentSize = count; + } + + /** + * @brief Replaces the contents with copies of those in the range [first, last) + * @param[in] first start of the range to copy the elements from + * @param[in] last end of the range to copy the elements from + * @note The behavior is undefined if either argument is an iterator into *this. + * @note The method will throw LengthError if std::distance(first, last) is negative or, + * greater than max_size() + */ + template constexpr void assign(InputIt first, InputIt last) + { + if (!empty()) { clear(); } + + auto dist = std::distance(first, last); + auto count = size_type(dist); + if (dist < 0 || count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + currentSize = count; + std::uninitialized_copy(first, last, begin()); + } + + /** + * @brief Replaces the contents with the elements from the initializer list ilist + * @param[in] ilist initializer list to copy the values from + * @note The method will throw LengthError if ilist.size() > max_size() + */ + constexpr void assign(std::initializer_list ilist) + { + assign(ilist.begin(), ilist.end()); + } + + /** + * @brief Returns a reference to the element at specified location pos, with bounds + * checking. + * @param[in] pos position of the element to return + * @return Reference to the requested element + * @note If pos is not within the range of the container, an OutOfRangeError is thrown. + */ + constexpr reference at(size_type pos) + { + if (pos >= size()) { + ECPP_STATIC_VECTOR_THROW(std::out_of_range("Index out of bounds")); + } + return (*this)[pos]; + } + + /** + * @brief Returns a reference to the element at specified location pos, with bounds + * checking. + * @param[in] pos position of the element to return + * @return Reference to the requested element + * @note If pos is not within the range of the container, an OutOfRangeError is thrown. + */ + constexpr const_reference at(size_type pos) const + { + if (pos >= size()) { + ECPP_STATIC_VECTOR_THROW(std::out_of_range("Index out of bounds")); + } + return (*this)[pos]; + } + + /** + * @brief Returns a reference to the element at specified location pos. No bounds + * checking is performed + * @param[in] pos position of the element to return + * @return Reference to the requested element + * @note Accessing a nonexistent element through this operator is undefined behavior + */ + constexpr reference operator[](size_type pos) noexcept { return *(begin() + pos); } + + /** + * @brief Returns a reference to the element at specified location pos. No bounds + * checking is performed + * @param[in] pos position of the element to return + * @return Reference to the requested element + * @note Accessing a nonexistent element through this operator is undefined behavior + */ + constexpr const_reference operator[](size_type pos) const noexcept + { + return *(begin() + pos); + } + + /** + * @brief Returns a reference to the first element in the container + * @return Reference to the first element + * @note Calling front on an empty container is undefined + */ + constexpr reference front() noexcept { return *begin(); } + + /** + * @brief Returns a reference to the first element in the container + * @return Reference to the first element + * @note Calling front on an empty container is undefined + */ + constexpr const_reference front() const noexcept { return *begin(); } + + /** + * @brief Returns a reference to the last element in the container + * @return Reference to the last element + * @note Calling back on an empty container is undefined + */ + constexpr reference back() noexcept { return *(end() - 1); } + + /** + * @brief Returns a reference to the last element in the container + * @return Reference to the last element + * @note Calling back on an empty container is undefined + */ + constexpr const_reference back() const noexcept { return *(end() - 1); } + + /** + * @brief Returns pointer to the underlying array serving as element storage + * @return Pointer to the underlying element storage. For non-empty containers, the + * returned pointer compares equal to the address of the first element + * @note The pointer is such that range [data(); data() + size()) is always a valid + * range, even if the container is empty (data() is not dereferenceable in that case) + */ + constexpr pointer data() noexcept + { + return std::launder(reinterpret_cast(container.begin())); + } + + /** + * @brief Returns pointer to the underlying array serving as element storage + * @return Pointer to the underlying element storage. For non-empty containers, the + * returned pointer compares equal to the address of the first element + * @note The pointer is such that range [data(); data() + size()) is always a valid + * range, even if the container is empty (data() is not dereferenceable in that case) + */ + constexpr const_pointer data() const noexcept + { + return std::launder(reinterpret_cast(container.begin())); + } + + /** + * @brief Returns an iterator to the first element of the vector. + * If the vector is empty, the returned iterator will be equal to end() + * @return Iterator to the first element + */ + constexpr iterator begin() noexcept { return data(); } + + /** + * @brief Returns an iterator to the first element of the vector. + * If the vector is empty, the returned iterator will be equal to end() + * @return Iterator to the first element + */ + constexpr const_iterator begin() const noexcept { return data(); } + + /** + * @brief Returns an iterator to the first element of the vector. + * If the vector is empty, the returned iterator will be equal to end() + * @return Iterator to the first element + */ + constexpr const_iterator cbegin() const noexcept { return data(); } + + /** + * @brief Returns an iterator to the element following the last element of the vector + * @return Iterator to the element following the last element + * @note This element acts as a placeholder; attempting to access it results in + * undefined behavior. + */ + constexpr iterator end() noexcept { return begin() + currentSize; } + + /** + * @brief Returns an iterator to the element following the last element of the vector + * @return Iterator to the element following the last element + * @note This element acts as a placeholder; attempting to access it results in + * undefined behavior. + */ + constexpr const_iterator end() const noexcept { return begin() + currentSize; } + + /** + * @brief Returns an iterator to the element following the last element of the vector + * @return Iterator to the element following the last element + * @note This element acts as a placeholder; attempting to access it results in + * undefined behavior. + */ + constexpr const_iterator cend() const noexcept { return begin() + currentSize; } + + /** + * @brief Returns a reverse iterator to the first element of the reversed vector. + * It corresponds to the last element of the non-reversed vector. If the vector is + * empty, the returned iterator is equal to rend(). + * @return Reverse iterator to the first element + */ + constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + + /** + * @brief Returns a reverse iterator to the first element of the reversed vector. + * It corresponds to the last element of the non-reversed vector. If the vector is + * empty, the returned iterator is equal to rend(). + * @return Reverse iterator to the first element + */ + constexpr const_reverse_iterator rbegin() const noexcept + { + return const_reverse_iterator(end()); + } + + /** + * @brief Returns a reverse iterator to the first element of the reversed vector. + * It corresponds to the last element of the non-reversed vector. If the vector is + * empty, the returned iterator is equal to rend(). + * @return Reverse iterator to the first element + */ + constexpr const_reverse_iterator crbegin() const noexcept + { + return const_reverse_iterator(cend()); + } + + /** + * @brief Returns a reverse iterator to the element following the last element of the + * reversed vector. It corresponds to the element preceding the first element of the + * non-reversed vector + * @return Reverse iterator to the element following the last element + * @note This element acts as a placeholder, attempting to access it results in + * undefined behavior + */ + constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + + /** + * @brief Returns a reverse iterator to the element following the last element of the + * reversed vector. It corresponds to the element preceding the first element of the + * non-reversed vector + * @return Reverse iterator to the element following the last element + * @note This element acts as a placeholder, attempting to access it results in + * undefined behavior + */ + constexpr const_reverse_iterator rend() const noexcept + { + return const_reverse_iterator(begin()); + } + + /** + * @brief Returns a reverse iterator to the element following the last element of the + * reversed vector. It corresponds to the element preceding the first element of the + * non-reversed vector + * @return Reverse iterator to the element following the last element + * @note This element acts as a placeholder, attempting to access it results in + * undefined behavior + */ + constexpr const_reverse_iterator crend() const noexcept + { + return const_reverse_iterator(begin()); + } + + /** + * @brief Checks if the container has no elements, i.e. whether begin() == end() + * @return true if the container is empty, false otherwise + */ + constexpr bool empty() const noexcept { return 0 == size(); } + + /** + * @brief Returns the number of elements in the container, i.e. std::distance(begin(), + * end()) + * @return The number of elements in the container + */ + constexpr size_type size() const noexcept { return currentSize; } + + /** + * @brief Returns the maximum number of elements the container is able to hold due to + * system or library implementation limitations i.e. N + * @return Maximum number of elements + */ + constexpr size_type max_size() const noexcept { return container.max_size(); } + + /** + * @brief Increase the capacity of the vector to a value that's greater or equal to + * new_cap. Because the static_vector uses static storage, when new_cap <= max_size() + * the method does nothing. + * @param[in] new_cap new capacity of the vector + * @note For compatibility purposes, The method throws LengthError when new_cap > + * max_size + */ + constexpr void reserve(size_type new_cap) + { + if (new_cap > max_size()) { + ECPP_STATIC_VECTOR_THROW(std::length_error("Reserve exceeds max_size")); + } + } + + /** + * @brief Returns the number of elements that the container has currently allocated + * space for + * @return Capacity of the currently allocated storage + */ + constexpr size_type capacity() const noexcept { return max_size(); } + + /** + * @brief Requests the removal of unused capacity. + * Because the static_vector uses static storage this method does nothing + */ + constexpr void shrink_to_fit() noexcept + { /* Do nothing, as the underlying storage cannot be shrinked */ } + + /** + * @brief Erases all elements from the container. After this call, size() returns zero. + * Invalidates any references, pointers, or iterators referring to contained elements. + * Any past-the-end iterators are also invalidated. Leaves the capacity() of the vector + * unchanged + */ + constexpr void clear() noexcept + { + std::for_each(begin(), end(), [](reference x) { std::destroy_at(&x); }); + currentSize = 0; + } + + /** + * @brief Inserts value before pos + * @param[in] pos iterator before which the content will be inserted. pos may be the + * end() iterator + * @param[in] value element value to insert + * @return Iterator pointing to the inserted value + * @note The method will throw LengthError if size() == max_size() + */ + constexpr iterator insert(const_iterator pos, T const &value) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto position = begin() + std::distance(cbegin(), pos); + // Move last element right by one (end() + 1 will become new end(), so uninitialized + // memory need to be initialized) + if (position != end()) { + std::uninitialized_move(end() - 1, end(), end()); + // Move [pos, end() -1) to [pos + 1, end()) + std::move_backward(position, end() - 1, end()); + } + std::construct_at(position, value); + ++currentSize; + return position; + } + + /** + * @brief Inserts value before pos + * @param[in] pos iterator before which the content will be inserted. pos may be the + * end() iterator + * @param[in] value element value to insert + * @return Iterator pointing to the inserted value + * @note The method will throw LengthError if size() == max_size() + */ + constexpr iterator insert(const_iterator pos, T &&value) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto position = begin() + std::distance(cbegin(), pos); + // Move last element right by one (end() + 1 will become new end(), so uninitialized + // memory need to be initialized) + if (position != end()) { + std::uninitialized_move(end() - 1, end(), end()); + // Move [pos, end() -1) to [pos + 1, end()) + std::move_backward(position, end() - 1, end()); + } + std::construct_at(position, std::move(value)); + ++currentSize; + return position; + } + + /** + * @brief Inserts count copies of the value before pos + * @param[in] pos iterator before which the content will be inserted. pos may be the + * end() iterator + * @param[in] count number of copies to be inserted + * @param[in] value element value to insert + * @return Iterator pointing to the first element inserted, or pos if count==0 + * @note The method will throw LengthError if size() + count > max_size() + */ + constexpr iterator insert(const_iterator pos, size_type count, T const &value) + { + if (size() + count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto position = begin() + std::distance(cbegin(), pos); + if (count > 0) { + if (position != end()) { + auto existingElementsToMove = std::distance( + position, end()); // Negative distance in this context is UB (position + // must be in range [begin(), end()]) + auto toCopyAtTheEnd = (count >= existingElementsToMove) + ? (count - existingElementsToMove) + : 0; + auto toMoveAssign = (count >= existingElementsToMove) + ? 0 + : (existingElementsToMove - count); + + // uninitialized_copy last toCopyAtTheEnd elements of input range at the + // end(), as they don't overlap with existing data + auto lastElem = std::uninitialized_fill_n(end(), toCopyAtTheEnd, value); + + // Move data from [pos end()) after last element of the vector. If size of + // the input range is smaller than number of elements to move + std::uninitialized_move(position + toMoveAssign, end(), lastElem); + + std::move_backward(position, position + toMoveAssign, end()); + + std::fill(position, position + count - toCopyAtTheEnd, value); + + } else { + std::uninitialized_fill_n(position, count, value); + } + currentSize += count; + } + return position; + } + + /** + * @brief Inserts elements from range [first, last) before pos + * @param[in] pos iterator before which the content will be inserted. pos may be the + * end() iterator + * @param[in] first start of the range of elements to insert, can't be iterators into + * container for which insert is called + * @param[in] last end of the range of elements to insert, can't be iterators into + * container for which insert is called + * @return Iterator pointing to the first element inserted, or pos if first==last + * @note The method will throw LengthError if size() + count > max_size() + */ + template + constexpr iterator insert(const_iterator pos, InputIt first, InputIt last) + { + auto dist = std::distance(first, last); + auto count = size_type(dist); + if (dist < 0 || (size() + count > max_size())) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto position = begin() + std::distance(cbegin(), pos); + if (position != end()) { + auto existingElementsToMove = std::distance( + position, end()); // Negative distance in this context is UB (position must + // be in range [begin(), end()]) + auto toCopyAtTheEnd = + (dist >= existingElementsToMove) ? (dist - existingElementsToMove) : 0; + auto toMoveAssign = + (dist >= existingElementsToMove) ? 0 : (existingElementsToMove - dist); + + // uninitialized_copy last toCopyAtTheEnd elements of input range at the end(), + // as they don't overlap with existing data + auto lastElem = std::uninitialized_copy(last - toCopyAtTheEnd, last, end()); + + // Move data from [pos end()) after last element of the vector. If size of the + // input range is smaller than number of elements to move + // + std::uninitialized_move(position + toMoveAssign, end(), lastElem); + + std::move_backward(position, position + toMoveAssign, end()); + + std::copy(first, last - toCopyAtTheEnd, position); + } else { + std::uninitialized_copy(first, last, end()); + } + currentSize += count; + return position; + } + + /** + * @brief Inserts elements from initializer list ilist before pos + * @param[in] pos iterator before which the content will be inserted. pos may be the + * end() iterator + * @param[in] ilist initializer list to insert the values from + * @return Iterator pointing to the first element inserted, or pos if ilist is empty + * @note The method will throw LengthError if size() + ilist.size() > max_size() + */ + constexpr iterator insert(const_iterator pos, std::initializer_list ilist) + { + return insert(pos, ilist.begin(), ilist.end()); + } + + /** + * @brief Inserts a new element into the container directly before pos + * @param pos iterator before which the new element will be constructed + * @param args arguments to forward to the constructor of the element + * @return Iterator pointing to the emplaced element + * @note The method will throw LengthError if size() == max_size() + */ + template constexpr iterator emplace(const_iterator pos, Args &&...args) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto const position = begin() + std::distance(cbegin(), pos); + // Move last element right by one (end() + 1 will become new end(), so uninitialized + // memory need to be initialized) + if (position != end()) { + std::uninitialized_move(end() - 1, end(), end()); + // Move [pos, end() -1) to [pos + 1, end()) + std::move_backward(position, end() - 1, end()); + } else { + std::construct_at(position, std::forward(args)...); + } + ++currentSize; + return position; + } + + /** + * @brief Removes the element at pos + * @param[in] pos to the element to remove + * @return Iterator following the last removed element + * @note If pos refers to the last element, then the end() iterator is returned + */ + constexpr iterator erase(const_iterator pos) + { + auto index = std::distance(cbegin(), pos); + std::move(begin() + index + 1, end(), begin() + index); + // Elements were moved left, now destroy the last element + currentSize--; + // Now, end() points to previous last element + std::destroy_at(end()); + return begin() + index; + } + + /** + * @brief Removes the elements in the range [first, last) + * @param[in] first start of range of elements to remove + * @param[in] last end of range of elements to remove + * @return Iterator following the last removed element + * @note If last==end() prior to removal, then the updated end() iterator is returned. + * @note If [first, last) is an empty range, then last is returned + */ + constexpr iterator erase(const_iterator first, const_iterator last) + { + auto last_ = begin() + std::distance(cbegin(), last); + if (first >= last) return last_; + auto first_ = begin() + std::distance(cbegin(), first); + + auto toErase = std::distance(first, last); // Guaranteed to be > 0 + auto lastValid = std::move(first_ + toErase, end(), first_); + std::for_each(lastValid, end(), [](reference x) { std::destroy_at(&x); }); + + currentSize -= size_type(toErase); + return first_; + } + + /** + * @brief Appends the given element value to the end of the container. The new element + * is initialized as a copy of value + * @param[in] value the value of the element to append + * @note The method will throw LengthError if size() == max_size() + */ + constexpr void push_back(T const &value) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + std::construct_at(end(), value); + ++currentSize; + } + + /** + * @brief Appends the given element value to the end of the container; value is moved + * into the new element + * @param[in] value the value of the element to append + * @note The method will throw LengthError if size() == max_size() + */ + constexpr void push_back(T &&value) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + std::construct_at(end(), std::move(value)); + ++currentSize; + } + + /** + * @brief Appends a new element to the end of the container. The element is constructed + * in-place. + * @param[in] args arguments to forward to the constructor of the element + * @return A reference to the inserted element + * @note The method will throw LengthError if size() == max_size() + */ + template constexpr reference emplace_back(Args &&...args) + { + if (size() + 1 > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } + auto const position = end(); + std::construct_at(position, std::forward(args)...); + ++currentSize; + return *position; + } + + /** + * @brief Removes the last element of the container + * Iterators and references to the last element, as well as the end() iterator, are + * invalidated + * @note Calling pop_back on an empty container results in undefined behavior. + */ + constexpr void pop_back() + { + currentSize--; + std::destroy_at(end()); + } + + /** + * @brief Resizes the container to contain count elements + * If the current size is greater than count, the container is reduced to its first + * count elements. If the current size is less than count, additional default-inserted + * elements are appended + * @param[in] count new size of the container + * @note The method will throw LengthError if count > max_size() + */ + constexpr void resize(size_type count) + { + if (count < size()) { + erase(begin() + count, end()); + } else if (count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } else { + auto toAdd = count - size(); + for (size_type i = 0; i != toAdd; ++i) { emplace_back(); } + } + } + + /** + * @brief Resizes the container to contain count elements + * If the current size is greater than count, the container is reduced to its first + * count elements. If the current size is less than count, additional copies of value + * are appended. + * @param[in] count new size of the container + * @param[in] value the value to initialize the new elements with + * @note The method will throw LengthError if count > max_size() + */ + constexpr void resize(size_type count, value_type const &value) + { + if (count < size()) { + erase(begin() + count, end()); + } else if (count > max_size()) { + ECPP_STATIC_VECTOR_THROW( + std::length_error("Insertion would exceed static_vector capacity")); + } else { + auto toAdd = count - size(); + for (size_type i = 0; i != toAdd; ++i) { push_back(value); } + } + } + + /** + * Exchanges the contents of the container with those of other. Swaps content of + * underlying storage. All iterators and references are invalidated + * @param[in] other reference to static_vector instance to swap with + */ + constexpr void swap(static_vector &other) noexcept + { + container.swap(other.container); + std::swap(currentSize, other.currentSize); + } + }; + + /** + * @brief Checks if the contents of lhs and rhs are equal, that is, they have the same number of + * elements and each element in lhs compares equal with the element in rhs at the same position + * @param[in] lhs first of vectors whose contents to compare + * @param[in] rhs second of vectors whose contents to compare + * @return true if the contents of the vectors are equal, false otherwise + */ + template + constexpr bool operator==(static_vector const &lhs, static_vector const &rhs) + { + return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); + } + + /** + * @brief Compares the contents of lhs and rhs lexicographically. + * The comparison is performed as if by calling std::lexicographical_compare_three_way on two + * vectors with a function object performing synthesized three-way comparison. The return type + * is same as the result type of synthesized three-way comparison + * @param[in] lhs first of vectors whose contents to compare + * @param[in] rhs second of vectors whose contents to compare + * @return The relative order of the first pair of non-equivalent elements in lhs and rhs if + * there are such elements, lhs.size() <=> rhs.size() otherwise + */ + template + constexpr auto operator<=>(static_vector const &lhs, static_vector const &rhs) + { + return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), + rhs.end()); + } + + /** + * @brief Specializes the std::swap algorithm for static_vector. + * Swaps the contents of lhs and rhs. Calls lhs.swap(rhs). + * @param[in,out] lhs container whose contents to swap + * @param[in,out] rhs containers whose contents to swap + */ + template + constexpr void swap(static_vector &lhs, static_vector &rhs) noexcept + { + lhs.swap(rhs); + } + + /** + * @brief Erases all elements that compare equal to value from the container + * @param[in, out] c container from which to erase + * @param[in] value value to be removed + * @return The number of erased elements + */ + template + constexpr typename static_vector::size_type erase(static_vector &c, U const &value) + { + auto oldSize = c.size(); + auto end = std::remove(c.begin(), c.end(), value); + c.erase(end, c.end()); + return c.size() - oldSize; + } + + /** + * @brief Erases all elements that compare equal to value from the container + * @param[in, out] c container from which to erase + * @param[in] pred unary predicate which returns ​true if the element should be erased + * @return The number of erased elements + */ + template + constexpr typename static_vector::size_type erase_if(static_vector &c, Pred pred) + { + auto oldSize = c.size(); + auto end = std::remove_if(c.begin(), c.end(), pred); + c.erase(end, c.end()); + return c.size() - oldSize; + } +} // namespace ecpp + +#endif \ No newline at end of file diff --git a/repos/ealanos/include/ealanos/util/json.hpp b/repos/ealanos/include/ealanos/util/json.hpp new file mode 100644 index 0000000000..5003a4fa2d --- /dev/null +++ b/repos/ealanos/include/ealanos/util/json.hpp @@ -0,0 +1,20842 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ +| | |__ | | | | | | version 3.6.1 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2013-2019 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef INCLUDE_NLOHMANN_JSON_HPP_ +#define INCLUDE_NLOHMANN_JSON_HPP_ + +#define NLOHMANN_JSON_VERSION_MAJOR 3 +#define NLOHMANN_JSON_VERSION_MINOR 6 +#define NLOHMANN_JSON_VERSION_PATCH 1 + +#include // all_of, find, for_each +#include // assert +#include // and, not, or +#include // nullptr_t, ptrdiff_t, size_t +#include // hash, less +#include // initializer_list +#include // istream, ostream +#include // random_access_iterator_tag +#include // unique_ptr +#include // accumulate +#include // string, stoi, to_string +#include // declval, forward, move, pair, swap +#include // vector + +// #include + + +#include + +// #include + + +#include // transform +#include // array +#include // and, not +#include // forward_list +#include // inserter, front_inserter, end +#include // map +#include // string +#include // tuple, make_tuple +#include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible +#include // unordered_map +#include // pair, declval +#include // valarray + +// #include + + +#include // exception +#include // runtime_error +#include // to_string + +// #include + + +#include // size_t + +namespace nlohmann +{ +namespace detail +{ +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} // namespace detail +} // namespace nlohmann + + +namespace nlohmann +{ +namespace detail +{ +//////////////// +// exceptions // +//////////////// + +/*! +@brief general exception of the @ref basic_json class + +This class is an extension of `std::exception` objects with a member @a id for +exception ids. It is used as the base class for all exceptions thrown by the +@ref basic_json class. This class can hence be used as "wildcard" to catch +exceptions. + +Subclasses: +- @ref parse_error for exceptions indicating a parse error +- @ref invalid_iterator for exceptions indicating errors with iterators +- @ref type_error for exceptions indicating executing a member function with + a wrong type +- @ref out_of_range for exceptions indicating access out of the defined range +- @ref other_error for exceptions indicating other library errors + +@internal +@note To have nothrow-copy-constructible exceptions, we internally use + `std::runtime_error` which can cope with arbitrary-length error messages. + Intermediate strings are built with static functions and then passed to + the actual constructor. +@endinternal + +@liveexample{The following code shows how arbitrary library exceptions can be +caught.,exception} + +@since version 3.0.0 +*/ +class exception : public std::exception +{ + public: + /// returns the explanatory string + const char* what() const noexcept override + { + return m.what(); + } + + /// the id of the exception + const int id; + + protected: + exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} + + static std::string name(const std::string& ename, int id_) + { + return "[json.exception." + ename + "." + std::to_string(id_) + "] "; + } + + private: + /// an exception object as storage for error messages + std::runtime_error m; +}; + +/*! +@brief exception indicating a parse error + +This exception is thrown by the library when a parse error occurs. Parse errors +can occur during the deserialization of JSON text, CBOR, MessagePack, as well +as when using JSON Patch. + +Member @a byte holds the byte index of the last read character in the input +file. + +Exceptions have ids 1xx. + +name / id | example message | description +------------------------------ | --------------- | ------------------------- +json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position. +json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. +json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. +json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. +json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. +json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`. +json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. +json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. +json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number. +json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. +json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read. +json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read. +json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet). + +@note For an input with n bytes, 1 is the index of the first character and n+1 + is the index of the terminating null byte or the end of file. This also + holds true when reading a byte vector (CBOR or MessagePack). + +@liveexample{The following code shows how a `parse_error` exception can be +caught.,parse_error} + +@sa - @ref exception for the base class of the library exceptions +@sa - @ref invalid_iterator for exceptions indicating errors with iterators +@sa - @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa - @ref out_of_range for exceptions indicating access out of the defined range +@sa - @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class parse_error : public exception +{ + public: + /*! + @brief create a parse error exception + @param[in] id_ the id of the exception + @param[in] pos the position where the error occurred (or with + chars_read_total=0 if the position cannot be + determined) + @param[in] what_arg the explanatory string + @return parse_error object + */ + static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + { + std::string w = exception::name("parse_error", id_) + "parse error" + + position_string(pos) + ": " + what_arg; + return parse_error(id_, pos.chars_read_total, w.c_str()); + } + + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + { + std::string w = exception::name("parse_error", id_) + "parse error" + + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + + ": " + what_arg; + return parse_error(id_, byte_, w.c_str()); + } + + /*! + @brief byte index of the parse error + + The byte index of the last read character in the input file. + + @note For an input with n bytes, 1 is the index of the first character and + n+1 is the index of the terminating null byte or the end of file. + This also holds true when reading a byte vector (CBOR or MessagePack). + */ + const std::size_t byte; + + private: + parse_error(int id_, std::size_t byte_, const char* what_arg) + : exception(id_, what_arg), byte(byte_) {} + + static std::string position_string(const position_t& pos) + { + return " at line " + std::to_string(pos.lines_read + 1) + + ", column " + std::to_string(pos.chars_read_current_line); + } +}; + +/*! +@brief exception indicating errors with iterators + +This exception is thrown if iterators passed to a library function do not match +the expected semantics. + +Exceptions have ids 2xx. + +name / id | example message | description +----------------------------------- | --------------- | ------------------------- +json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. +json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. +json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. +json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid. +json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid. +json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range. +json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. +json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. +json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. +json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. +json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to. +json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container. +json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered. +json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin(). + +@liveexample{The following code shows how an `invalid_iterator` exception can be +caught.,invalid_iterator} + +@sa - @ref exception for the base class of the library exceptions +@sa - @ref parse_error for exceptions indicating a parse error +@sa - @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa - @ref out_of_range for exceptions indicating access out of the defined range +@sa - @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class invalid_iterator : public exception +{ + public: + static invalid_iterator create(int id_, const std::string& what_arg) + { + std::string w = exception::name("invalid_iterator", id_) + what_arg; + return invalid_iterator(id_, w.c_str()); + } + + private: + invalid_iterator(int id_, const char* what_arg) + : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating executing a member function with a wrong type + +This exception is thrown in case of a type error; that is, a library function is +executed on a JSON value whose type does not match the expected semantics. + +Exceptions have ids 3xx. + +name / id | example message | description +----------------------------- | --------------- | ------------------------- +json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. +json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. +json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t &. +json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types. +json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types. +json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types. +json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types. +json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types. +json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types. +json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types. +json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types. +json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types. +json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined. +json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers. +json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive. +json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. | +json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) | + +@liveexample{The following code shows how a `type_error` exception can be +caught.,type_error} + +@sa - @ref exception for the base class of the library exceptions +@sa - @ref parse_error for exceptions indicating a parse error +@sa - @ref invalid_iterator for exceptions indicating errors with iterators +@sa - @ref out_of_range for exceptions indicating access out of the defined range +@sa - @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class type_error : public exception +{ + public: + static type_error create(int id_, const std::string& what_arg) + { + std::string w = exception::name("type_error", id_) + what_arg; + return type_error(id_, w.c_str()); + } + + private: + type_error(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating access out of the defined range + +This exception is thrown in case a library function is called on an input +parameter that exceeds the expected range, for instance in case of array +indices or nonexisting object keys. + +Exceptions have ids 4xx. + +name / id | example message | description +------------------------------- | --------------- | ------------------------- +json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1. +json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it. +json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object. +json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved. +json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. +json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF. +json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. | +json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. | +json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string | + +@liveexample{The following code shows how an `out_of_range` exception can be +caught.,out_of_range} + +@sa - @ref exception for the base class of the library exceptions +@sa - @ref parse_error for exceptions indicating a parse error +@sa - @ref invalid_iterator for exceptions indicating errors with iterators +@sa - @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa - @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class out_of_range : public exception +{ + public: + static out_of_range create(int id_, const std::string& what_arg) + { + std::string w = exception::name("out_of_range", id_) + what_arg; + return out_of_range(id_, w.c_str()); + } + + private: + out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating other library errors + +This exception is thrown in case of errors that cannot be classified with the +other exception types. + +Exceptions have ids 5xx. + +name / id | example message | description +------------------------------ | --------------- | ------------------------- +json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. + +@sa - @ref exception for the base class of the library exceptions +@sa - @ref parse_error for exceptions indicating a parse error +@sa - @ref invalid_iterator for exceptions indicating errors with iterators +@sa - @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa - @ref out_of_range for exceptions indicating access out of the defined range + +@liveexample{The following code shows how an `other_error` exception can be +caught.,other_error} + +@since version 3.0.0 +*/ +class other_error : public exception +{ + public: + static other_error create(int id_, const std::string& what_arg) + { + std::string w = exception::name("other_error", id_) + what_arg; + return other_error(id_, w.c_str()); + } + + private: + other_error(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; +} // namespace detail +} // namespace nlohmann + +// #include + + +#include // pair + +// This file contains all internal macro definitions +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdocumentation" +#endif + +// allow for portable deprecation warnings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) + #define JSON_DEPRECATED __declspec(deprecated) +#else + #define JSON_DEPRECATED +#endif + +// allow for portable nodiscard warnings +#if defined(__has_cpp_attribute) + #if __has_cpp_attribute(nodiscard) + #define JSON_NODISCARD [[nodiscard]] + #elif __has_cpp_attribute(gnu::warn_unused_result) + #define JSON_NODISCARD [[gnu::warn_unused_result]] + #else + #define JSON_NODISCARD + #endif +#else + #define JSON_NODISCARD +#endif + +// allow to disable exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #include + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// manual branch prediction +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_LIKELY(x) __builtin_expect(x, 1) + #define JSON_UNLIKELY(x) __builtin_expect(x, 0) +#else + #define JSON_LIKELY(x) x + #define JSON_UNLIKELY(x) x +#endif + +// C++ language standard detection +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json + +// #include + + +#include // not +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type + +namespace nlohmann +{ +namespace detail +{ +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +template +using uncvref_t = typename std::remove_cv::type>::type; + +// implementation of C++14 index_sequence and affiliates +// source: https://stackoverflow.com/a/32223343 +template +struct index_sequence +{ + using type = index_sequence; + using value_type = std::size_t; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +template +struct merge_and_renumber; + +template +struct merge_and_renumber, index_sequence> + : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; + +template +struct make_index_sequence + : merge_and_renumber < typename make_index_sequence < N / 2 >::type, + typename make_index_sequence < N - N / 2 >::type > {}; + +template<> struct make_index_sequence<0> : index_sequence<> {}; +template<> struct make_index_sequence<1> : index_sequence<0> {}; + +template +using index_sequence_for = make_index_sequence; + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static constexpr T value{}; +}; + +template +constexpr T static_const::value; +} // namespace detail +} // namespace nlohmann + +// #include + + +#include // not +#include // numeric_limits +#include // false_type, is_constructible, is_integral, is_same, true_type +#include // declval + +// #include + + +#include // random_access_iterator_tag + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; +} // namespace detail +} // namespace nlohmann + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template +struct iterator_types {}; + +template +struct iterator_types < + It, + void_t> +{ + using difference_type = typename It::difference_type; + using value_type = typename It::value_type; + using pointer = typename It::pointer; + using reference = typename It::reference; + using iterator_category = typename It::iterator_category; +}; + +// This is required as some compilers implement std::iterator_traits in a way that +// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. +template +struct iterator_traits +{ +}; + +template +struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> + : iterator_types +{ +}; + +template +struct iterator_traits::value>> +{ + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; +}; +} // namespace detail +} // namespace nlohmann + +// #include + +// #include + +// #include + + +#include + +// #include + + +// http://en.cppreference.com/w/cpp/experimental/is_detected +namespace nlohmann +{ +namespace detail +{ +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + nonesuch(nonesuch const&&) = delete; + void operator=(nonesuch const&) = delete; + void operator=(nonesuch&&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template