@@ -7,47 +7,51 @@ namespace cage
7
7
{
8
8
namespace detail
9
9
{
10
+ template <class T , uint32 MaxSize>
11
+ concept AnyValueConcept = std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T> && std::is_same_v<std::remove_cvref_t <T>, T> && sizeof (T) <= MaxSize && sizeof (T) > 0 ;
12
+
10
13
template <uint32 MaxSize>
11
14
struct alignas (16 ) AnyBase
12
15
{
13
16
AnyBase () = default ;
14
17
AnyBase (const AnyBase &) = default ;
15
18
16
- template <class T >
19
+ template <AnyValueConcept<MaxSize> T>
17
20
CAGE_FORCE_INLINE AnyBase (const T &v) noexcept
18
21
{
19
- static_assert (std::is_trivially_copyable_v<T>);
20
- static_assert (sizeof (T) <= MaxSize);
21
- static_assert (sizeof (T) > 0 );
22
22
detail::typeIndex<T>(); // detect hash collisions
23
23
detail::memcpy (data_, &v, sizeof (T));
24
24
type_ = detail::typeHash<T>();
25
25
}
26
26
27
27
AnyBase &operator =(const AnyBase &) = default ;
28
28
29
- template <class T >
29
+ template <AnyValueConcept<MaxSize> T>
30
30
CAGE_FORCE_INLINE AnyBase &operator =(const T &v) noexcept
31
31
{
32
32
return *this = AnyBase (v);
33
33
}
34
34
35
- CAGE_FORCE_INLINE void clear () noexcept { type_ = m; }
36
- CAGE_FORCE_INLINE uint32 typeHash () const noexcept { return type_; }
37
- CAGE_FORCE_INLINE explicit operator bool () const noexcept { return type_ != m; }
35
+ template <AnyValueConcept<MaxSize> T>
36
+ CAGE_FORCE_INLINE bool has () const noexcept
37
+ {
38
+ return detail::typeHash<T>() == type_;
39
+ }
38
40
39
- template <class T >
41
+ template <AnyValueConcept<MaxSize> T>
40
42
T get () const
41
43
{
42
- static_assert (std::is_trivially_copyable_v<T>);
43
- static_assert (sizeof (T) <= MaxSize);
44
- static_assert (sizeof (T) > 0 );
45
44
CAGE_ASSERT (detail::typeHash<T>() == type_);
46
45
T tmp;
47
46
detail::memcpy (&tmp, data_, sizeof (T));
48
47
return tmp;
49
48
}
50
49
50
+ CAGE_FORCE_INLINE void clear () noexcept { type_ = m; }
51
+ CAGE_FORCE_INLINE uint32 typeHash () const noexcept { return type_; }
52
+ CAGE_FORCE_INLINE explicit operator bool () const noexcept { return type_ != m; }
53
+ static constexpr uint32 MaxSize = MaxSize;
54
+
51
55
private:
52
56
char data_[MaxSize];
53
57
uint32 type_ = m;
0 commit comments