SPH
Dynamic.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/Means.h"
12 
14 
18 enum class DynamicId {
19  SIZE = 1,
20  FLOAT,
21  VECTOR,
22  TENSOR,
26  STRING,
27 };
28 
35 class Dynamic {
36 private:
39  Size,
40  Float,
41  Vector,
42  Tensor,
45  MinMaxMean,
46  std::string>;
47 
48  DynamicVariant storage;
49 
50 public:
53 
55 
57  template <typename T, typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Dynamic>::value>>
58  Dynamic(T&& value)
59  : storage(std::forward<T>(value)) {}
60 
61  Dynamic(const Dynamic& other)
62  : storage(other.storage) {}
63 
64  Dynamic(Dynamic&& other)
65  : storage(std::move(other.storage)) {}
66 
68  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
69  Dynamic& operator=(T&& rhs) {
70  storage = std::forward<T>(rhs);
71  return *this;
72  }
73 
74  Dynamic& operator=(const Dynamic& other) {
75  storage = other.storage;
76  return *this;
77  }
78 
80  storage = std::move(other.storage);
81  return *this;
82  }
83 
87  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
88  INLINE T& get() {
89  return storage.get<T>();
90  }
91 
93  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
94  const T& get() const {
95  return storage.get<T>();
96  }
97 
99  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
100  operator T&() {
101  return this->get<T>();
102  }
103 
105  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
106  operator const T&() const {
107  return this->get<T>();
108  }
109 
111  Float getScalar() const {
112  return forValue(storage, ScalarFunctor());
113  }
114 
115  DynamicId getType() const {
116  return DynamicId(storage.getTypeIdx());
117  }
118 
120  bool empty() const {
121  return storage.getTypeIdx() == 0;
122  }
123 
124  explicit operator bool() const {
125  return !this->empty();
126  }
127 
128  bool operator!() const {
129  return this->empty();
130  }
131 
133  template <typename T, typename = std::enable_if_t<DynamicVariant::canHold<T>()>>
134  bool operator==(const T& value) const {
135  return this->get<T>() == value;
136  }
137 
139  friend std::ostream& operator<<(std::ostream& stream, const Dynamic& value) {
140  forValue(value.storage, [&stream](const auto& v) { stream << std::setw(20) << v; });
141  return stream;
142  }
143 
144 private:
145  struct ScalarFunctor {
146  template <typename T>
147  Float operator()(const T& value) {
148  return norm(value);
149  }
150  Float operator()(const Size value) {
151  return Float(value);
152  }
153  Float operator()(const Vector& value) {
154  return getLength(value);
155  }
156  Float operator()(const MinMaxMean& value) {
157  return value.mean();
158  }
159  Float operator()(const std::string&) {
161  }
162  Float operator()(const NothingType&) {
163  return NAN;
164  }
165  };
166 };
167 
168 
INLINE Float norm(const AntisymmetricTensor &t)
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
DynamicId
Enum representing a value type stored in a Value object.
Definition: Dynamic.h:18
@ SYMMETRIC_TENSOR
@ TRACELESS_TENSOR
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Computing minimum, maximum and mean value of floats.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Generic tensor of the 2nd order.
Symmetric traceless 2nd order tensor.
Object capable of storing values of different types.
decltype(auto) INLINE forValue(Variant< TArgs... > &variant, TFunctor &&functor)
Definition: Variant.h:428
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
BasicVector< Float > Vector
Definition: Vector.h:539
Convenient object for storing a single value of different types.
Definition: Dynamic.h:35
Float getScalar() const
Converts the stored value into a single number, using one of possible conversions.
Definition: Dynamic.h:111
Dynamic & operator=(Dynamic &&other)
Definition: Dynamic.h:79
const T & get() const
Returns the const reference to the stored value given its type.
Definition: Dynamic.h:94
bool operator==(const T &value) const
Equality operator with one of stored types.
Definition: Dynamic.h:134
Dynamic(const Dynamic &other)
Definition: Dynamic.h:61
bool empty() const
Checks if the value has been initialized.
Definition: Dynamic.h:120
~Dynamic()
Needs to be in .cpp to compile with clang, for some reason.
Dynamic & operator=(T &&rhs)
Assign one of possible value types into the value.
Definition: Dynamic.h:69
friend std::ostream & operator<<(std::ostream &stream, const Dynamic &value)
Prints the currently stored value into the stream, using << operator of its type.
Definition: Dynamic.h:139
bool operator!() const
Definition: Dynamic.h:128
DynamicId getType() const
Definition: Dynamic.h:115
INLINE T & get()
Returns the reference to the stored value given its type.
Definition: Dynamic.h:88
Dynamic()
Construct an uninitialized value.
Dynamic & operator=(const Dynamic &other)
Definition: Dynamic.h:74
Dynamic(T &&value)
Contruct value from one of possible value types.
Definition: Dynamic.h:58
Dynamic(Dynamic &&other)
Definition: Dynamic.h:64
Helper class for statistics, accumulating minimal, maximal and mean value of a set of numbers.
Definition: Means.h:172
INLINE Float mean() const
Definition: Means.h:191
Symmetric tensor of 2nd order.
Generic 2nd-order tensor with 9 independent components.
Definition: Tensor.h:13
Symmetric traceless 2nd order tensor.
INLINE Size getTypeIdx() const
Returns index of type currently stored in variant.
Definition: Variant.h:310
INLINE T & get()
Returns the stored value.
Definition: Variant.h:335
Overload of std::swap for Sph::Array.
Definition: Array.h:578