1#ifndef CHI_DATA_TYPES_VARYING_H
2#define CHI_DATA_TYPES_VARYING_H
20 static constexpr bool value = std::is_same_v<T, std::vector<std::byte>>;
25 static constexpr bool value = std::is_same_v<T, bool>;
31 std::is_same_v<T, std::string> or std::is_same_v<T, char*>;
36 static constexpr bool value = std::is_floating_point_v<T>;
42 std::is_integral_v<T> and not std::is_same_v<T, bool>;
46 using BoolType =
typename std::enable_if_t<IsBool<T>::value, T>;
48 using FloatType =
typename std::enable_if_t<IsFloat<T>::value, T>;
50 using IntegerType =
typename std::enable_if_t<IsInteger<T>::value, T>;
58 typename std::enable_if_t<IsInteger<T>::value, int64_t>;
69 return static_cast<double>(value);
75 return static_cast<int64_t
>(value);
86 virtual std::vector<std::byte>
BytesValue()
const;
88 virtual std::unique_ptr<VaryingType>
Clone()
const = 0;
89 virtual size_t Size()
const = 0;
107 template <
typename T>
128 std::unique_ptr<VaryingType>
Clone()
const override
130 return std::make_unique<VaryingArbitraryType<T>>(
value_);
132 size_t Size()
const override {
return sizeof(T); }
138 switch (this->
Type())
158 return not(*
this == that);
164 switch (this->
Type())
185 switch (this->
Type())
204 return (*
this > that) or (*
this == that);
208 return (*
this < that) or (*
this == that);
217 std::unique_ptr<VaryingType>
data_ =
nullptr;
231 template <
typename T>
234 constexpr bool is_supported_type =
236 static_assert(is_supported_type,
237 "Constructor called with unsupported type");
255 static std::unique_ptr<VaryingType>
Helper(
const bool& value)
257 return std::make_unique<VaryingArbitraryType<bool>>(value);
260 static std::unique_ptr<VaryingType>
Helper(
const int64_t& value)
262 return std::make_unique<VaryingArbitraryType<int64_t>>(value);
265 static std::unique_ptr<VaryingType>
Helper(
const double& value)
267 return std::make_unique<VaryingArbitraryType<double>>(value);
309 explicit Varying(
const std::vector<std::byte>& value);
311 explicit Varying(
const std::string& value);
315 :
Varying((not value) ? std::string() : std::string(value))
342 template <typename T, std::enable_if_t<IsBool<T>::value,
bool> =
true>
346 data_ = std::make_unique<VaryingArbitraryType<bool>>(value);
352 template <typename T, std::enable_if_t<IsInteger<T>::value,
bool> =
true>
356 data_ = std::make_unique<VaryingArbitraryType<int64_t>>(value);
362 template <typename T, std::enable_if_t<IsFloat<T>::value,
bool> =
true>
366 data_ = std::make_unique<VaryingArbitraryType<double>>(value);
382 return (*
this > that) or (*
this == that);
389 return (*
this < that) or (*
this == that);
393 template <
typename T>
401 template <
typename T>
404 static constexpr bool value = std::is_integral_v<T> and
405 std::is_signed_v<T> and
406 not std::is_same_v<T, bool>;
408 template <
typename T>
411 static constexpr bool value = std::is_integral_v<T> and
412 std::is_unsigned_v<T> and
413 not std::is_same_v<T, bool>;
416 template <
typename T>
417 using StringType =
typename std::enable_if_t<IsString<T>::value, T>;
418 template <
typename T>
420 typename std::enable_if_t<IsSignedInteger<T>::value, T>;
421 template <
typename T>
423 typename std::enable_if_t<IsUnsignedInteger<T>::value, T>;
426 template <
typename T>
431 return data_->BoolValue();
435 template <
typename T>
440 const double value =
data_->FloatValue();
442 return static_cast<T
>(value);
446 template <
typename T>
451 return data_->StringValue();
455 template <
typename T>
460 const int64_t value =
data_->IntegerValue();
462 return static_cast<T
>(value);
466 template <
typename T>
471 const int64_t value =
data_->IntegerValue();
474 throw std::logic_error(std::string(__PRETTY_FUNCTION__) +
475 ": Attempt to cast negative number to unsigned.");
477 return static_cast<T
>(value);
499 std::string
PrintStr(
bool with_type =
true)
const;
VaryingArbitraryType(T value)
bool operator<(const VaryingType &that) const override
bool operator>=(const VaryingType &that) const override
bool operator!=(const VaryingType &that) const override
bool operator>(const VaryingType &that) const override
double FloatValue() const override
bool BoolValue() const override
int64_t IntegerValue() const override
bool operator==(const VaryingType &that) const override
bool operator<=(const VaryingType &that) const override
size_t Size() const override
std::unique_ptr< VaryingType > Clone() const override
std::string StringValue() const override
virtual bool operator==(const VaryingType &that) const =0
virtual double FloatValue() const
virtual std::unique_ptr< VaryingType > Clone() const =0
virtual ~VaryingType()=default
VaryingDataType Type() const
VaryingType(VaryingDataType type)
virtual int64_t IntegerValue() const
virtual bool operator!=(const VaryingType &that) const =0
virtual bool operator>(const VaryingType &that) const =0
virtual std::vector< std::byte > BytesValue() const
virtual bool operator<(const VaryingType &that) const =0
virtual std::string StringValue() const
virtual size_t Size() const =0
virtual bool operator<=(const VaryingType &that) const =0
virtual bool operator>=(const VaryingType &that) const =0
virtual bool BoolValue() const
BoolStorageType< T > CastValue(const T &value)
std::string PrintStr(bool with_type=true) const
typename std::enable_if_t< IsString< T >::value, T > StringType
typename std::enable_if_t< IsInteger< T >::value, int64_t > IntegerStorageType
bool operator<(const Varying &that) const
UnsignedIntegerType< T > GetValue() const
static std::unique_ptr< VaryingType > Helper(const double &value)
typename std::enable_if_t< IsFloat< T >::value, double > FloatStorageType
static std::unique_ptr< VaryingType > Helper(const int64_t &value)
typename std::enable_if_t< IsInteger< T >::value, T > IntegerType
Varying & operator=(const T &value)
VaryingDataType Type() const
bool operator>=(const Varying &that) const
typename std::enable_if_t< IsFloat< T >::value, T > FloatType
typename std::enable_if_t< IsUnsignedInteger< T >::value, T > UnsignedIntegerType
bool operator>(const Varying &that) const
FloatType< T > GetValue() const
IntegerStorageType< T > CastValue(const T &value)
typename std::enable_if_t< IsBool< T >::value, bool > BoolStorageType
bool operator==(const Varying &that) const
typename std::enable_if_t< IsSignedInteger< T >::value, T > SignedIntegerType
double FloatValue() const
std::string StringValue() const
Varying & operator=(const Varying &other)
static std::unique_ptr< VaryingType > Helper(const bool &value)
StringType< T > GetValue() const
int64_t IntegerValue() const
BoolType< T > GetValue() const
bool operator<=(const Varying &that) const
void CheckTypeMatch(VaryingDataType type_A, VaryingDataType type_B_required) const
std::unique_ptr< VaryingType > data_
SignedIntegerType< T > GetValue() const
bool operator!=(const Varying &that) const
typename std::enable_if_t< IsBool< T >::value, T > BoolType
FloatStorageType< T > CastValue(const T &value)
std::string TypeName() const
Varying(const char *value)
@ INTEGER
Datatype mapping to int64_t.
@ STRING
Datatype mapping to std::string.
@ VOID
Basically undefined or null.
@ BOOL
Datatype mapping to bool.
@ ARBITRARY_BYTES
Basic sequence of bytes.
@ FLOAT
Datatype mapping to double.
std::string VaryingDataTypeStringName(VaryingDataType type)
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value
std::ostream & operator<<(std::ostream &outstr, const chi_data_types::Varying &value)