diff --git a/include/moose/archive.i b/include/moose/archive.i index 358f342..93ab1e2 100644 --- a/include/moose/archive.i +++ b/include/moose/archive.i @@ -226,7 +226,7 @@ namespace moose template void Archive::archive (const char* name, T& value, EntryTypeDummy ) { - auto range = make_range (value); + auto const range = TypeTraits::toRange (value); if (is_reading ()) { for (auto i = range.begin; i != range.end; ++i) diff --git a/include/moose/extensions/glm.h b/include/moose/extensions/glm.h index af6b120..d371c43 100644 --- a/include/moose/extensions/glm.h +++ b/include/moose/extensions/glm.h @@ -35,15 +35,15 @@ namespace moose { - template - struct TypeTraits > + template + struct TypeTraits > { static constexpr EntryType entryType = EntryType::Range; + + static auto toRange (glm::vec & v) -> Range + { + auto* p = glm::value_ptr (v); + return {p, p + n}; + } }; - - template - inline Range make_range (glm::vec & v) - { - return make_range (glm::value_ptr (v), n); - } }// end of namespace moose diff --git a/include/moose/range.h b/include/moose/range.h index 99debb9..84e05bb 100644 --- a/include/moose/range.h +++ b/include/moose/range.h @@ -26,23 +26,10 @@ namespace moose { - -template -struct Range -{ - Iterator begin; - Iterator end; -}; - -template -static auto make_range (Value* v, size_t size) -> Range ; - -template -static auto make_range (Iterator begin, Iterator end) -> Range ; - -template -static auto make_range (Container& container) -> Range; - + template + struct Range + { + Iterator begin; + Iterator end; + }; }// end of namespace moose - -#include diff --git a/include/moose/range.i b/include/moose/range.i deleted file mode 100644 index 7da4637..0000000 --- a/include/moose/range.i +++ /dev/null @@ -1,53 +0,0 @@ -// This file is part of moose, a C++ serialization library -// -// Copyright (C) 2020 Sebastian Reiter -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY -// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#pragma once - -#include - -namespace moose -{ - -template -static auto make_range (Iterator begin, Iterator end) --> Range -{ - return Range {begin, end}; -} - -template -static auto make_range (Value* v, size_t size) --> Range -{ - return make_range (v, v + size); -} - -template -static auto make_range (Container& container) --> Range -{ - return make_range (container.begin (), container.end ()); -} - -}// end of namespace moose diff --git a/include/moose/stl/array.h b/include/moose/stl/array.h index b99fd05..114aa9c 100644 --- a/include/moose/stl/array.h +++ b/include/moose/stl/array.h @@ -32,6 +32,5 @@ namespace moose { template - struct TypeTraits > - { static constexpr EntryType entryType = EntryType::Range; }; + struct TypeTraits > : public DefaultRangeTraits> {}; } diff --git a/include/moose/stl_serialization.h b/include/moose/stl_serialization.h index e9d3d64..8b49f40 100644 --- a/include/moose/stl_serialization.h +++ b/include/moose/stl_serialization.h @@ -55,6 +55,11 @@ namespace moose static constexpr EntryType entryType = EntryType::Vector; + static auto toRange (Type& vector) -> Range + { + return {vector.begin (), vector.end ()}; + } + static void pushBack (Type& vector, ValueType const& value) { vector.push_back (value); } @@ -71,6 +76,11 @@ namespace moose static constexpr EntryType entryType = EntryType::Vector; + static auto toRange (Type& map) -> Range + { + return {map.begin (), map.end ()}; + } + static void pushBack (Type& map, ValueType const& value) { map.insert (value); } @@ -86,6 +96,11 @@ namespace moose static constexpr EntryType entryType = EntryType::Vector; + static auto toRange (Type& set) -> Range + { + return {set.begin (), set.end ()}; + } + static void pushBack (Type& set, ValueType const& value) { set.insert (value); } diff --git a/include/moose/type_traits.h b/include/moose/type_traits.h index 7209552..a1bcc02 100644 --- a/include/moose/type_traits.h +++ b/include/moose/type_traits.h @@ -26,6 +26,7 @@ #pragma once #include +#include #include @@ -34,8 +35,21 @@ namespace moose enum class EntryType { Struct, - Value, ///< Built in values. Todo: Call this `BuiltIn` + + Value, ///< Built in values. + + /** A fixed range of values. Typetraits have to define the following members: + \code + template <> + struct TypeTraits + { + static constexpr EntryType entryType = EntryType::Range; + static auto toRange (YourRangeType& v) -> Range ; + }; + \endcode + */ Range, + /** A dynamic sequence of values. Think of a `std::vector`. The following typetraits has to be specified for it to be compatible with the archive. \code @@ -45,11 +59,13 @@ namespace moose static constexpr EntryType entryType = EntryType::Vector; using Type = YourVectorType; using ValueType = typename Type::value_type; + static auto toRange (YourVectorType& v) -> Range ; static void pushBack (Type& vector, ValueType const& value); static void clear (Type& vector); }; \endcode */ + Vector, /** Types which just wrap a single value of a different type may not need a custom entry. Instead it may be convenient to just forward the wrapped value for serialization. @@ -65,7 +81,9 @@ namespace moose }; \endcode */ + ForwardValue, + /** Just like `ForwardValue`, but `getForwardedValue` returns a reference.*/ ForwardReference, }; @@ -80,6 +98,16 @@ namespace moose static constexpr EntryType entryType = EntryType::Struct; }; + template + struct DefaultRangeTraits + { + static constexpr EntryType entryType = EntryType::Range; + static auto toRange (T& container) -> Range + { + return {container.begin (), container.end ()}; + } + }; + /** Overload this method for your types to specify a custom default hint which will be used during serialization, if a user didn't specify a hint in the archive call. */