Go to the documentation of this file.
9 #ifndef EAGINE_VALUE_TREE_WRAPPERS_HPP
10 #define EAGINE_VALUE_TREE_WRAPPERS_HPP
12 #include "../assert.hpp"
13 #include "../callable_ref.hpp"
14 #include "../memory/span_algo.hpp"
15 #include "../reflect/enumerators.hpp"
16 #include "../type_identity.hpp"
17 #include "../valid_if/decl.hpp"
42 : _owner{std::move(temp._owner)}
43 , _pimpl{temp._pimpl} {
44 temp._pimpl =
nullptr;
50 , _pimpl{that._pimpl} {
51 if(_owner && _pimpl) {
52 _owner->add_ref(*_pimpl);
58 if(
this != std::addressof(temp)) {
60 swap(_owner, temp._owner);
61 swap(_pimpl, temp._pimpl);
68 if(
this != std::addressof(that)) {
73 if(_owner && _pimpl) {
74 _owner->add_ref(*_pimpl);
83 EAGINE_ASSERT(_owner);
84 _owner->release(*_pimpl);
89 explicit operator bool()
const {
90 return _owner && _pimpl;
103 if(_owner && _pimpl) {
104 return _owner->attribute_name(*_pimpl);
113 std::shared_ptr<compound_interface> owner,
115 : _owner{std::move(owner)}
118 std::shared_ptr<compound_interface> _owner{};
119 attribute_interface* _pimpl{
nullptr};
122 class compound_attribute;
124 template <
typename T>
125 struct not_converted_value {
126 constexpr not_converted_value(T& dest) noexcept
129 constexpr
auto dest() noexcept -> auto& {
133 template <
identifier_t V>
134 constexpr
auto apply(selector<V>)
const noexcept {
142 template <
typename T>
143 struct converted_enum_value {
144 static_assert(has_enumerator_mapping_v<T>);
147 constexpr converted_enum_value(T& dest) noexcept
150 constexpr
auto dest() noexcept -> auto& {
154 template <
identifier_t V>
155 auto apply(selector<V> sel)
const {
156 if(
auto converted{
from_string(_temp, type_identity<T>(), sel)}) {
168 template <
typename T>
169 struct converted_value
170 : std::conditional_t<
171 has_enumerator_mapping_v<T>,
172 converted_enum_value<T>,
173 not_converted_value<T>> {
175 using base = std::conditional_t<
176 has_enumerator_mapping_v<T>,
177 converted_enum_value<T>,
178 not_converted_value<T>>;
184 struct converted_value<std::chrono::duration<float>>
185 : not_converted_value<std::chrono::duration<float>> {
186 using base = not_converted_value<std::chrono::duration<float>>;
190 template <
typename R,
typename P>
191 class converted_value<std::chrono::duration<R, P>> {
192 using T = std::chrono::duration<R, P>;
195 constexpr converted_value(T& dest) noexcept
198 constexpr
auto dest() noexcept -> auto& {
202 template <
identifier_t V>
203 auto apply(selector<V>)
const {
204 _dest = std::chrono::duration_cast<T>(_temp);
209 std::chrono::duration<float> _temp{};
231 template <
typename Compound,
typename... Args>
232 static auto make(Args&&... args) -> std::
233 enable_if_t<std::is_base_of_v<compound_interface, Compound>,
compound> {
234 return {Compound::make_shared(std::forward<Args>(args)...)};
238 explicit operator bool() const noexcept {
245 return _pimpl->type_id();
257 return {_pimpl, _pimpl->structure()};
269 if(_pimpl && attrib._pimpl) {
270 return _pimpl->attribute_name(*attrib._pimpl);
282 if(_pimpl && attrib._pimpl) {
283 return _pimpl->canonical_type(*attrib._pimpl);
291 if(_pimpl && attrib._pimpl) {
292 return _pimpl->is_link(*attrib._pimpl);
303 if(_pimpl && attrib._pimpl) {
304 return _pimpl->nested_count(*attrib._pimpl);
320 if(_pimpl && attrib._pimpl) {
321 return {_pimpl, _pimpl->nested(*attrib._pimpl, index)};
331 if(_pimpl && attrib._pimpl) {
332 return {_pimpl, _pimpl->nested(*attrib._pimpl, name)};
350 if(_pimpl && attrib._pimpl) {
351 return {_pimpl, _pimpl->find(*attrib._pimpl, path)};
363 span<const string_view> tags)
const ->
attribute {
364 if(_pimpl && attrib._pimpl) {
365 return {_pimpl, _pimpl->find(*attrib._pimpl, path, tags)};
387 if(_pimpl && attrib._pimpl) {
388 return _pimpl->value_count(*attrib._pimpl);
404 template <
typename T>
407 if(_pimpl && attrib._pimpl) {
409 dest, _pimpl->fetch_values(*attrib._pimpl, offset, dest));
415 template <
typename T>
419 span<T> dest)
const -> span<T> {
424 template <
typename T>
431 template <
typename T>
437 template <
typename T>
444 template <
typename T>
468 template <
typename T,
identifier_t V>
474 converted_value<T> conv{dest};
475 if(!
fetch_values(attrib, offset, cover_one(conv.dest())).empty()) {
476 return conv.apply(sel);
482 template <
typename T>
489 template <
typename T,
identifier_t V>
496 for(T& elem : dest) {
502 return head(dest, index);
506 template <
typename T,
identifier_t V>
514 template <
typename T>
521 template <
typename T,
identifier_t V>
531 template <
typename T>
538 template <
typename T,
identifier_t V>
545 template <
typename T>
551 template <
typename T,
identifier_t V>
558 template <
typename T,
identifier_t V>
566 template <
typename T>
572 template <
typename T,
identifier_t V>
580 template <
typename T>
586 template <std::
size_t L>
597 template <
typename T,
identifier_t V>
605 return {std::move(temp),
true};
611 template <
typename T>
620 template <
typename T,
identifier_t V>
628 return {std::move(temp),
true};
634 template <
typename T>
643 template <
typename T,
identifier_t V>
649 return {std::move(temp),
true};
655 template <
typename T>
663 template <
typename T,
identifier_t V>
667 return get<T>(attrib, 0, tid, sel);
671 template <
typename T>
678 template <
typename T,
identifier_t V>
683 return get<T>(path, 0, tid, sel);
687 template <
typename T>
694 template <
typename T,
identifier_t V>
697 return get<T>(name, 0, tid, sel);
701 template <
typename T>
719 span<const attribute>)>;
725 compound(std::shared_ptr<compound_interface> pimpl) noexcept
726 : _pimpl{std::move(pimpl)} {}
728 std::shared_ptr<compound_interface> _pimpl{};
750 explicit operator bool() const noexcept {
791 return {_c, _c.
nested(_a, index)};
805 return {_c, _c.
find(_a, path)};
814 template <
typename T>
820 template <
typename T>
831 template <
typename T,
identifier_t V>
840 template <
typename T,
identifier_t V>
846 template <
typename T,
identifier_t V>
852 template <
typename T>
858 template <
typename T>
860 return _c.
get(_a, offset, tid);
864 template <
typename T>
866 return _c.
get(_a, tid);
882 return {std::move(c), std::move(a)};
887 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
888 #include <eagine/value_tree/wrappers.inl>
891 #endif // EAGINE_VALUE_TREE_WRAPPERS_HPP
auto fetch_value(const basic_string_path &path, span_size_t offset, T &dest) const -> bool
Fetches values through the specified path, into dest.
Definition: wrappers.hpp:532
auto value_count() const -> span_size_t
Returns the number of value elements accessible through an attribute.
Definition: wrappers.hpp:809
auto nested(span_size_t index) const -> compound_attribute
Returns nested attribute of an attribute at the specified index.
Definition: wrappers.hpp:790
auto get(const attribute &attrib, span_size_t offset, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at an attribute, at the given offset.
Definition: wrappers.hpp:612
auto is_link() const noexcept -> bool
Indicates if the specified attribute is a reference or link in the tree.
Definition: wrappers.hpp:765
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
auto canonical_type() const -> value_type
Returns the canonical value type of this attribute.
Definition: wrappers.hpp:770
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
auto canonical_type(const attribute &attrib) const -> value_type
Returns the caninical value type of an attribute.
Definition: wrappers.hpp:281
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
auto from_string(string_view src) noexcept
Converts the string representation in src to a value of type T.
Definition: from_string.hpp:360
static auto make(Args &&... args) -> std::enable_if_t< std::is_base_of_v< compound_interface, Compound >, compound >
Instantiates a particular implementation.
Definition: wrappers.hpp:232
auto select_value(T &dest, selector< V > sel) const -> bool
Fetches a value from this attribute, with selector.
Definition: wrappers.hpp:841
compound_attribute(compound c, attribute a) noexcept
Construction from a compound and attribute pair.
Definition: wrappers.hpp:743
auto select_value(const basic_string_path &path, span_size_t offset, T &dest, selector< V > sel) const -> bool
Fetches values through the specified path, with a selector, into dest.
Definition: wrappers.hpp:522
auto select_value(const attribute &attrib, T &dest, selector< V > sel) const -> bool
Fetches a value at the specified attribute, with a selector, into dest.
Definition: wrappers.hpp:552
auto nested_count(const attribute &attrib) const -> span_size_t
Returns the count of nested attributes of an attribute.
Definition: wrappers.hpp:302
auto fetch_values(span_size_t offset, span< T > dest) const
Fetches values from this attribute, starting at offset, into dest.
Definition: wrappers.hpp:815
auto fetch_value(const attribute &attrib, span_size_t offset, T &dest) const -> bool
Fetches a single value at the specified attribute, at offset into dest.
Definition: wrappers.hpp:483
auto root() const -> compound_attribute
Returns the structure root as an compound_attribute.
Definition: wrappers.hpp:874
auto fetch_values(span< T > dest) const
Fetches values from this attribute, into dest.
Definition: wrappers.hpp:821
auto structure() const -> attribute
Returns the root of attribute hierarchy describing the tree structure.
Definition: wrappers.hpp:255
attribute(attribute &&temp) noexcept
Move constructor.
Definition: wrappers.hpp:41
auto fetch_blob(const basic_string_path &path, memory::block dest) const -> memory::block
Fetches a BLOB at the attribute with the specified path, into dest.
Definition: wrappers.hpp:456
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Primary template for conditionally valid values.
Definition: decl.hpp:49
Handle class for value tree compounds.
Definition: wrappers.hpp:223
auto find(const basic_string_path &path) const -> compound_attribute
Returns nested attribute of an attribute at the specified path.
Definition: wrappers.hpp:804
auto get(const attribute &attrib, span_size_t offset, type_identity< T >, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at an attribute, at offset, with selector.
Definition: wrappers.hpp:598
auto operator=(const attribute &that) -> attribute &
Copy assignment operator. Handles attribute reference counting.
Definition: wrappers.hpp:67
attribute() noexcept=default
Default constructor. Constructs empty attribute refering to nothing.
@ unknown
Unknown value type.
static constexpr auto head(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Returns the first l elements from the front of a span.
Definition: span_algo.hpp:99
auto get(string_view name, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at name.
Definition: wrappers.hpp:702
auto select_value(string_view name, T &dest, selector< V > sel) const -> bool
Fetches values through the specified name, with a selector, into dest.
Definition: wrappers.hpp:539
Template used to construct tag-types used mostly in tag-dispatching.
Definition: selector.hpp:21
auto operator=(attribute &&temp) noexcept -> attribute &
Move assignment operator.
Definition: wrappers.hpp:57
Basic class storing paths made of string elements.
Definition: string_path.hpp:27
auto fetch_values(string_view name, span< T > dest) const -> span< T >
Fetches values at the attribute with the specified name, into dest.
Definition: wrappers.hpp:445
auto nested(string_view name) const -> compound_attribute
Returns nested attribute of an attribute with the specified name.
Definition: wrappers.hpp:797
void traverse(visit_handler visitor)
Traverses the tree, calls the visitor function on each node.
auto fetch_value(T &dest) const -> bool
Fetches a value from this attribute, into dest.
Definition: wrappers.hpp:853
auto select_values(const attribute &attrib, span< T > dest, selector< V > sel) const -> span< T >
Fetches values at the specified attribute, with a selector, into dest.
Definition: wrappers.hpp:560
auto value_count(const attribute &attrib) const -> span_size_t
Returns the number of value elements accessible through an attribute.
Definition: wrappers.hpp:386
auto nested_count() const -> span_size_t
Returns the count of nested attributes of an attribute.
Definition: wrappers.hpp:778
compound() noexcept=default
Default constructor. Constructs an empty compound.
auto find(const attribute &attrib, const basic_string_path &path, span< const string_view > tags) const -> attribute
Returns nested attribute of an attribute at path with tags.
Definition: wrappers.hpp:360
auto fetch_blob(string_view name, memory::block dest) const -> memory::block
Fetches a BLOB at the attribute with the specified name, into dest.
Definition: wrappers.hpp:462
virtual auto type_id() const noexcept -> identifier_t=0
Returns the implementation type identifier of this attribute.
auto select_value(const basic_string_path &path, T &dest, selector< V > sel) const -> bool
Fetches a value through the specified path, with selector, into dest.
Definition: wrappers.hpp:574
auto get(const basic_string_path &path, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at path.
Definition: wrappers.hpp:688
auto fetch_value(const attribute &attrib, T &dest) const -> bool
Fetches a single value at the specified attribute, into dest.
Definition: wrappers.hpp:567
auto is_link(const attribute &attrib) const -> bool
Indicates if the specified attribute is a reference or link in the tree.
Definition: wrappers.hpp:290
auto value_count(string_view name) -> span_size_t
Returns the number of value elements at attribute with the given name.
Definition: wrappers.hpp:399
auto fetch_values(const basic_string_path &path, span_size_t offset, span< T > dest) const -> span< T >
Fetches values at the given path, starting at offset into dest.
Definition: wrappers.hpp:416
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
auto get(const basic_string_path &path, span_size_t offset, type_identity< T >, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at path, at given offset, with selector.
Definition: wrappers.hpp:621
auto get(type_identity< T > tid={}) const
Returns a value of type T, from this attribute.
Definition: wrappers.hpp:865
auto fetch_blob(memory::block dest) const
Fetches a BLOB from this attribute, into dest.
Definition: wrappers.hpp:826
auto select_values(const attribute &attrib, span_size_t offset, span< T > dest, selector< V > sel) const -> span< T >
Fetches values at the specified attribute, with a selector, into dest.
Definition: wrappers.hpp:490
auto has_nested() const -> span_size_t
Indicates if an attribute has nested attribute accessible by index.
Definition: wrappers.hpp:783
Interface for value tree attribute implementations.
Definition: interface.hpp:72
~attribute() noexcept
Destructor. Handles attribute reference counting.
Definition: wrappers.hpp:81
auto fetch_values(const basic_string_path &path, span< T > dest) const -> span< T >
Fetches values at the attribute with the specified path, into dest.
Definition: wrappers.hpp:438
auto get(string_view name, span_size_t offset, type_identity< T >, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at name, at given offset, with selector.
Definition: wrappers.hpp:645
auto fetch_value(string_view name, T &dest) const -> bool
Fetches values through the specified name, into dest.
Definition: wrappers.hpp:546
auto attribute_name(const attribute &attrib) const -> string_view
Returns the name of an attribute.
Definition: wrappers.hpp:268
Handle class for value tree attributes.
Definition: wrappers.hpp:35
compound_attribute() noexcept=default
Default constructor. Constructs empty attribute refering to nothing.
auto get(string_view name, span_size_t offset, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at name, at given offset.
Definition: wrappers.hpp:657
auto find(const basic_string_path &path, span< const string_view > tags) const -> attribute
Returns nested attribute of root attribute at path with tags.
Definition: wrappers.hpp:380
auto find(const basic_string_path &path) const -> attribute
Returns nested attribute of root attribute at the specified path.
Definition: wrappers.hpp:373
auto get(const basic_string_path &path, type_identity< T > tid, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at path, with selector.
Definition: wrappers.hpp:679
auto select_value(const attribute &attrib, span_size_t offset, T &dest, selector< V > sel) const -> bool
Fetches a single value at the specified attribute, with a selector.
Definition: wrappers.hpp:469
auto select_values(span< T > dest, selector< V > sel) const -> span< T >
Fetches a value from this attribute, with selector, into dest.
Definition: wrappers.hpp:847
auto select_value(string_view name, span_size_t offset, T &dest, selector< V > sel) const -> bool
Fetches values through the specified name, with a selector, into dest.
Definition: wrappers.hpp:508
auto get(const attribute &attrib, type_identity< T > tid, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at an attribute, with selector.
Definition: wrappers.hpp:665
Combination of a reference to a tree compound and a single attribute.
Definition: wrappers.hpp:736
auto fetch_values(string_view name, span_size_t offset, span< T > dest) const -> span< T >
Fetches values at the given name, starting at offset into dest.
Definition: wrappers.hpp:425
auto type_id() const noexcept
Returns the shared implementation type id of the attribute and compound.
Definition: wrappers.hpp:755
auto get(string_view name, type_identity< T > tid, selector< V > sel) const -> optionally_valid< T >
Returns the value of type T at name, with selector.
Definition: wrappers.hpp:695
auto type_id() const noexcept -> identifier_t
Returns the implementation type id of this attribute.
Definition: wrappers.hpp:94
auto get(const attribute &attrib, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at an attribute.
Definition: wrappers.hpp:672
auto name() const -> string_view
Returns the implementation type id of this attribute.
Definition: wrappers.hpp:102
std::uint64_t identifier_t
The underlying integer type for eagine::identifier.
Definition: identifier_t.hpp:19
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
auto nested(const attribute &attrib, span_size_t index) const -> attribute
Returns nested attribute of an attribute at the specified index.
Definition: wrappers.hpp:319
auto get(const basic_string_path &path, span_size_t offset, type_identity< T > tid={}) const -> optionally_valid< T >
Returns the value of type T at path, at given offset.
Definition: wrappers.hpp:635
auto has_nested(const attribute &attrib) const -> bool
Indicates if an attribute has nested attribute accessible by index.
Definition: wrappers.hpp:311
auto fetch_value(const basic_string_path &path, T &dest) const -> bool
Fetches a value through the specified path, into dest.
Definition: wrappers.hpp:581
auto value_count(const basic_string_path &path) const -> span_size_t
Returns the number of value elements at the specified path.
Definition: wrappers.hpp:394
auto nested(const attribute &attrib, string_view name) const -> attribute
Returns nested attribute of an attribute with the specified name.
Definition: wrappers.hpp:330
auto fetch_blob(const attribute &attrib, memory::block dest) const -> memory::block
Fetches a BLOB at the given attribute, into dest.
Definition: wrappers.hpp:450
auto fetch_values(const attribute &attrib, span_size_t offset, span< T > dest) const -> span< T >
Fetches values at the given attribute, starting at offset into dest.
Definition: wrappers.hpp:405
attribute(const attribute &that)
Copy constructor. Handles attribute reference counting.
Definition: wrappers.hpp:48
auto fetch_value(string_view name, span_size_t offset, T &dest) const -> bool
Fetches values through the specified name, into dest.
Definition: wrappers.hpp:515
static auto operator/(compound c, attribute a) noexcept -> compound_attribute
Operator for creating a compound_attribute from compound and attribute.
Definition: wrappers.hpp:880
static constexpr auto starts_with(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > with) -> bool
Indicates if span spn starts with the content of with.
Definition: span_algo.hpp:170
auto nested(string_view name) const -> attribute
Returns nested attribute of the root attribute with the specified name.
Definition: wrappers.hpp:340
auto find(const attribute &attrib, const basic_string_path &path) const -> attribute
Returns nested attribute of an attribute at the specified path.
Definition: wrappers.hpp:348
auto has_value(const attribute &attrib, const char(&what)[L]) const -> bool
Tests if there is an value at an attribute, that starts with what.
Definition: wrappers.hpp:587
Value-tree code is placed in this namespace.
Definition: eagine.hpp:53
auto name() const noexcept -> string_view
Returns the name of this attribute.
Definition: wrappers.hpp:760
auto fetch_values(const attribute &attrib, span< T > dest) const -> span< T >
Fetches values at the given attribute, into dest.
Definition: wrappers.hpp:432
auto get(span_size_t offset, type_identity< T > tid={}) const
Returns a value of type T, from this attribute, at offset.
Definition: wrappers.hpp:859
callable_ref< bool(compound &, const attribute &, const basic_string_path &)> visit_handler
Type of traverse/visit handler.
Definition: wrappers.hpp:709
auto fetch_value(span_size_t offset, T &dest, selector< V > sel=default_selector) const -> bool
Fetches a value from this attribute, starting at offset, with selector.
Definition: wrappers.hpp:832
auto type_id() const noexcept -> identifier_t
Returns the implementation type id of this attribute.
Definition: wrappers.hpp:243