HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Friends
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 datumoperator= (datum const &other) noexcept
 
constexpr datumoperator= (datum &&other) noexcept
 
constexpr datumoperator= (std::floating_point auto value) noexcept(sizeof(value)<=4)
 
constexpr datumoperator= (numeric_integral auto value) noexcept(sizeof(value)<=4)
 
constexpr datumoperator= (bool value) noexcept
 
constexpr datumoperator= (std::chrono::year_month_day value) noexcept
 
constexpr datumoperator= (std::monostate) noexcept
 
constexpr datumoperator= (nullptr_t) noexcept
 
datumoperator= (std::string value) noexcept
 
datumoperator= (char const *value) noexcept
 
datumoperator= (std::string_view value) noexcept
 
datumoperator= (vector_type value) noexcept
 
datumoperator= (map_type value) noexcept
 
datumoperator= (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 consttype_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 constback () const
 
constexpr datumback ()
 
constexpr datum constfront () const
 
constexpr datumfront ()
 
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.
 
datumfind_one (jsonpath const &path) noexcept
 Find a object by path.
 
datumfind_one_or_create (jsonpath const &path) noexcept
 Find a object by path potentially creating intermediate objects.
 
datum constfind_one (jsonpath const &path) const noexcept
 Find a object by path.
 
datum constoperator[] (datum const &rhs) const
 
constexpr datumoperator[] (datum const &rhs)
 
constexpr datum constoperator[] (auto const &rhs) const
 
constexpr datumoperator[] (auto const &rhs)
 
constexpr datumoperator++ ()
 
constexpr datumoperator-- ()
 
constexpr datum operator++ (int)
 
constexpr datum operator-- (int)
 
constexpr datumoperator+= (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 To >
constexpr bool promotable_to (datum const &rhs) noexcept
 Check if the type held by the datum can be promoted.
 
template<typename T >
constexpr T const & get (datum const &rhs) noexcept
 Get the value of a datum.
 
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 constget_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 constget_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
inlinenoexcept

Find a object by path.

Parameters
pathThe 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)
inlinenoexcept

Find a object by path.

Parameters
pathThe 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)
inlinenoexcept

Find a object by path potentially creating intermediate objects.

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

◆ is_break()

constexpr bool hi::v1::datum::is_break ( ) const
inlineconstexprnoexcept

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

See also
skeleton_node

◆ is_continue()

constexpr bool hi::v1::datum::is_continue ( ) const
inlineconstexprnoexcept

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

See also
skeleton_node

◆ is_undefined()

constexpr bool hi::v1::datum::is_undefined ( ) const
inlineconstexprnoexcept

Check if the datum has an undefined value.

◆ items()

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

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

◆ keys()

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

Get the sorted list of keys of a map.

◆ remove()

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

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
pathA json path to remove.
Returns
true if one or more objects where removed.

◆ values()

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

Get the list of values of a map.

Friends And Related Symbol Documentation

◆ get [1/2]

template<typename T >
constexpr 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 >
constexpr T const & get ( datum const & rhs)
friend

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 >
constexpr 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.
pathThe json-path to the value to extract.
Returns
A pointer to the value, or nullptr.

◆ get_if [3/4]

template<typename T >
constexpr 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.
pathThe json-path to the value to extract.
Returns
A pointer to the value, or nullptr.

◆ holds_alternative

template<typename T >
constexpr 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

◆ promotable_to

template<typename To >
constexpr bool promotable_to ( 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.

◆ promote_if

template<typename To >
constexpr 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: