RAMSES Documentation  27.0.130
Information for RAMSES users and developers
StronglyTypedValue.h
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // Copyright (C) 2017 BMW Car IT GmbH
3 // -------------------------------------------------------------------------
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 // -------------------------------------------------------------------------
8 
9 #ifndef RAMSES_STRONGLYTYPEDVALUE_H
10 #define RAMSES_STRONGLYTYPEDVALUE_H
11 
13 #include <functional>
14 #include <type_traits>
15 
16 namespace ramses
17 {
21  template <typename _baseType, _baseType _invalid, typename _uniqueId>
23  {
24  public:
28  using BaseType = _baseType;
29 
34  static constexpr StronglyTypedValue Invalid()
35  {
36  return StronglyTypedValue(_invalid);
37  }
38 
43  explicit constexpr StronglyTypedValue(BaseType value)
44  : m_value(value)
45  {
46  }
47 
51  constexpr StronglyTypedValue()
52  : m_value(_invalid)
53  {
54  }
55 
60  constexpr StronglyTypedValue(const StronglyTypedValue& other) = default;
61 
68 
73  constexpr BaseType getValue() const
74  {
75  return m_value;
76  }
77 
83  {
84  return m_value;
85  }
86 
91  constexpr bool isValid() const
92  {
93  return m_value != _invalid;
94  }
95 
101  constexpr bool operator==(const StronglyTypedValue& other) const
102  {
103  return m_value == other.m_value;
104  }
105 
111  constexpr bool operator!=(const StronglyTypedValue& other) const
112  {
113  return m_value != other.m_value;
114  }
115 
116  static_assert(std::is_arithmetic<BaseType>::value || std::is_pointer<BaseType>::value, "expected arithmetic or pointer basetype");
117 
118  private:
119  BaseType m_value;
120  };
121 }
122 
123 namespace std
124 {
126  template <typename _baseType, _baseType _invalid, typename _uniqueId>
127  struct hash<ramses::StronglyTypedValue<_baseType, _invalid, _uniqueId>>
128  {
129  public:
136  {
137  return static_cast<size_t>(hash<_baseType>()(v.getValue()));
138  }
139  };
140 }
141 
142 #endif
Helper class to create strongly typed values out of various types.
Definition: StronglyTypedValue.h:23
_baseType BaseType
The underlying type of the class.
Definition: StronglyTypedValue.h:28
constexpr StronglyTypedValue()
Default constructor with invalid value.
Definition: StronglyTypedValue.h:51
constexpr BaseType getValue() const
Getter for retrieving the underlying value.
Definition: StronglyTypedValue.h:73
constexpr bool isValid() const
Predicate to check value is unequal to Invalid() value.
Definition: StronglyTypedValue.h:91
constexpr StronglyTypedValue(const StronglyTypedValue &other)=default
Copy constructor.
constexpr StronglyTypedValue(BaseType value)
Constructor based on the underlying type.
Definition: StronglyTypedValue.h:43
StronglyTypedValue & operator=(const StronglyTypedValue &other)=default
Assignment operator.
BaseType & getReference()
Getter for a reference of the underlying value.
Definition: StronglyTypedValue.h:82
constexpr bool operator==(const StronglyTypedValue &other) const
Equals operator, compares the object to another.
Definition: StronglyTypedValue.h:101
constexpr bool operator!=(const StronglyTypedValue &other) const
Not equals operator, compares the object to another.
Definition: StronglyTypedValue.h:111
static constexpr StronglyTypedValue Invalid()
Static Getter for Invalid.
Definition: StronglyTypedValue.h:34
The RAMSES namespace contains all client side objects and functions used to implement RAMSES applicat...
Definition: AnimatedProperty.h:15
Definition: Glyph.h:87
size_t operator()(const ramses::StronglyTypedValue< _baseType, _invalid, _uniqueId > &v) const
Hasher implementation.
Definition: StronglyTypedValue.h:135