jbson
C++11/1y BSON library
|
View on GitHub
BSON element. More...
#include <element.hpp>
Public Types | |
using | container_type = std::decay_t< Container > |
Underlying storage container. | |
Public Member Functions | |
basic_element ()=default | |
Default constructor. More... | |
template<typename OtherContainer > | |
basic_element (const basic_element< OtherContainer > &, std::enable_if_t< std::is_constructible< container_type, OtherContainer >::value > *=nullptr) | |
Copy constructor. More... | |
template<typename OtherContainer > | |
basic_element (const basic_element< OtherContainer > &, std::enable_if_t<!std::is_constructible< container_type, OtherContainer >::value > *=nullptr, std::enable_if_t< std::is_constructible< container_type, typename OtherContainer::const_iterator, typename OtherContainer::const_iterator >::value > *=nullptr) | |
Copy constructor. More... | |
template<typename OtherContainer > | |
basic_element (basic_element< OtherContainer > &&, std::enable_if_t< std::is_constructible< container_type, OtherContainer && >::value > *=nullptr) | |
Move constructor. More... | |
template<typename ForwardRange > | |
basic_element (ForwardRange &&, std::enable_if_t<!std::is_constructible< std::string, ForwardRange >::value > *=nullptr, std::enable_if_t< detail::is_range_of_same_value< ForwardRange, typename Container::value_type >::value > *=nullptr) | |
Construct an element from raw BSON byte sequence. More... | |
template<typename ForwardIterator > | |
basic_element (ForwardIterator &&first, ForwardIterator &&last, std::enable_if_t< !std::is_constructible< boost::string_ref, ForwardIterator >::value||std::is_convertible< ForwardIterator, typename container_type::const_iterator >::value > *=nullptr, std::enable_if_t< detail::is_range_of_same_value< decltype(boost::make_iterator_range(first, last)), typename Container::value_type >::value > *=nullptr) | |
Construct an element from raw BSON byte sequence. More... | |
template<typename T > | |
basic_element (std::string name, element_type type, T &&val) | |
Construct an element with specified name, type and value. More... | |
template<typename ForwardIterator > | |
basic_element (std::string, element_type, ForwardIterator, ForwardIterator) | |
Construct an element with specified name, type and value data. More... | |
basic_element (std::string, element_type=element_type::null_element) | |
Construct an element with specified name, and optionally, type. More... | |
template<typename T > | |
basic_element (std::string name, T &&val) | |
Construct an element with specified name and value. More... | |
boost::string_ref | name () const noexcept(std::is_nothrow_constructible< boost::string_ref, const std::string & >::value) |
Returns name of element. More... | |
void | name (std::string n) |
Sets name to n. More... | |
template<typename OutContainer > | |
operator OutContainer () const | |
bool | operator!= (const basic_element &other) const |
Checks if this and other are not equal. | |
bool | operator< (const basic_element &other) const |
Checks if this is less than (<) other . More... | |
bool | operator== (const basic_element &other) const |
Checks if this and other are equal. | |
size_t | size () const noexcept |
Returns size in bytes. | |
void | swap (basic_element &other) noexcept |
Swaps contents with other . | |
element_type | type () const noexcept |
Returns BSON type of this element. | |
void | type (element_type new_type) |
Sets type of this element. More... | |
template<typename T > | |
std::enable_if_t < detail::is_valid_element_value_type < container_type, T >::value, T > | value () const |
Returns the value data in the form of a specific type. More... | |
template<typename T > | |
std::enable_if_t <!detail::is_valid_element_value_type < container_type, T >::value, T > | value () const |
Returns the value data in the form of a specific type. More... | |
template<typename T > | |
void | value (T &&val, std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Sets value from a detail::TypeMap compatible type. More... | |
template<typename T > | |
void | value (T &&val, std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Sets value from a user-defined type. More... | |
template<typename T > | |
void | value (element_type new_type, T &&val, std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Sets element_type, and value from a detail::TypeMap compatible type. More... | |
template<typename T > | |
void | value (element_type new_type, T &&val, std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Sets element_type, and value from a user-defined type. More... | |
template<element_type EType, typename T > | |
void | value (T &&val, std::enable_if_t< std::is_same< std::decay_t< T >, detail::ElementTypeMapSet< EType, container_type >>::value > *=nullptr) |
Sets value. Statically ensures type compatibility. More... | |
template<element_type EType, typename T > | |
void | value (T &&val, std::enable_if_t<!std::is_same< std::decay_t< T >, detail::ElementTypeMapSet< EType, container_type >>::value > *=nullptr) |
Sets value. Statically ensures type compatibility. More... | |
template<typename Visitor > | |
void | visit (Visitor &&, std::enable_if_t< std::is_void< decltype(std::declval< Visitor >()("", std::declval< element_type >(), std::declval< double >()))>::value > *=nullptr) const |
Apply the visitor pattern with a void-return visitor. More... | |
template<typename Visitor > | |
auto | visit (Visitor &&, std::enable_if_t<!std::is_void< decltype(std::declval< Visitor >()("", std::declval< element_type >(), std::declval< double >()))>::value > *=nullptr) const -> decltype(std::declval< Visitor >()("", std::declval< element_type >(), std::declval< double >())) |
Apply the visitor pattern with a value-returning visitor. More... | |
template<typename OutContainer > | |
void | write_to_container (OutContainer &, typename OutContainer::const_iterator) const |
Transforms basic_element to BSON data. More... | |
Static Public Member Functions | |
static void | write_to_container (container_type &, typename container_type::const_iterator, boost::string_ref, element_type) |
Constructs a BSON element without data, in-place into a container. More... | |
template<typename T > | |
static void | write_to_container (container_type &, typename container_type::const_iterator, boost::string_ref, T &&) |
Constructs a type-deduced BSON element in-place into a container. More... | |
template<typename T > | |
static void | write_to_container (container_type &, typename container_type::const_iterator, boost::string_ref, element_type, T &&, std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Constructs a BSON element in-place, from compatible data, into a container. More... | |
template<typename T > | |
static void | write_to_container (container_type &, typename container_type::const_iterator, boost::string_ref, element_type, T &&, std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *=nullptr) |
Constructs a BSON element in-place, from incompatible data, into a container. More... | |
Friends | |
template<typename > | |
struct | basic_element |
BSON element.
Container | Type of the underlying data storage. Must be range of char . Must be detectably noexcept swappable. |
Currently stores the name and type separately from the value data. This is to simplify the implementation and the exception safety of it.
However, should the storage be unified it would eliminate std::string copies for all non-data-owning elements. It would also change the semantics to be more reasonable. Modifying the name of an element does not change the name in the underlying storage, and should thus be disallowed. The only way to enforce this is by using const basic_elements everywhere, or perhaps some tricky metaprogramming, or unified storage.
Definition at line 69 of file element.hpp.
|
default |
Default constructor.
Resultant basic_element represents a valid, empty-named, element_type::null_element typed BSON element.
jbson::basic_element< Container >::basic_element | ( | const basic_element< OtherContainer > & | elem, |
std::enable_if_t< std::is_constructible< container_type, OtherContainer >::value > * | = nullptr |
||
) |
Copy constructor.
Copies the contents of a basic_element with a compatible container_type.
elem | Element to be copied. |
Definition at line 678 of file element.hpp.
jbson::basic_element< Container >::basic_element | ( | const basic_element< OtherContainer > & | elem, |
std::enable_if_t<!std::is_constructible< container_type, OtherContainer >::value > * | = nullptr , |
||
std::enable_if_t< std::is_constructible< container_type, typename OtherContainer::const_iterator, typename OtherContainer::const_iterator >::value > * | = nullptr |
||
) |
Copy constructor.
Copies the contents of a basic_element with an incompatible container_type.
elem | Element to be copied. |
Does this via container_type(begin(other), end(other))
.
Definition at line 689 of file element.hpp.
jbson::basic_element< Container >::basic_element | ( | basic_element< OtherContainer > && | elem, |
std::enable_if_t< std::is_constructible< container_type, OtherContainer && >::value > * | = nullptr |
||
) |
Move constructor.
Moves the contents of a basic_element with a compatible container_type.
elem | Element to be moved. |
Definition at line 702 of file element.hpp.
|
explicit |
Construct an element from raw BSON byte sequence.
range | Range of bytes representing BSON data. |
invalid_element_size | When range is too small. When value data is indicated to be larger than range. |
invalid_element_type | When the detected element_type is invalid. |
Definition at line 715 of file element.hpp.
|
inline |
Construct an element from raw BSON byte sequence.
first | Iterator to beginning of data. |
last | Iterator past end of data. |
invalid_element_size | When range is too small. When value data is indicated to be larger than range. |
invalid_element_type | When the detected element_type is invalid. |
Definition at line 118 of file element.hpp.
|
inline |
Construct an element with specified name, type and value.
invalid_element_type | When supplied type is invalid. |
incompatible_type_conversion | When type is value-less, e.g. null_element. |
Definition at line 132 of file element.hpp.
jbson::basic_element< Container >::basic_element | ( | std::string | name, |
element_type | type, | ||
ForwardIterator | first, | ||
ForwardIterator | last | ||
) |
Construct an element with specified name, type and value data.
name | Name of the element. |
type | Type of element the supplied data represents. |
first | Iterator to start of data. |
last | Iterator past the end of data. |
invalid_element_type | When the supplied element_type is invalid. |
Definition at line 747 of file element.hpp.
|
explicit |
Construct an element with specified name, and optionally, type.
name | Element name. |
type | Element type. Defaults to element_type::null_element. |
invalid_element_type | When the supplied element_type is invalid. |
Definition at line 759 of file element.hpp.
|
inline |
Construct an element with specified name and value.
Attempts type deduction to determine suitable element_type.
name | Element name. |
val | JSON compatible value. |
incompatible_type_conversion | When type deduction fails. |
Definition at line 151 of file element.hpp.
|
inlinenoexcept |
Returns name of element.
Definition at line 160 of file element.hpp.
|
inline |
Sets name to n.
[in] | n | Name to set element to. |
n
construction could throw). Definition at line 170 of file element.hpp.
|
explicit |
Explicit conversion to a write_to_container compatible container.
Definition at line 666 of file element.hpp.
|
inline |
Checks if this is less than (<) other
.
When the elements have differing types, simply compares names.
Only when both the types and names are equal are the values compared.
When the values are element_type::string_element, they are fetched and then compared according to the current global locale.
When the values are element_type::double_element, they are fetched and compared with <
.
For all other types, a lexicographical comparison of the data is performed.
Definition at line 475 of file element.hpp.
|
inline |
Sets type of this element.
new_type | Valid BSON element_type. |
invalid_element_type | When type is invalid. |
Definition at line 188 of file element.hpp.
|
inline |
Returns the value data in the form of a specific type.
T | The value type to be returned. Must be default constructible. |
T
. incompatible_type_conversion | When ReturnT is incompatible with the current element_type. |
Definition at line 206 of file element.hpp.
|
inline |
Returns the value data in the form of a specific type.
T | The value type to be returned. Must be default constructible. |
T
. incompatible_type_conversion | When ReturnT is incompatible with the current element_type. |
Definition at line 236 of file element.hpp.
|
inline |
Sets value from a detail::TypeMap compatible type.
Statically deduces element_type to closest match from detail::TypeMap. Forwards to value<element_type>(val).
val | Value to set. |
invalid_element_size | When detected size differs from size of data. |
Definition at line 255 of file element.hpp.
|
inline |
Sets value from a user-defined type.
Forwards to a user-supplied value_set function found by argument-dependent lookup (ADL).
val | Value to set. |
Definition at line 274 of file element.hpp.
|
inline |
Sets element_type, and value from a detail::TypeMap compatible type.
new_type | element_type of supplied value. |
val | Value to set. |
new_type
is correct type for val
. val
to the type as determined by detail::ElementTypeMapSet and new_type
.invalid_element_size | When the size of the data differs from that detected. |
incompatible_type_conversion | When val's type is incompatible with new_type . |
invalid_element_type | When type is invalid. |
Definition at line 305 of file element.hpp.
|
inline |
Sets element_type, and value from a user-defined type.
Forwards to a user-supplied value_set function found by argument-dependent lookup (ADL).
new_type | element_type of supplied value. |
val | Value to set. |
invalid_element_size | When the size of the data differs from that detected. |
invalid_element_type | When new_type is invalid. |
Definition at line 332 of file element.hpp.
|
inline |
Sets value. Statically ensures type compatibility.
Does not explicitly perform any type conversions.
EType | element_type to set and check against. |
val | Value to set. |
invalid_element_size | When the size of the data differs from that detected. |
Definition at line 374 of file element.hpp.
|
inline |
Sets value. Statically ensures type compatibility.
Performs conversion to type mapped from detail::ElementTypeMapSet.
EType | element_type to set and check against. |
val | Value to set. |
invalid_element_size | When the size of the data differs from that detected. |
Definition at line 411 of file element.hpp.
void jbson::basic_element< Container >::visit | ( | Visitor && | visitor, |
std::enable_if_t< std::is_void< decltype(std::declval< Visitor >()("", std::declval< element_type >(), std::declval< double >()))>::value > * | = nullptr |
||
) | const |
Apply the visitor pattern with a void-return visitor.
Apply the visitor pattern on the element, based on its element_type.
Will pass the element's value, or nothing when the value type is void, to a supplied functor which returns void
. This value is retrieved as if via get(), so the functor must accept all variations of:
visitor | Functor that should accept multiple signatures. |
invalid_element_type | When type is invalid. |
Definition at line 982 of file element.hpp.
auto jbson::basic_element< Container >::visit | ( | Visitor && | visitor, |
std::enable_if_t<!std::is_void< decltype(std::declval< Visitor >()("", std::declval< element_type >(), std::declval< double >()))>::value > * | = nullptr |
||
) | const -> decltype(std::declval<Visitor>()("", std::declval<element_type>(), std::declval<double>())) |
Apply the visitor pattern with a value-returning visitor.
Apply the visitor pattern on the element, based on its element_type.
Will pass the element's value, or nothing when the value type is void, to a supplied functor which returns some non-void type. This value is retrieved as if via get(), so the functor must accept all variations of:
visitor | Functor that should accept multiple signatures. |
invalid_element_type | When type is invalid. |
Definition at line 1006 of file element.hpp.
|
static |
Constructs a BSON element without data, in-place into a container.
c | Container to write to. |
it | Position in c to insert data. |
name | Name of element. |
type | Type of element. |
invalid_element_type | When type is invalid |
incompatible_type_conversion | When type is non-void, i.e. should contain data. |
Definition at line 649 of file element.hpp.
|
static |
Constructs a type-deduced BSON element in-place into a container.
Accepts and performs type deduction only for types compatible with detail::TypeMap.
c | Container to write to. |
it | Position in c to insert data. |
name | Name of element. |
val | Value of element. |
Definition at line 562 of file element.hpp.
|
static |
Constructs a BSON element in-place, from compatible data, into a container.
c | Container to write to. |
it | Position in c to insert data. |
name | Name of element. |
type | Type of element. |
val | Value of element. |
invalid_element_type | When type is invalid |
incompatible_type_conversion | When type is void, i.e. should not contain data. |
Definition at line 594 of file element.hpp.
|
static |
Constructs a BSON element in-place, from incompatible data, into a container.
c | Container to write to. |
it | Position in c to insert data. |
name | Name of element. |
type | Type of element. |
val | Value of element. |
Constructs a basic_element to allow usage of value_set, and calls e.write_to_container(c
, it
).
invalid_element_type | When type is invalid. |
incompatible_type_conversion | When type is void, i.e. should not contain data. |
invalid_element_size | When the size of the data differs from that detected. |
Definition at line 630 of file element.hpp.
void jbson::basic_element< Container >::write_to_container | ( | OutContainer & | c, |
typename OutContainer::const_iterator | it | ||
) | const |
Transforms basic_element to BSON data.
[out] | c | Container to write to. |
it | Position in c to insert data. |
invalid_element_type | When type of this basic_element is invalid. |
invalid_element_size | When the detected size of the data differs from the actual size. |
Definition at line 532 of file element.hpp.