HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
hi::v1::datum Class Reference

#include <hikogui/codec/datum.hpp>

Data Structures

struct  break_type
struct  continue_type

Public Types

using vector_type = std::vector<datum>
using map_type = std::map<datum, datum>

Public Member Functions

constexpr datum (datum const &other) noexcept
constexpr datum (datum &&other) noexcept
constexpr datum (std::monostate) noexcept
constexpr datum (nullptr_t) noexcept
constexpr datum (continue_type) noexcept
constexpr datum (break_type) noexcept
constexpr datum (bool value) noexcept
constexpr datum (std::floating_point auto value) noexcept
constexpr datum (numeric_integral auto value) noexcept
constexpr datum (std::chrono::year_month_day value) noexcept
 datum (std::string value) noexcept
 datum (std::string_view value) noexcept
 datum (char const *value) noexcept
 datum (vector_type value) noexcept
 datum (map_type value) noexcept
 datum (bstring value) noexcept
constexpr datum & operator= (datum const &other) noexcept
constexpr datum & operator= (datum &&other) noexcept
constexpr datum & operator= (std::floating_point auto value) noexcept(sizeof(value)<=4)
constexpr datum & operator= (numeric_integral auto value) noexcept(sizeof(value)<=4)
constexpr datum & operator= (bool value) noexcept
constexpr datum & operator= (std::chrono::year_month_day value) noexcept
constexpr datum & operator= (std::monostate) noexcept
constexpr datum & operator= (nullptr_t) noexcept
datum & operator= (std::string value) noexcept
datum & operator= (char const *value) noexcept
datum & operator= (std::string_view value) noexcept
datum & operator= (vector_type value) noexcept
datum & operator= (map_type value) noexcept
datum & operator= (bstring value) noexcept
constexpr operator bool () const noexcept
constexpr bool empty () const
template<std::floating_point T>
constexpr operator T () const
template<numeric_integral T>
constexpr operator T () const
constexpr operator std::chrono::year_month_day () const
 operator std::string () const noexcept
 operator std::string_view () const
 operator vector_type () const
 operator map_type () const
 operator bstring () const
constexpr char const * type_name () const noexcept
constexpr bool is_undefined () const noexcept
 Check if the datum has an undefined value.
constexpr bool is_break () const noexcept
 Check if the result of a expression was a break flow control statement.
constexpr bool is_continue () const noexcept
 Check if the result of a expression was a continue flow control statement.
std::size_t hash () const noexcept
constexpr std::size_t size () const
constexpr datum const & back () const
constexpr datum & back ()
constexpr datum const & front () const
constexpr datum & front ()
constexpr auto cbegin () const
constexpr auto begin () const
constexpr auto begin ()
constexpr auto cend () const
constexpr auto end () const
constexpr auto end ()
vector_type keys () const
 Get the sorted list of keys of a map.
vector_type values () const
 Get the list of values of a map.
vector_type items () const
 Get key value pairs of items of a map sorted by the key.
constexpr void push_back (datum const &rhs)
constexpr void push_back (datum &&rhs)
template<typename Arg>
constexpr void push_back (Arg &&arg)
constexpr void pop_back ()
constexpr bool contains (datum const &rhs) const
template<typename Arg>
constexpr bool contains (Arg const &arg) const
std::vector< datum * > find (jsonpath const &path) noexcept
std::vector< datum const * > find (jsonpath const &path) const noexcept
bool remove (jsonpath const &path) noexcept
 Remove the object by path.
datum * find_one (jsonpath const &path) noexcept
 Find a object by path.
datum * find_one_or_create (jsonpath const &path) noexcept
 Find a object by path potentially creating intermediate objects.
datum const * find_one (jsonpath const &path) const noexcept
 Find a object by path.
