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>
14 #include <turtle_brains/core/tb_file_utilities.hpp>
21 #include <initializer_list>
72 class DynamicStructure;
74 namespace FileUtilities
79 template<>
void WriteBinary<tbCore::DynamicStructure>(
const tbCore::DynamicStructure& data, TurtleBrains::Core::OutputFile& outputFile);
84 template<>
void ReadBinary<tbCore::DynamicStructure>(
tbCore::DynamicStructure&
object, TurtleBrains::Core::InputFile& inputFile);
163 mValueType(kNilType),
166 for (
auto value : arguments)
185 DynamicStructure(std::initializer_list<std::pair<const String, const DynamicStructure>> arguments);
307 const Type minimumValue = std::numeric_limits<Type>::min(),
const Type maximumValue = std::numeric_limits<Type>::max(),
312 if (std::is_unsigned<Type>::value)
314 tb_error_if(intValue < 0,
"%s (Expected _value(%d) to be greater than 0 for unsigned ranges.",
315 errorMessage.c_str(), intValue);
316 tb_error_if(minimumValue < 0,
"tbExternalError: Expected minimumValue of unsigned type to be greater than equal to 0.");
317 tb_error_if(maximumValue < 0,
"tbExternalError: Expected maximumValue of unsigned type to be greater than equal to 0.");
319 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
323 tb_error_if(intValue <
static_cast<int64>(minimumValue) ||intValue >
static_cast<int64>(maximumValue),
324 "%s (Expected: %d <= _value(%d)_ <= %d", errorMessage.c_str(), minimumValue, intValue, maximumValue);
327 return static_cast<Type
>(intValue);
354 const Type minimumValue = std::numeric_limits<Type>::min(),
const Type maximumValue = std::numeric_limits<Type>::max(),
357 if (
true ==
IsNil() || (
false == implicitConversion &&
false ==
IsInteger()))
362 return AsRangedInteger(errorMessage, minimumValue, maximumValue, implicitConversion);
508 friend void TurtleBrains::Core::FileUtilities::WriteBinary<tbCore::DynamicStructure>(
const tbCore::DynamicStructure& data, OutputFile& outputFile);
509 friend void TurtleBrains::Core::FileUtilities::ReadBinary<tbCore::DynamicStructure>(
tbCore::DynamicStructure&
object, InputFile& inputFile);
608 ArrayContainer::const_iterator
begin(
void)
const;
613 ArrayContainer::iterator
begin(
void);
621 ArrayContainer::const_iterator
EndArray(
void)
const;
631 ArrayContainer::const_iterator
end(
void)
const;
636 ArrayContainer::iterator
end(
void);
792 explicit operator int()
const {
return AsRangedInteger<int>(); }
797 explicit operator float()
const {
return AsFloat(
true); }
802 explicit operator bool()
const {
return AsBoolean(
true); }
841 float ConvertToFloat(
void)
const;
846 bool ConvertToBoolean(
void)
const;
851 String ConvertToString(
void)
const;
855 enum class NotFoundPolicy { OnMissingError, OnMissingNull, OnMissingAdd };
871 const DynamicStructure& ActuallyGetMember(
const String& memberName,
const NotFoundPolicy& policy = NotFoundPolicy::OnMissingError)
const;
872 DynamicStructure& ActuallyGetMember(
const String& memberName,
const NotFoundPolicy& policy = NotFoundPolicy::OnMissingError);
893 DynamicStructureValueType mValueType;
918 namespace Implementation {
919 template<
class> constexpr
bool tbiDependentTrue =
true;
929 std::ostream& operator<<(std::ostream& outputStream,
const DynamicStructure& data);
937 inline bool operator==(
const DynamicStructure& leftSide,
const int& rightSide) {
return (leftSide.
AsInteger() == rightSide) ? true :
false; }
938 inline bool operator==(
const int& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.
AsInteger() == leftSide) ? true :
false; }
939 inline bool operator!=(
const DynamicStructure& leftSide,
const int& rightSide) {
return (leftSide.AsInteger() != rightSide) ? true :
false; }
940 inline bool operator!=(
const int& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() != leftSide) ? true :
false; }
942 inline bool operator==(
const DynamicStructure& leftSide,
const tbCore::int64& rightSide) {
return (leftSide.AsInteger() == rightSide) ? true :
false; }
943 inline bool operator==(
const tbCore::int64& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() == leftSide) ? true :
false; }
944 inline bool operator!=(
const DynamicStructure& leftSide,
const tbCore::int64& rightSide) {
return (leftSide.AsInteger() != rightSide) ? true :
false; }
945 inline bool operator!=(
const tbCore::int64& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsInteger() != leftSide) ? true :
false; }
947 inline bool operator==(
const DynamicStructure& leftSide,
const float& rightSide) {
return (fabs(leftSide.AsFloat() - rightSide) <=
DynamicStructure::kFloatElipson) ? true :
false; }
948 inline bool operator==(
const float& leftSide,
const DynamicStructure& rightSide) {
return (fabs(rightSide.AsFloat() - leftSide) <=
DynamicStructure::kFloatElipson) ? true :
false; }
949 inline bool operator!=(
const DynamicStructure& leftSide,
const float& rightSide) {
return (fabs(leftSide.AsFloat() - rightSide) >
DynamicStructure::kFloatElipson) ? true :
false; }
950 inline bool operator!=(
const float& leftSide,
const DynamicStructure& rightSide) {
return (fabs(rightSide.AsFloat() - leftSide) >
DynamicStructure::kFloatElipson) ? true :
false; }
952 inline bool operator==(
const DynamicStructure& leftSide,
const bool& rightSide) {
return (leftSide.AsBoolean() == rightSide) ? true :
false; }
953 inline bool operator==(
const bool& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsBoolean() == leftSide) ? true :
false; }
954 inline bool operator!=(
const DynamicStructure& leftSide,
const bool& rightSide) {
return (leftSide.AsBoolean() != rightSide) ? true :
false; }
955 inline bool operator!=(
const bool& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsBoolean() != leftSide) ? true :
false; }
957 inline bool operator==(
const DynamicStructure& leftSide,
const String& rightSide) {
return (leftSide.AsString() == rightSide) ? true :
false; }
958 inline bool operator==(
const String& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsString() == leftSide) ? true :
false; }
959 inline bool operator!=(
const DynamicStructure& leftSide,
const String& rightSide) {
return (leftSide.AsString() != rightSide) ? true :
false; }
960 inline bool operator!=(
const String& leftSide,
const DynamicStructure& rightSide) {
return (rightSide.AsString() != leftSide) ? true :
false; }
972 template<
typename Type> DynamicStructure ToDynamicStructure(
const Type&
object)
976 tb_static_error_if(
true == Core::Implementation::tbiDependentTrue<Type>,
"Missing a ToDynamicStructure() overload?");
980 template<
typename Type> Type FromDynamicStructure(
const DynamicStructure& data)
984 tb_static_error_if(
true == Core::Implementation::tbiDependentTrue<Type>,
"Missing a FromDynamicStructure() overload?");
988 template<>
inline DynamicStructure ToDynamicStructure<int>(
const int& value) {
return tbCore::DynamicStructure(value); }
989 template<>
inline DynamicStructure ToDynamicStructure<bool>(
const bool& value) {
return tbCore::DynamicStructure(value); }
990 template<>
inline DynamicStructure ToDynamicStructure<float>(
const float& value) {
return tbCore::DynamicStructure(value); }
991 template<>
inline DynamicStructure ToDynamicStructure<String>(
const String& value) {
return tbCore::DynamicStructure(value); }
993 template<>
inline int FromDynamicStructure<int>(
const DynamicStructure& data) {
return data.AsRangedInteger<
int>(); }
994 template<>
inline bool FromDynamicStructure<bool>(
const DynamicStructure& data) {
return data.AsBoolean(); }
995 template<>
inline float FromDynamicStructure<float>(
const DynamicStructure& data) {
return data.AsFloat(); }
996 template<>
inline String FromDynamicStructure<String>(
const DynamicStructure& data) {
return data.AsString(); }
Definition: tb_dynamic_structure.hpp:95
DynamicStructure & SetMember(const String &memberName, const DynamicStructure &memberValue)
StructureContainer::iterator EndStructure(void)
std::vector< DynamicStructure > ArrayContainer
Definition: tb_dynamic_structure.hpp:103
const DynamicStructure & operator[](const Type &nameOrIndex) const
Definition: tb_dynamic_structure.hpp:531
DynamicStructure & operator=(const DynamicStructure &other) noexcept
size_t ArraySize(void) const
DynamicStructure(std::initializer_list< std::pair< const String, const DynamicStructure >> arguments)
bool operator==(const DynamicStructure &rightSide) const
DynamicStructure & operator[](const Type &nameOrIndex)
Definition: tb_dynamic_structure.hpp:536
static const float kFloatElipson
Definition: tb_dynamic_structure.hpp:915
DynamicStructure & GetValue(const tbCore::uint64 &arrayIndex)
DynamicStructure & GetMember(const String &memberName)
static const bool kImplicitTypeChange
Definition: tb_dynamic_structure.hpp:914
const ArrayContainer & AsArray(void) const
DynamicStructure(const char *stringValue)
float AsFloatWithDefault(const float defaultValue, bool implicitConversion=true) const
float AsFloat(bool implicitConversion=kImplicitConversions) const
tbCore::int64 AsInteger(bool implicitConversion=kImplicitConversions) const
static const DynamicStructure kNullValue
Definition: tb_dynamic_structure.hpp:908
ArrayContainer & AsArray(void)
ArrayContainer::const_iterator EndArray(void) const
static const String kTrueAsString
Definition: tb_dynamic_structure.hpp:910
DynamicStructure & PushValue(const DynamicStructure &value)
static const String kNullAsString
Definition: tb_dynamic_structure.hpp:909
const StructureContainer & AsStructure(void) const
DynamicStructure(std::initializer_list< const Type > arguments)
Definition: tb_dynamic_structure.hpp:162
static const String kFalseAsString
Definition: tb_dynamic_structure.hpp:911
bool operator!=(const DynamicStructure &rightSide) const
ArrayContainer::iterator end(void)
ArrayContainer::iterator BeginArray(void)
static const unsigned int kInvalidSize
Definition: tb_dynamic_structure.hpp:912
bool HasMember(const String &memberName) const
StructureContainer::const_iterator BeginStructure(void) const
const DynamicStructure & GetValue(const tbCore::uint64 &arrayIndex) const
const DynamicStructure & GetMember(const String &memberName) const
bool IsStructure(void) const
std::map< String, DynamicStructure > StructureContainer
Definition: tb_dynamic_structure.hpp:108
size_t StructureSize(void) const
Type AsRangedInteger(const String &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:306
void SetValue(const String &stringValue, bool implicitTypeChange=kImplicitTypeChange)
void SetValue(const int &integerValue, bool implicitTypeChange=kImplicitTypeChange)
StructureContainer & AsStructure(void)
void SetValue(const float &floatValue, bool implicitTypeChange=kImplicitTypeChange)
DynamicStructure(const DynamicStructure &other) noexcept
ArrayContainer::const_iterator BeginArray(void) const
bool IsString(void) const
DynamicStructure & AddMember(const String &memberName, const DynamicStructure &memberValue)
void SetValue(const bool &booleanValue, bool implicitTypeChange=kImplicitTypeChange)
String AsStringWithDefault(const String &defaultValue, bool implicitConversion=true) const
StructureContainer::const_iterator EndStructure(void) const
bool IsBoolean(void) const
bool AsBooleanWithDefault(const bool defaultValue, bool implicitConversion=true) const
StructureContainer::iterator BeginStructure(void)
ArrayContainer::iterator EndArray(void)
DynamicStructure(const String &stringValue)
DynamicStructure(const float &floatValue)
tbCore::int64 AsIntegerWithDefault(const tbCore::int64 &defaultValue, bool implicitConversion=kImplicitConversions) const
Type AsRangedIntegerWithDefault(const Type &defaultValue, const String &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:353
DynamicStructure(const int &integerValue)
bool IsInteger(void) const
void RemoveMember(const String &memberName)
String AsString(bool implicitConversion=kImplicitConversions) const
ArrayContainer::const_iterator begin(void) const
DynamicStructure(const bool &booleanValue)
bool AsBoolean(bool implicitConversion=kImplicitConversions) const
ArrayContainer::const_iterator end(void) const
static const bool kImplicitConversions
Definition: tb_dynamic_structure.hpp:913
ArrayContainer::iterator begin(void)
#define tb_static_error_if(errorTest, message)
Definition: tb_error.hpp:51
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
std::uint8_t uint8
Unsigned integer with a size of 8 bits. Supports values from 0 to 255.
Definition: tb_types.hpp:22
String ToJsonString(const tbCore::DynamicStructure &data, bool prettyJson)
std::string String
Definition: tb_string.hpp:302
std::int64_t int64
Signed integer with a size of 64 bits. Supports values from -(2^63) to (2^63 - 1).
Definition: tb_types.hpp:28
std::uint64_t uint64
Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1).
Definition: tb_types.hpp:29
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22