Skip to content

Commit 8d75159

Browse files
fix: handling of custom containers that has a TypeDefinition
1 parent d00554d commit 8d75159

File tree

4 files changed

+79
-66
lines changed

4 files changed

+79
-66
lines changed

data_tamer_cpp/include/data_tamer/channel.hpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace DataTamer
1111
{
12+
using SerializeMe::has_TypeDefinition;
1213

1314
// Utility
1415
inline std::chrono::nanoseconds NsecSinceEpoch()
@@ -73,7 +74,7 @@ class LogChannel : public std::enable_shared_from_this<LogChannel>
7374
* @param value pointer to the value
7475
* @return the ID to be used to unregister or enable/disable this value.
7576
*/
76-
template <typename T>
77+
template <typename T, bool = true>
7778
RegistrationID registerValue(const std::string& name, const T* value);
7879

7980
/**
@@ -85,7 +86,8 @@ class LogChannel : public std::enable_shared_from_this<LogChannel>
8586
* @param value pointer to the vectors of values.
8687
* @return the ID to be used to unregister or enable/disable the values.
8788
*/
88-
template <template <class, class> class Container, class T, class... TArgs>
89+
template <template <class, class> class Container, class T, class... TArgs,
90+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
8991
RegistrationID registerValue(const std::string& name,
9092
const Container<T, TArgs...>* value);
9193

@@ -98,7 +100,8 @@ class LogChannel : public std::enable_shared_from_this<LogChannel>
98100
* @param value pointer to the array of values.
99101
* @return the ID to be used to unregister or enable/disable the values.
100102
*/
101-
template <typename T, size_t N>
103+
template <typename T, size_t N,
104+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
102105
RegistrationID registerValue(const std::string& name, const std::array<T, N>* value);
103106

104107
/**
@@ -259,7 +262,7 @@ inline void LogChannel::updateTypeRegistry()
259262
}
260263
}
261264

262-
template <typename T>
265+
template <typename T, bool>
263266
inline RegistrationID LogChannel::registerValue(const std::string& name,
264267
const T* value_ptr)
265268
{
@@ -288,7 +291,8 @@ inline RegistrationID LogChannel::registerCustomValue(const std::string& name,
288291
return registerValueImpl(name, ValuePtr(value_ptr, serializer), serializer);
289292
}
290293

291-
template <template <class, class> class Container, class T, class... TArgs>
294+
template <template <class, class> class Container, class T, class... TArgs,
295+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
292296
inline RegistrationID LogChannel::registerValue(const std::string& prefix,
293297
const Container<T, TArgs...>* vect)
294298
{
@@ -304,7 +308,8 @@ inline RegistrationID LogChannel::registerValue(const std::string& prefix,
304308
}
305309
}
306310

307-
template <typename T, size_t N>
311+
template <typename T, size_t N,
312+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
308313
inline RegistrationID LogChannel::registerValue(const std::string& prefix,
309314
const std::array<T, N>* vect)
310315
{

data_tamer_cpp/include/data_tamer/contrib/SerializeMe.hpp

+51-43
Original file line numberDiff line numberDiff line change
@@ -68,46 +68,73 @@ using SpanBytes = Span<uint8_t>;
6868
using SpanBytesConst = Span<uint8_t const>;
6969
using StringSize = uint16_t;
7070

71-
//------------- Forward declarations of BufferSize ------------------
71+
// Check if a Function like this is implemented:
72+
//
73+
// template <typename Func> std::string_view TypeDefinition(T&, Func&);
74+
75+
template <typename T, class = void>
76+
struct has_TypeDefinition : std::false_type
77+
{
78+
};
79+
80+
const auto EmptyFuncion = [](const char*, void*) {};
81+
using EmptyFunc = decltype(EmptyFuncion);
82+
83+
template <typename T1, typename T2>
84+
using enable_if_same_t = std::enable_if_t<std::is_same_v<T1, T2>>;
7285

7386
template <typename T>
87+
struct has_TypeDefinition<
88+
T, enable_if_same_t<std::string_view,
89+
decltype(TypeDefinition(std::declval<T&>(),
90+
std::declval<EmptyFunc&>()))>>
91+
: std::true_type
92+
{
93+
};
94+
95+
//------------- Forward declarations of BufferSize ------------------
96+
97+
template <typename T, bool = true>
7498
size_t BufferSize(const T& val);
7599

76100
template <>
77101
size_t BufferSize(const std::string& str);
78102

79-
template <class T, size_t N>
103+
template <class T, size_t N, std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
80104
size_t BufferSize(const std::array<T, N>& v);
81105

82-
template <template <class, class> class Container, class T, class... TArgs>
106+
template <template <class, class> class Container, class T, class... TArgs,
107+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
83108
size_t BufferSize(const Container<T, TArgs...>& vect);
84109

85110
//---------- Forward declarations of DeserializeFromBuffer -----------
86111

87-
template <typename T>
112+
template <typename T, bool = true>
88113
void DeserializeFromBuffer(SpanBytesConst& buffer, T& dest);
89114

90115
template <>
91116
void DeserializeFromBuffer(SpanBytesConst& buffer, std::string& str);
92117

93-
template <class T, size_t N>
118+
template <class T, size_t N, std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
94119
void DeserializeFromBuffer(SpanBytesConst& buffer, std::array<T, N>& v);
95120

96-
template <template <class, class> class Container, class T, class... TArgs>
121+
template <template <class, class> class Container, class T, class... TArgs,
122+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
97123
void DeserializeFromBuffer(SpanBytesConst& buffer, Container<T, TArgs...>& dest);
98124

99125
//---------- Forward declarations of SerializeIntoBuffer -----------
100126

101-
template <typename T>
127+
template <typename T, bool = true>
102128
void SerializeIntoBuffer(SpanBytes& buffer, const T& value);
103129

104130
template <>
105131
void SerializeIntoBuffer(SpanBytes& buffer, const std::string& str);
106132

107-
template <class T, size_t N>
133+
template <class T, size_t N, std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
108134
void SerializeIntoBuffer(SpanBytes& buffer, const std::array<T, N>& v);
109135

110-
template <template <class, class> class Container, class T, class... TArgs>
136+
template <template <class, class> class Container, class T, class... TArgs,
137+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
111138
void SerializeIntoBuffer(SpanBytes& buffer, const Container<T, TArgs...>& vect);
112139

113140
//-----------------------------------------------------------------------
@@ -228,31 +255,6 @@ inline T EndianSwap(T t)
228255
std::runtime_error("Problem with IndianSwap");
229256
}
230257
}
231-
232-
// Check if a Function like this is implemented:
233-
//
234-
// template <typename Func> std::string_view TypeDefinition(T&, Func&);
235-
236-
template <typename T, class = void>
237-
struct has_TypeDefinition : std::false_type
238-
{
239-
};
240-
241-
const auto EmptyFuncion = [](const char*, void*) {};
242-
using EmptyFunc = decltype(EmptyFuncion);
243-
244-
template <typename T1, typename T2>
245-
using enable_if_same_t = std::enable_if_t<std::is_same_v<T1, T2>>;
246-
247-
template <typename T>
248-
struct has_TypeDefinition<
249-
T, enable_if_same_t<std::string_view,
250-
decltype(TypeDefinition(std::declval<T&>(),
251-
std::declval<EmptyFunc&>()))>>
252-
: std::true_type
253-
{
254-
};
255-
256258
template <typename T>
257259
inline constexpr bool is_number()
258260
{
@@ -314,7 +316,7 @@ inline constexpr bool is_vector()
314316
//-----------------------------------------------------------------------
315317
//-----------------------------------------------------------------------
316318

317-
template <typename T>
319+
template <typename T, bool>
318320
inline size_t BufferSize(const T& val)
319321
{
320322
static_assert(is_number<T>() || has_TypeDefinition<T>(), "Missing TypeDefinition");
@@ -341,13 +343,15 @@ inline size_t BufferSize(const std::string& str)
341343
return sizeof(StringSize) + str.size();
342344
}
343345

344-
template <class T, size_t N>
346+
template <class T, size_t N,
347+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
345348
inline size_t BufferSize(const std::array<T, N>&)
346349
{
347350
return BufferSize(T{}) * N;
348351
}
349352

350-
template <template <class, class> class Container, class T, class... TArgs>
353+
template <template <class, class> class Container, class T, class... TArgs,
354+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
351355
inline size_t BufferSize(const Container<T, TArgs...>& vect)
352356
{
353357
if constexpr(std::is_trivially_copyable_v<T> && is_vector<Container<T, TArgs...>>())
@@ -369,7 +373,7 @@ inline size_t BufferSize(const Container<T, TArgs...>& vect)
369373
//-----------------------------------------------------------------------
370374
//-----------------------------------------------------------------------
371375

372-
template <typename T>
376+
template <typename T, bool>
373377
inline void DeserializeFromBuffer(SpanBytesConst& buffer, T& dest)
374378
{
375379
static_assert(is_number<T>() || has_TypeDefinition<T>(), "Missing TypeDefinition");
@@ -412,7 +416,8 @@ inline void DeserializeFromBuffer(SpanBytesConst& buffer, std::string& dest)
412416
buffer.trimFront(size);
413417
}
414418

415-
template <typename T, size_t N>
419+
template <typename T, size_t N,
420+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
416421
inline void DeserializeFromBuffer(SpanBytesConst& buffer, std::array<T, N>& dest)
417422
{
418423
if(N * BufferSize(T{}) > buffer.size())
@@ -434,7 +439,8 @@ inline void DeserializeFromBuffer(SpanBytesConst& buffer, std::array<T, N>& dest
434439
}
435440
}
436441

437-
template <template <class, class> class Container, class T, class... TArgs>
442+
template <template <class, class> class Container, class T, class... TArgs,
443+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
438444
inline void DeserializeFromBuffer(SpanBytesConst& buffer, Container<T, TArgs...>& dest)
439445
{
440446
uint32_t num_values = 0;
@@ -475,7 +481,7 @@ inline void DeserializeFromBuffer(SpanBytesConst& buffer, Container<T, TArgs...>
475481
//-----------------------------------------------------------------------
476482
//-----------------------------------------------------------------------
477483

478-
template <typename T>
484+
template <typename T, bool>
479485
inline void SerializeIntoBuffer(SpanBytes& buffer, T const& value)
480486
{
481487
static_assert(is_number<T>() || has_TypeDefinition<T>(), "Missing TypeDefinition");
@@ -523,7 +529,8 @@ inline void SerializeIntoBuffer(SpanBytes& buffer, std::string const& str)
523529
buffer.trimFront(size);
524530
}
525531

526-
template <typename T, size_t N>
532+
template <typename T, size_t N,
533+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
527534
inline void SerializeIntoBuffer(SpanBytes& buffer, std::array<T, N> const& vect)
528535
{
529536
if(N > std::numeric_limits<uint32_t>::max())
@@ -553,7 +560,8 @@ inline void SerializeIntoBuffer(SpanBytes& buffer, std::array<T, N> const& vect)
553560
}
554561
}
555562

556-
template <template <class, class> class Container, class T, class... TArgs>
563+
template <template <class, class> class Container, class T, class... TArgs,
564+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
557565
inline void SerializeIntoBuffer(SpanBytes& buffer, Container<T, TArgs...> const& vect)
558566
{
559567
const auto num_values = static_cast<uint32_t>(vect.size());

data_tamer_cpp/include/data_tamer/custom_types.hpp

-7
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ template <typename T>
151151
inline CustomSerializerT<T>::CustomSerializerT(std::string type_name)
152152
: _name(std::move(type_name))
153153
{
154-
static_assert(!SerializeMe::container_info<T>().is_container, "Don't pass containers a "
155-
"template type");
156-
157154
bool is_fixed_size = true;
158155
GetFixedSize<T>(is_fixed_size, _fixed_size);
159156
if(!is_fixed_size)
@@ -198,8 +195,6 @@ inline CustomSerializer::Ptr TypesRegistry::getSerializer()
198195
{
199196
static_assert(!IsNumericType<T>(), "You don't need to create a serializer for a "
200197
"numerical type.");
201-
static_assert(!SerializeMe::container_info<T>().is_container, "Don't pass containers "
202-
"as template type");
203198

204199
std::scoped_lock lk(_mutex);
205200
T dummy;
@@ -221,8 +216,6 @@ inline CustomSerializer::Ptr TypesRegistry::addType(const std::string& type_name
221216
{
222217
static_assert(!IsNumericType<T>(), "You don't need to create a serializer for a "
223218
"numerical type.");
224-
static_assert(!SerializeMe::container_info<T>().is_container, "Don't pass containers "
225-
"as template type");
226219

227220
std::scoped_lock lk(_mutex);
228221
if(skip_if_present && _types.count(type_name) != 0)

data_tamer_cpp/include/data_tamer/values.hpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace DataTamer
1111
{
12+
using SerializeMe::has_TypeDefinition;
1213

1314
/**
1415
* @brief The ValuePtr is a non-owning pointer to
@@ -19,19 +20,23 @@ class ValuePtr
1920
public:
2021
ValuePtr() = default;
2122

22-
template <typename T>
23+
template <typename T, bool = true>
2324
ValuePtr(const T* pointer, CustomSerializer::Ptr type_info = {});
2425

25-
template <template <class, class> class Container, class T, class... TArgs>
26+
template <template <class, class> class Container, class T, class... TArgs,
27+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
2628
ValuePtr(const Container<T, TArgs...>* vect);
2729

28-
template <template <class, class> class Container, class T, class... TArgs>
30+
template <template <class, class> class Container, class T, class... TArgs,
31+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool> = true>
2932
ValuePtr(const Container<T, TArgs...>* vect, CustomSerializer::Ptr type_info);
3033

31-
template <typename T, size_t N>
34+
template <typename T, size_t N,
35+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
3236
ValuePtr(const std::array<T, N>* vect);
3337

34-
template <typename T, size_t N>
38+
template <typename T, size_t N,
39+
std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool> = true>
3540
ValuePtr(const std::array<T, N>* vect, CustomSerializer::Ptr type_info);
3641

3742
ValuePtr(ValuePtr const& other) = delete;
@@ -70,7 +75,7 @@ class ValuePtr
7075
//------------------------------------------------------------
7176
//------------------------------------------------------------
7277

73-
template <typename T>
78+
template <typename T, bool>
7479
inline ValuePtr::ValuePtr(const T* pointer, CustomSerializer::Ptr type_info)
7580
: v_ptr_(pointer)
7681
, type_(GetBasicType<T>())
@@ -89,7 +94,8 @@ inline ValuePtr::ValuePtr(const T* pointer, CustomSerializer::Ptr type_info)
8994
}
9095
}
9196

92-
template <template <class, class> class Container, class T, class... TArgs>
97+
template <template <class, class> class Container, class T, class... TArgs,
98+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
9399
inline ValuePtr::ValuePtr(const Container<T, TArgs...>* vect)
94100
: v_ptr_(vect)
95101
, type_(GetBasicType<T>())
@@ -103,7 +109,8 @@ inline ValuePtr::ValuePtr(const Container<T, TArgs...>* vect)
103109
get_size_impl_ = [vect]() -> size_t { return SerializeMe::BufferSize(*vect); };
104110
}
105111

106-
template <template <class, class> class Container, class T, class... TArgs>
112+
template <template <class, class> class Container, class T, class... TArgs,
113+
std::enable_if_t<!has_TypeDefinition<Container<T, TArgs...>>::value, bool>>
107114
inline ValuePtr::ValuePtr(const Container<T, TArgs...>* vect,
108115
CustomSerializer::Ptr type_info)
109116
: v_ptr_(vect)
@@ -137,7 +144,7 @@ inline ValuePtr::ValuePtr(const Container<T, TArgs...>* vect,
137144
};
138145
}
139146

140-
template <typename T, size_t N>
147+
template <typename T, size_t N, std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
141148
inline ValuePtr::ValuePtr(const std::array<T, N>* array)
142149
: v_ptr_(array)
143150
, type_(GetBasicType<T>())
@@ -151,7 +158,7 @@ inline ValuePtr::ValuePtr(const std::array<T, N>* array)
151158
get_size_impl_ = [array]() { return SerializeMe::BufferSize(*array); };
152159
}
153160

154-
template <typename T, size_t N>
161+
template <typename T, size_t N, std::enable_if_t<!has_TypeDefinition<std::array<T, N>>::value, bool>>
155162
inline ValuePtr::ValuePtr(const std::array<T, N>* array, CustomSerializer::Ptr type_info)
156163
: v_ptr_(array)
157164
, type_(GetBasicType<T>())

0 commit comments

Comments
 (0)