9 #ifndef TurtleBrains_DynamicStructure_hpp
10 #define TurtleBrains_DynamicStructure_hpp
12 #include <turtle_brains/core/tb_string.hpp>
13 #include <turtle_brains/core/tb_types.hpp>
20 #include <initializer_list>
26 class DynamicStructure;
98 template<
typename Type>
DynamicStructure(std::initializer_list<const Type> arguments) :
102 for (
auto value : arguments)
121 DynamicStructure(std::initializer_list<std::pair<const tbCore::tbString, const DynamicStructure>> arguments);
154 bool IsNil(
void)
const;
244 const Type minimumValue = std::numeric_limits<Type>::min(),
const Type maximumValue = std::numeric_limits<Type>::max(),
249 if (std::is_unsigned<Type>::value)
251 tb_error_if(intValue < 0,
"%s (Expected _value(%d) to be greater than 0 for unsigned ranges.",
252 errorMessage.c_str(), intValue);
253 tb_error_if(minimumValue < 0,
"tbExternalError: Expected minimumValue of unsigned type to be greater than equal to 0.");
254 tb_error_if(maximumValue < 0,
"tbExternalError: Expected maximumValue of unsigned type to be greater than equal to 0.");
255 tb_error_if(static_cast<uint64>(intValue) < static_cast<uint64>(minimumValue) || static_cast<uint64>(intValue) > static_cast<uint64>(maximumValue),
256 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
260 tb_error_if(intValue < static_cast<int64>(minimumValue) ||intValue > static_cast<int64>(maximumValue),
261 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
264 return static_cast<Type
>(intValue);
267 template <
typename Type> Type AsRangedIntegerWithDefault(
const tbCore::tbString& errorMessage,
const Type& defaultValue,
268 const Type minimumValue = std::numeric_limits<Type>::min(),
const Type maximumValue = std::numeric_limits<Type>::max(),
271 if (
true ==
IsNil() || (
false == implicitConversion &&
false ==
IsInteger()))
276 return AsRangedInteger(errorMessage, minimumValue, maximumValue, implicitConversion);
349 const Type minimumValue = std::numeric_limits<Type>::min(),
const Type maximumValue = std::numeric_limits<Type>::max())
const
353 if (std::is_unsigned<Type>::value)
355 tb_error_if(std::is_unsigned<Type>::value && intValue < 0,
"%s (Expected _value(%d) to be greater than 0 for unsigned ranges.",
356 errorMessage.c_str(), intValue);
357 tb_error_if(static_cast<uint64>(intValue) < minimumValue || static_cast<uint64>(intValue) > maximumValue,
358 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
362 tb_error_if(intValue < static_cast<int64>(minimumValue) ||intValue > static_cast<int64>(maximumValue),
363 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
366 return static_cast<Type
>(intValue);
382 float AsFloatWithDefault(
const float defaultValue,
bool implicitConversion =
true)
const;
492 template<
typename Type>
const DynamicStructure& operator[](
const Type& nameOrIndex)
const {
return LookupByNameOrIndex(nameOrIndex); }
497 template<
typename Type>
DynamicStructure& operator[](
const Type& nameOrIndex) {
return LookupByNameOrIndex(nameOrIndex); }
551 size_t size(
void)
const;
559 ArrayContainer::const_iterator
BeginArray(
void)
const;
569 ArrayContainer::const_iterator
begin(
void)
const;
574 ArrayContainer::iterator
begin(
void);
582 ArrayContainer::const_iterator
EndArray(
void)
const;
587 ArrayContainer::iterator
EndArray(
void);
592 ArrayContainer::const_iterator
end(
void)
const;
597 ArrayContainer::iterator
end(
void);
608 const ArrayContainer&
AsArray(
void)
const;
680 StructureContainer::const_iterator
EndStructure(
void)
const;
724 explicit operator int()
const {
return AsRangedInteger<int>(); }
729 explicit operator float()
const {
return AsFloat(
true); }
734 explicit operator bool()
const {
return AsBoolean(
true); }
773 float ConvertToFloat(
void)
const;
778 bool ConvertToBoolean(
void)
const;
804 DynamicStructureValueType mValueType;
814 ArrayContainer* mArray;
815 StructureContainer* mStructure;
833 inline const tbCore::DynamicStructure& tbCore::DynamicStructure::LookupByNameOrIndex(
const char* memberName)
const {
return GetMember(memberName); }
834 inline tbCore::DynamicStructure& tbCore::DynamicStructure::LookupByNameOrIndex(
const char* memberName) {
return GetMember(memberName); }
843 std::ostream& operator<<(std::ostream& outputStream,
const DynamicStructure& data);
851 inline bool operator==(
const DynamicStructure& leftSide,
const int& rightSide) {
return (leftSide.AsInteger() == rightSide) ?
true :
false; }
852 inline bool operator==(
const int& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() == leftSide) ?
true :
false; }
853 inline bool operator!=(
const DynamicStructure& leftSide,
const int& rightSide) {
return (leftSide.AsInteger() != rightSide) ?
true :
false; }
854 inline bool operator!=(
const int& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() != leftSide) ?
true :
false; }
856 inline bool operator==(
const DynamicStructure& leftSide,
const tbCore::int64& rightSide) {
return (leftSide.AsInteger() == rightSide) ?
true :
false; }
857 inline bool operator==(
const tbCore::int64& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() == leftSide) ?
true :
false; }
858 inline bool operator!=(
const DynamicStructure& leftSide,
const tbCore::int64& rightSide) {
return (leftSide.AsInteger() != rightSide) ?
true :
false; }
859 inline bool operator!=(
const tbCore::int64& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() != leftSide) ?
true :
false; }
861 inline bool operator==(
const DynamicStructure& leftSide,
const float& rightSide) {
return (fabs(leftSide.AsFloat() - rightSide) <=
DynamicStructure::kFloatElipson) ?
true :
false; }
862 inline bool operator==(
const float& leftSide,
const DynamicStructure& rightSide) {
return (fabs(rightSide.AsFloat() - leftSide) <=
DynamicStructure::kFloatElipson) ?
true :
false; }
863 inline bool operator!=(
const DynamicStructure& leftSide,
const float& rightSide) {
return (fabs(leftSide.AsFloat() - rightSide) >
DynamicStructure::kFloatElipson) ?
true :
false; }
864 inline bool operator!=(
const float& leftSide,
const DynamicStructure& rightSide) {
return (fabs(rightSide.AsFloat() - leftSide) >
DynamicStructure::kFloatElipson) ?
true :
false; }
866 inline bool operator==(
const DynamicStructure& leftSide,
const bool& rightSide) {
return (leftSide.AsBoolean() == rightSide) ?
true :
false; }
867 inline bool operator==(
const bool& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsBoolean() == leftSide) ?
true :
false; }
868 inline bool operator!=(
const DynamicStructure& leftSide,
const bool& rightSide) {
return (leftSide.AsBoolean() != rightSide) ?
true :
false; }
869 inline bool operator!=(
const bool& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsBoolean() != leftSide) ?
true :
false; }
871 inline bool operator==(
const DynamicStructure& leftSide,
const tbCore::tbString& rightSide) {
return (leftSide.AsString() == rightSide) ?
true :
false; }
872 inline bool operator==(
const tbCore::tbString& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsString() == leftSide) ?
true :
false; }
873 inline bool operator!=(
const DynamicStructure& leftSide,
const tbCore::tbString& rightSide) {
return (leftSide.AsString() != rightSide) ?
true :
false; }
874 inline bool operator!=(
const tbCore::tbString& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsString() != leftSide) ?
true :
false; }
int64_t int64
Signed integer with a size of 64 bits. Supports values from -(2^63) to (2^63 - 1).
Definition: tb_types.hpp:29
tbCore::tbString ToJsonString(const tbCore::DynamicStructure &data, bool prettyJson)
ArrayContainer::const_iterator BeginArray(void) const
const DynamicStructure & GetMember(const tbCore::tbString &memberName) const
bool IsString(void) const
tbCore::tbString AsStringWithDefault(const tbCore::tbString &defaultValue, bool implicitConversion=true) const
tbCore::int64 AsIntegerWithDefault(const tbCore::int64 &defaultValue, bool implicitConversion=true) const
DynamicStructure & PushValue(const DynamicStructure &value)
static const bool kImplicitConversions
Definition: tb_dynamic_structure.hpp:824
Type AsRangedIntegerWithDefault(const Type &defaultValue, const tbCore::tbString &errorMessage, const Type minimumValue=std::numeric_limits< Type >::min(), const Type maximumValue=std::numeric_limits< Type >::max()) const
Definition: tb_dynamic_structure.hpp:348
static const DynamicStructure kNullValue
Definition: tb_dynamic_structure.hpp:819
ArrayContainer::const_iterator begin(void) const
tbCore::tbString AsString(bool implicitConversion=kImplicitConversions) const
StructureContainer::const_iterator EndStructure(void) const
static const float kFloatElipson
Definition: tb_dynamic_structure.hpp:826
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
float AsFloat(bool implicitConversion=kImplicitConversions) const
static const tbCore::tbString kTrueAsString
Definition: tb_dynamic_structure.hpp:821
size_t StructureSize(void) const
static const unsigned int kInvalidSize
Definition: tb_dynamic_structure.hpp:823
uint64_t uint64
Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1).
Definition: tb_types.hpp:30
bool operator!=(const DynamicStructure &rightSide) const
friend void WriteBinary(const tbCore::DynamicStructure &data, std::ostream &output)
uint8_t uint8
Unsigned integer with a size of 8 bits. Supports values from 0 to 255.
Definition: tb_types.hpp:23
const StructureContainer & AsStructure(void) const
ArrayContainer::const_iterator EndArray(void) const
Type AsRangedInteger(const tbCore::tbString &errorMessage="Out of Range", const Type minimumValue=std::numeric_limits< Type >::min(), const Type maximumValue=std::numeric_limits< Type >::max(), bool implicitConversion=kImplicitConversions) const
Definition: tb_dynamic_structure.hpp:243
static const tbCore::tbString kNullAsString
Definition: tb_dynamic_structure.hpp:820
bool IsBoolean(void) const
bool AsBooleanWithDefault(const bool defaultValue, bool implicitConversion=true) const
Definition: tb_dynamic_structure.hpp:37
size_t ArraySize(void) const
const DynamicStructure & GetValue(const tbCore::uint64 &arrayIndex) const
float AsFloatWithDefault(const float defaultValue, bool implicitConversion=true) const
void WriteBinary(const tbCore::DynamicStructure &data, std::ostream &output)
bool IsInteger(void) const
std::map< tbCore::tbString, DynamicStructure > StructureContainer
Definition: tb_dynamic_structure.hpp:48
DynamicStructure(std::initializer_list< const Type > arguments)
Definition: tb_dynamic_structure.hpp:98
static const tbCore::tbString kFalseAsString
Definition: tb_dynamic_structure.hpp:822
bool operator==(const DynamicStructure &rightSide) const
bool AsBoolean(bool implicitConversion=kImplicitConversions) const
std::vector< DynamicStructure > ArrayContainer
Definition: tb_dynamic_structure.hpp:43
StructureContainer::const_iterator BeginStructure(void) const
DynamicStructure & AddMember(const tbCore::tbString &memberName, const DynamicStructure &memberValue)
void SetValue(const int &integerValue, bool implicitTypeChange=kImplicitTypeChange)
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
tbCore::int64 AsInteger(bool implicitConversion=kImplicitConversions) const
ArrayContainer::const_iterator end(void) const
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42
std::string tbString
Definition: tb_string.hpp:335
bool IsStructure(void) const
DynamicStructure & operator=(const DynamicStructure &other)
const ArrayContainer & AsArray(void) const
static const bool kImplicitTypeChange
Definition: tb_dynamic_structure.hpp:825
DynamicStructure & SetMember(const tbCore::tbString &memberName, const DynamicStructure &memberValue)