datum const & operator[] (datum const &rhs) const
constexpr datum & operator[] (datum const &rhs)
constexpr datum const & operator[] (auto const &rhs) const
constexpr datum & operator[] (auto const &rhs)
constexpr datum & operator++ ()
constexpr datum & operator-- ()
constexpr datum operator++ (int)
constexpr datum operator-- (int)
constexpr datum & operator+= (auto const &rhs)
 X (-=, -) X(*
X (/=,/) X(%
X(& X (|=,|) X(^=, ^) X(<<=,<<) X(> >=, > >) friend constexpr bool operator==(datum const &lhs, datum const &rhs) noexcept { if(auto const doubles=promote_if< double >(lhs, rhs)) { return doubles.lhs()==doubles.rhs();} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return long_longs.lhs()==long_longs.rhs();} else if(auto const bools=promote_if< bool >(lhs, rhs)) { return bools.lhs()==bools.rhs();} else if(auto const ymds=promote_if< std::chrono::year_month_day >(lhs, rhs)) { return ymds.lhs()==ymds.rhs();} else if(auto const strings=promote_if< std::string >(lhs, rhs)) { return strings.lhs()==strings.rhs();} else if(auto const vectors=promote_if< vector_type >(lhs, rhs)) { return vectors.lhs()==vectors.rhs();} else if(auto const maps=promote_if< map_type >(lhs, rhs)) { return maps.lhs()==maps.rhs();} else { return lhs._tag==rhs._tag;} } friend constexpr std::partial_ordering operator<=>(datum const &lhs, datum const &rhs) noexcept { if(auto const doubles=promote_if< double >(lhs, rhs)) { return doubles.lhs()<=> doubles.rhs();} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return long_longs.lhs()<=> long_longs.rhs();} else if(auto const bools=promote_if< bool >(lhs, rhs)) { return bools.lhs()<=> bools.rhs();} else if(auto const year_month_days=promote_if< std::chrono::year_month_day >(lhs, rhs)) { return year_month_days.lhs()<=> year_month_days.rhs();} else if(auto const strings=promote_if< std::string >(lhs, rhs)) { return strings.lhs()<=> strings.rhs();} else if(auto const vectors=promote_if< vector_type >(lhs, rhs)) { return vectors.lhs()<=> vectors.rhs();} else if(auto const maps=promote_if< map_type >(lhs, rhs)) { return maps.lhs()<=> maps.rhs();} else if(auto const bstrings=promote_if< bstring >(lhs, rhs)) { return bstrings.lhs()<=> bstrings.rhs();} else { return lhs._tag<=> rhs._tag;} } friend constexpr datum operator-(datum const &rhs) { if(auto const rhs_double=get_if< double >(rhs)) { return datum{- *rhs_double};} else if(auto const rhs_long_long=get_if< long long >(rhs)) { return datum{- *rhs_long_long};} else { throw std::domain_error(std::format("Can not evaluate -{}", repr(rhs)));} } friend constexpr datum operator~(datum const &rhs) { if(auto const rhs_long_long=get_if< long long >(rhs)) { return datum{~ *rhs_long_long};} else { throw std::domain_error(std::format("Can not evaluate ~{}", repr(rhs)));} } friend constexpr datum operator+(datum const &lhs, datum const &rhs) { if(auto const doubles=promote_if< double >(lhs, rhs)) { return datum{doubles.lhs()+doubles.rhs()};} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs()+long_longs.rhs()};} else if(auto const strings=promote_if< std::string >(lhs, rhs)) { return datum{strings.lhs()+strings.rhs()};} else if(auto const vectors=promote_if< vector_type >(lhs, rhs)) { auto r=vectors.lhs();r.insert(r.end(), vectors.rhs().begin(), vectors.rhs().end());return datum{std::move(r)};} else { throw std::domain_error(std::format("Can not evaluate {} '+' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator-(datum const &lhs, datum const &rhs) { if(auto const doubles=promote_if< double >(lhs, rhs)) { return datum{doubles.lhs() - doubles.rhs()};} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs() - long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '-' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator*(datum const &lhs, datum const &rhs) { if(auto const doubles=promote_if< double >(lhs, rhs)) { return datum{doubles.lhs() *doubles.rhs()};} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs() *long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '*' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator/(datum const &lhs, datum const &rhs) { if(auto const doubles=promote_if< double >(lhs, rhs)) { if(doubles.rhs()==0) { throw std::domain_error(std::format("Divide by zero {} '/' {}", repr(lhs), repr(rhs)));} return datum{doubles.lhs()/doubles.rhs()};} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { if(long_longs.rhs()==0) { throw std::domain_error(std::format("Divide by zero {} '/' {}", repr(lhs), repr(rhs)));} return datum{long_longs.lhs()/long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '/' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator%(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { if(long_longs.rhs()==0) { throw std::domain_error(std::format("Divide by zero {} '%' {}", repr(lhs), repr(rhs)));} return datum{long_longs.lhs() % long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '%' {}", repr(lhs), repr(rhs)));} } friend constexpr datum pow(datum const &lhs, datum const &rhs) { if(auto const doubles=promote_if< double >(lhs, rhs)) { return datum{pow(doubles.lhs(), doubles.rhs())};} else if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{pow(long_longs.lhs(), long_longs.rhs())};} else { throw std::domain_error(std::format("Can not evaluate pow({}, {})", repr(lhs), repr(rhs)));} } friend constexpr datum operator&(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs() &long_longs.rhs()};} else if(auto const bools=promote_if< bool >(lhs, rhs)) { return datum{bools.lhs() and bools.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '&' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator|(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs()|long_longs.rhs()};} else if(auto const bools=promote_if< bool >(lhs, rhs)) { return datum{bools.lhs() or bools.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '|' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator^(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { return datum{long_longs.lhs() ^ long_longs.rhs()};} else if(auto const bools=promote_if< bool >(lhs, rhs)) { return datum{bools.lhs() !=bools.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '^' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator<<(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { if(long_longs.rhs()< 0 or long_longs.rhs() >(sizeof(long long) *CHAR_BIT - 1)) { throw std::domain_error(std::format("Invalid shift count {} '<<' {}", repr(lhs), repr(rhs)));} return datum{long_longs.lhs()<< long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '<<' {}", repr(lhs), repr(rhs)));} } friend constexpr datum operator>>(datum const &lhs, datum const &rhs) { if(auto const long_longs=promote_if< long long >(lhs, rhs)) { if(long_longs.rhs()< 0 or long_longs.rhs() >(sizeof(long long) *CHAR_BIT - 1)) { throw std::domain_error(std::format("Invalid shift count {} '>>' {}", repr(lhs), repr(rhs)));} return datum{long_longs.lhs() > > long_longs.rhs()};} else { throw std::domain_error(std::format("Can not evaluate {} '>>' {}", repr(lhs), repr(rhs)));} } friend std::ostream &operator<<(std::ostream &lhs, datum const &rhs) { return lhs<< to_string(rhs);}#define X(op) \ \ \ \ \ \ \ \ \ \ \ \ X(==) X(<=>) X(+) X(-) X(*) X(/) X(%) X(&) X(|) X(^) X(<<) X(> >) friend std::string repr(datum const &rhs) noexcept { switch(rhs._tag) { case tag_type::monostate:return "undefined";case tag_type::floating_point:return std::format("{:.1f}", rhs._value._double);case tag_type::integral:return std::format("{}", rhs._value._long_long);case tag_type::boolean:return rhs._value._bool ? "true" :"false";case tag_type::year_month_day:return std::format("{:%Y-%m-%d}", rhs._value._year_month_day);case tag_type::null:return "null";case tag_type::flow_break:return "break";case tag_type::flow_continue:return "continue";case tag_type::string:return std::format("\"{}\"", *rhs._value._string);case tag_type::vector:{ auto r=std::string{"["};for(auto const &item :*rhs._value._vector) { r+=repr(item);r+=',';} r+=']
 for (auto const &item :*rhs._value._map)

Static Public Member Functions

template<typename... Args>
static datum make_vector (Args const &... args) noexcept
template<typename Key, typename Value, typename... Args>
static void populate_map (map_type &r, Key const &key, Value const &value, Args const &... args) noexcept
template<typename... Args>
static datum make_map (Args const &... args) noexcept
static datum make_break () noexcept
static datum make_continue () noexcept

Data Fields

return r
 r = '}'
default : hi_no_default()

Friends

template<typename To>
constexpr auto promote_if (datum const &lhs, datum const &rhs) noexcept
 Promote two datum-arguments to a common type.
constexpr friend std::size_t size (datum const &rhs)
std::string to_string (datum const &rhs) noexcept
 Get the string representation of the value.
template<typename T>
constexpr bool holds_alternative (datum const &rhs) noexcept
 Check if the stored value is of a specific type.
template<typename T>
constexpr T const & get (datum const &rhs) noexcept
 Check if the type held by the datum can be promoted.
template<typename T>
constexpr T & get (datum &rhs) noexcept
 Get the value of a datum.
template<typename T>
constexpr T * get_if (datum &rhs) noexcept
 Get the value of a datum.
template<typename T>
constexpr T const * get_if (datum const &rhs) noexcept
 Get the value of a datum.
template<typename T>
T * get_if (datum &rhs, jsonpath const &path) noexcept
 Get the value of a datum.
template<typename T>
T const * get_if (datum const &rhs, jsonpath const &path) noexcept
 Get the value of a datum.

Detailed Description

A dynamic data type.

This class holds data of different types, useful as the data-type used for variables of scripting languages, or for serializing and deserializing JSON and other object storage formats.

Not only does this datum handle the storage of data, but can also different operations which are dynamically executed.

Member Function Documentation

◆ find_one() [1/2]

datum const * hi::v1::datum::find_one ( jsonpath const & path) const
inlinenodiscardnoexcept

Find a object by path.

Parameters
Path handling utilities.The json path to use to find an object. Path must be singular.
Returns
A pointer to the object found, or nullptr.

◆ find_one() [2/2]

datum * hi::v1::datum::find_one ( jsonpath const & path)
inlinenodiscardnoexcept

Find a object by path.

Parameters
Path handling utilities.The json path to use to find an object. Path must be singular.
Returns
A pointer to the object found, or nullptr.

◆ find_one_or_create()

datum * hi::v1::datum::find_one_or_create ( jsonpath const & path)
inlinenodiscardnoexcept

Find a object by path potentially creating intermediate objects.

Parameters
Path handling utilities.The json path to use to find an object. Path must be singular.
Returns
A pointer to the object found, or nullptr.

◆ is_break()

bool hi::v1::datum::is_break ( ) const
inlinenodiscardconstexprnoexcept

Check if the result of a expression was a break flow control statement.

See also
skeleton_node

◆ is_continue()

bool hi::v1::datum::is_continue ( ) const
inlinenodiscardconstexprnoexcept

Check if the result of a expression was a continue flow control statement.

See also
skeleton_node

◆ is_undefined()

bool hi::v1::datum::is_undefined ( ) const
inlinenodiscardconstexprnoexcept

Check if the datum has an undefined value.

◆ items()

vector_type hi::v1::datum::items ( ) const
inlinenodiscard

Get key value pairs of items of a map sorted by the key.

◆ keys()

vector_type hi::v1::datum::keys ( ) const
inlinenodiscard

Get the sorted list of keys of a map.

◆ remove()

bool hi::v1::datum::remove ( jsonpath const & path)
inlinenodiscardnoexcept

Remove the object by path.

This function will remove the object pointed to with path. Any resulting empty maps and arrays will also be removed.

Parameters
Path handling utilities.A json path to remove.
Returns
true if one or more objects where removed.

◆ values()

vector_type hi::v1::datum::values ( ) const
inlinenodiscard

Get the list of values of a map.

◆ get [1/2]

template<typename T>
T & get ( datum & rhs)
friend

Get the value of a datum.

It is undefined behavior if the type does not match the stored value.

Template Parameters
TType to check, must be one of: bool, double, long long, std::chrono::year_month_day
Parameters
rhsThe datum to get the value from.
Returns
A copy of the value in the datum.

◆ get [2/2]

template<typename T>
T const & get ( datum const & rhs)
friend

Check if the type held by the datum can be promoted.

A value can always be promoted to its own type. The following promotions also exist:

  • bool -> `long long'
Template Parameters
ToType to promote the value to.
Parameters
rhsThe value to promote. */ template<typename To> [[nodiscard]] friend constexpr bool promotable_to(datum const& rhs) noexcept { if constexpr (std::is_same_v<To, double>) { return holds_alternative<double>(rhs) or holds_alternative<long long>(rhs) or holds_alternative<bool>(rhs); } else if constexpr (std::is_same_v<To, long long>) { return holds_alternative<long long>(rhs) or holds_alternative<bool>(rhs); } else { return holds_alternative<To>(rhs); } }

/** Get the value of a datum.

It is monostate behavior if the type does not match the stored value.

Template Parameters
TType to check, must be one of: bool, double, long long, std::chrono::year_month_day
Parameters
rhsThe datum to get the value from.
Returns
A copy of the value in the datum.

◆ get_if [1/4]

template<typename T>
T * get_if ( datum & rhs)
friend

Get the value of a datum.

If the type does not matched the stored value this function will return a nullptr.

Template Parameters
TType to check.
Parameters
rhsThe datum to get the value from.
Returns
A pointer to the value, or nullptr.

◆ get_if [2/4]

template<typename T>
T * get_if ( datum & rhs,
jsonpath const & path )
friend

Get the value of a datum.

If the type does not matched the stored value this function will return a nullptr.

Template Parameters
TType to check.
Parameters
rhsThe datum to get the value from.
Path handling utilities.The json-path to the value to extract.
Returns
A pointer to the value, or nullptr.

◆ get_if [3/4]

template<typename T>
T const * get_if ( datum const & rhs)
friend

Get the value of a datum.

If the type does not matched the stored value this function will return a nullptr.

Template Parameters
TType to check.
Parameters
rhsThe datum to get the value from.
Returns
A pointer to the value, or nullptr.

◆ get_if [4/4]

template<typename T>
T const * get_if ( datum const & rhs,
jsonpath const & path )
friend

Get the value of a datum.

If the type does not matched the stored value this function will return a nullptr.

Template Parameters
TType to check.
Parameters
rhsThe datum to get the value from.
Path handling utilities.The json-path to the value to extract.
Returns
A pointer to the value, or nullptr.

◆ holds_alternative

template<typename T>
bool holds_alternative ( datum const & rhs)
friend

Check if the stored value is of a specific type.

Template Parameters
TType to check.
Parameters
rhsThe datum to check the value-type of.
Returns
True if the value-type matches the template parameter T

◆ promote_if

template<typename To>
auto promote_if ( datum const & lhs,
datum const & rhs )
friend

Promote two datum-arguments to a common type.

Template Parameters
ToType to promote to.
Parameters
lhsThe left hand side.
rhsThe right hand side.

◆ to_string

std::string to_string ( datum const & rhs)
friend

Get the string representation of the value.


The documentation for this class was generated from the following file: