Skip to content

Commit

Permalink
Range: Avoid make_range function. Extend traits instead.
Browse files Browse the repository at this point in the history
This avoids issues where the required overload of `make_range` was not found.
  • Loading branch information
sreiter committed Jul 21, 2024
1 parent 1296b76 commit 24f2d62
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 84 deletions.
2 changes: 1 addition & 1 deletion include/moose/archive.i
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace moose
template <class T>
void Archive::archive (const char* name, T& value, EntryTypeDummy <EntryType::Range>)
{
auto range = make_range (value);
auto const range = TypeTraits<T>::toRange (value);
if (is_reading ())
{
for (auto i = range.begin; i != range.end; ++i)
Expand Down
16 changes: 8 additions & 8 deletions include/moose/extensions/glm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@

namespace moose
{
template <glm::length_t n, class T>
struct TypeTraits <glm::vec <n, T>>
template <glm::length_t n, class T, glm::qualifier Q>
struct TypeTraits <glm::vec <n, T, Q>>
{
static constexpr EntryType entryType = EntryType::Range;

static auto toRange (glm::vec <n, T, Q>& v) -> Range <float*>
{
auto* p = glm::value_ptr (v);
return {p, p + n};
}
};

template <glm::length_t n, class T>
inline Range <float*> make_range (glm::vec <n, T>& v)
{
return make_range (glm::value_ptr (v), n);
}
}// end of namespace moose
25 changes: 6 additions & 19 deletions include/moose/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,10 @@

namespace moose
{

template <class Iterator>
struct Range
{
Iterator begin;
Iterator end;
};

template <class Value>
static auto make_range (Value* v, size_t size) -> Range <Value*>;

template <class Iterator>
static auto make_range (Iterator begin, Iterator end) -> Range <Iterator>;

template <class Container>
static auto make_range (Container& container) -> Range<decltype (container.begin ())>;

template <class Iterator>
struct Range
{
Iterator begin;
Iterator end;
};
}// end of namespace moose

#include <moose/range.i>
53 changes: 0 additions & 53 deletions include/moose/range.i

This file was deleted.

3 changes: 1 addition & 2 deletions include/moose/stl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@
namespace moose
{
template <class T, size_t n>
struct TypeTraits <std::array <T, n>>
{ static constexpr EntryType entryType = EntryType::Range; };
struct TypeTraits <std::array <T, n>> : public DefaultRangeTraits<std::array<T, n>> {};
}
15 changes: 15 additions & 0 deletions include/moose/stl_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace moose

static constexpr EntryType entryType = EntryType::Vector;

static auto toRange (Type& vector) -> Range <decltype (vector.begin ())>
{
return {vector.begin (), vector.end ()};
}

static void pushBack (Type& vector, ValueType const& value)
{ vector.push_back (value); }

Expand All @@ -71,6 +76,11 @@ namespace moose

static constexpr EntryType entryType = EntryType::Vector;

static auto toRange (Type& map) -> Range <decltype (map.begin ())>
{
return {map.begin (), map.end ()};
}

static void pushBack (Type& map, ValueType const& value)
{ map.insert (value); }

Expand All @@ -86,6 +96,11 @@ namespace moose

static constexpr EntryType entryType = EntryType::Vector;

static auto toRange (Type& set) -> Range <decltype (set.begin ())>
{
return {set.begin (), set.end ()};
}

static void pushBack (Type& set, ValueType const& value)
{ set.insert (value); }

Expand Down
30 changes: 29 additions & 1 deletion include/moose/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#pragma once

#include <moose/hint.h>
#include <moose/range.h>

#include <string>

Expand All @@ -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 <YourRangeType>
{
static constexpr EntryType entryType = EntryType::Range;
static auto toRange (YourRangeType& v) -> Range <SomeIteratorType>;
};
\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
Expand All @@ -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 <SomeIteratorType>;
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.
Expand All @@ -65,7 +81,9 @@ namespace moose
};
\endcode
*/

ForwardValue,

/** Just like `ForwardValue`, but `getForwardedValue` returns a reference.*/
ForwardReference,
};
Expand All @@ -80,6 +98,16 @@ namespace moose
static constexpr EntryType entryType = EntryType::Struct;
};

template <class T>
struct DefaultRangeTraits
{
static constexpr EntryType entryType = EntryType::Range;
static auto toRange (T& container) -> Range <decltype (container.begin ())>
{
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.
*/
Expand Down

0 comments on commit 24f2d62

Please sign in to comment.