View on GitHub
jbson
C++11/1y BSON library
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
jbson::basic_element< Container > Struct Template Reference

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
 

Detailed Description

template<class Container>
struct jbson::basic_element< Container >

BSON element.

Template Parameters
ContainerType 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.

Constructor & Destructor Documentation

template<class Container>
jbson::basic_element< Container >::basic_element ( )
default

Default constructor.

Resultant basic_element represents a valid, empty-named, element_type::null_element typed BSON element.

template<class Container >
template<typename OtherContainer >
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.

Parameters
elemElement to be copied.

Definition at line 678 of file element.hpp.

template<class Container >
template<typename OtherContainer >
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.

Parameters
elemElement to be copied.

Does this via container_type(begin(other), end(other)).

Definition at line 689 of file element.hpp.

template<class Container >
template<typename OtherContainer >
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.

Parameters
elemElement to be moved.

Definition at line 702 of file element.hpp.

template<class Container >
template<typename ForwardRange >
jbson::basic_element< Container >::basic_element ( ForwardRange &&  range,
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 
)
explicit

Construct an element from raw BSON byte sequence.

Parameters
rangeRange of bytes representing BSON data.
Exceptions
invalid_element_sizeWhen range is too small. When value data is indicated to be larger than range.
invalid_element_typeWhen the detected element_type is invalid.

Definition at line 715 of file element.hpp.

template<class Container>
template<typename ForwardIterator >
jbson::basic_element< Container >::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 
)
inline

Construct an element from raw BSON byte sequence.

Parameters
firstIterator to beginning of data.
lastIterator past end of data.
Exceptions
invalid_element_sizeWhen range is too small. When value data is indicated to be larger than range.
invalid_element_typeWhen the detected element_type is invalid.

Definition at line 118 of file element.hpp.

template<class Container>
template<typename T >
jbson::basic_element< Container >::basic_element ( std::string  name,
element_type  type,
T &&  val 
)
inline

Construct an element with specified name, type and value.

Exceptions
invalid_element_typeWhen supplied type is invalid.
incompatible_type_conversionWhen type is value-less, e.g. null_element.

Definition at line 132 of file element.hpp.

template<class Container >
template<typename ForwardIterator >
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.

Parameters
nameName of the element.
typeType of element the supplied data represents.
firstIterator to start of data.
lastIterator past the end of data.
Warning
This constructor bypasses any type checking of the data.
Exceptions
invalid_element_typeWhen the supplied element_type is invalid.

Definition at line 747 of file element.hpp.

template<class Container >
jbson::basic_element< Container >::basic_element ( std::string  name,
element_type  type = element_type::null_element 
)
explicit

Construct an element with specified name, and optionally, type.

Parameters
nameElement name.
typeElement type. Defaults to element_type::null_element.
Exceptions
invalid_element_typeWhen the supplied element_type is invalid.

Definition at line 759 of file element.hpp.

template<class Container>
template<typename T >
jbson::basic_element< Container >::basic_element ( std::string  name,
T &&  val 
)
inline

Construct an element with specified name and value.

Attempts type deduction to determine suitable element_type.

Parameters
nameElement name.
valJSON compatible value.
Exceptions
incompatible_type_conversionWhen type deduction fails.

Definition at line 151 of file element.hpp.

Member Function Documentation

template<class Container>
boost::string_ref jbson::basic_element< Container >::name ( ) const
inlinenoexcept

Returns name of element.

Returns
boost::string_ref. The basic_element must remain alive at least as long as the returned string.

Definition at line 160 of file element.hpp.

template<class Container>
void jbson::basic_element< Container >::name ( std::string  n)
inline

Sets name to n.

Parameters
[in]nName to set element to.
Warning
Strong exception guarantee (parameter n construction could throw).

Definition at line 170 of file element.hpp.

template<class Container >
template<typename OutContainer >
jbson::basic_element< Container >::operator OutContainer ( ) const
explicit

Explicit conversion to a write_to_container compatible container.

See also
write_to_container

Definition at line 666 of file element.hpp.

template<class Container>
bool jbson::basic_element< Container >::operator< ( const basic_element< Container > &  other) const
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.

template<class Container>
void jbson::basic_element< Container >::type ( element_type  new_type)
inline

Sets type of this element.

Note
No checks are performed determining if the current value data is compatible with the new type.
Parameters
new_typeValid BSON element_type.
Exceptions
invalid_element_typeWhen type is invalid.
Warning
Strong exception guarantee.

Definition at line 188 of file element.hpp.

template<class Container>
template<typename T >
std::enable_if_t<detail::is_valid_element_value_type<container_type, T>::value, T> jbson::basic_element< Container >::value ( ) const
inline

Returns the value data in the form of a specific type.

Note
This overload only returns types from detail::ElementTypeMap.
Template Parameters
TThe value type to be returned. Must be default constructible.
Returns
Value represented by this basic_element in the form of a T.
Exceptions
incompatible_type_conversionWhen ReturnT is incompatible with the current element_type.
See also
detail::ElementTypeMap

Definition at line 206 of file element.hpp.

template<class Container>
template<typename T >
std::enable_if_t<!detail::is_valid_element_value_type<container_type, T>::value, T> jbson::basic_element< Container >::value ( ) const
inline

Returns the value data in the form of a specific type.

Note
This overload requires a compatible value_get function overload. It is found via ADL.
Template Parameters
TThe value type to be returned. Must be default constructible.
Returns
Value represented by this basic_element in the form of a T.
Exceptions
incompatible_type_conversionWhen ReturnT is incompatible with the current element_type.
See also
value_get

Definition at line 236 of file element.hpp.

template<class Container>
template<typename T >
void jbson::basic_element< Container >::value ( T &&  val,
std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
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).

Parameters
valValue to set.
Warning
Strong exception guarantee.
Exceptions
invalid_element_sizeWhen detected size differs from size of data.

Definition at line 255 of file element.hpp.

template<class Container>
template<typename T >
void jbson::basic_element< Container >::value ( T &&  val,
std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
inline

Sets value from a user-defined type.

Forwards to a user-supplied value_set function found by argument-dependent lookup (ADL).

Parameters
valValue to set.
Warning
Strong exception guarantee.
See also
value_set

Definition at line 274 of file element.hpp.

template<class Container>
template<typename T >
void jbson::basic_element< Container >::value ( element_type  new_type,
T &&  val,
std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
inline

Sets element_type, and value from a detail::TypeMap compatible type.

Parameters
new_typeelement_type of supplied value.
valValue to set.
Note
Does not perform type deduction. Assumes new_type is correct type for val.
Attempts to convert val to the type as determined by detail::ElementTypeMapSet and new_type.
Warning
Strong exception guarantee.
Exceptions
invalid_element_sizeWhen the size of the data differs from that detected.
incompatible_type_conversionWhen val's type is incompatible with new_type.
invalid_element_typeWhen type is invalid.

Definition at line 305 of file element.hpp.

template<class Container>
template<typename T >
void jbson::basic_element< Container >::value ( element_type  new_type,
T &&  val,
std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
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).

Parameters
new_typeelement_type of supplied value.
valValue to set.
Warning
Strong exception guarantee.
Exceptions
invalid_element_sizeWhen the size of the data differs from that detected.
invalid_element_typeWhen new_type is invalid.

Definition at line 332 of file element.hpp.

template<class Container>
template<element_type EType, typename T >
void jbson::basic_element< Container >::value ( T &&  val,
std::enable_if_t< std::is_same< std::decay_t< T >, detail::ElementTypeMapSet< EType, container_type >>::value > *  = nullptr 
)
inline

Sets value. Statically ensures type compatibility.

Does not explicitly perform any type conversions.

Template Parameters
ETypeelement_type to set and check against.
Parameters
valValue to set.
Warning
Strong exception guarantee.
Exceptions
invalid_element_sizeWhen the size of the data differs from that detected.
See also
detail::ElementTypeMapSet detail::serialise()

Definition at line 374 of file element.hpp.

template<class Container>
template<element_type EType, typename T >
void jbson::basic_element< Container >::value ( T &&  val,
std::enable_if_t<!std::is_same< std::decay_t< T >, detail::ElementTypeMapSet< EType, container_type >>::value > *  = nullptr 
)
inline

Sets value. Statically ensures type compatibility.

Performs conversion to type mapped from detail::ElementTypeMapSet.

Template Parameters
ETypeelement_type to set and check against.
Parameters
valValue to set.
Warning
Strong exception guarantee.
Exceptions
invalid_element_sizeWhen the size of the data differs from that detected.
See also
detail::ElementTypeMapSet detail::serialise()

Definition at line 411 of file element.hpp.

template<class Container >
template<typename Visitor >
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(boost::string_ref, element_type) // for void types, e.g. element_type::null_element
visitor(boost::string_ref, element_type, detail::ElementTypeMap<type>) // for all other element_type
Parameters
visitorFunctor that should accept multiple signatures.
Exceptions
invalid_element_typeWhen type is invalid.
See also
detail::ElementTypeMap

Definition at line 982 of file element.hpp.

template<class Container >
template<typename Visitor >
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(boost::string_ref, element_type) // for void types, e.g. element_type::null_element
visitor(boost::string_ref, element_type, detail::ElementTypeMap<type>) // for all other element_type
Parameters
visitorFunctor that should accept multiple signatures.
Exceptions
invalid_element_typeWhen type is invalid.
See also
detail::ElementTypeMap

Definition at line 1006 of file element.hpp.

template<typename Container >
void jbson::basic_element< Container >::write_to_container ( container_type c,
typename container_type::const_iterator  it,
boost::string_ref  name,
element_type  type 
)
static

Constructs a BSON element without data, in-place into a container.

Parameters
cContainer to write to.
itPosition in c to insert data.
nameName of element.
typeType of element.
Warning
Basic exception guarantee.
Exceptions
invalid_element_typeWhen type is invalid
incompatible_type_conversionWhen type is non-void, i.e. should contain data.

Definition at line 649 of file element.hpp.

template<typename Container >
template<typename T >
void jbson::basic_element< Container >::write_to_container ( container_type c,
typename container_type::const_iterator  it,
boost::string_ref  name,
T &&  val 
)
static

Constructs a type-deduced BSON element in-place into a container.

Accepts and performs type deduction only for types compatible with detail::TypeMap.

Parameters
cContainer to write to.
itPosition in c to insert data.
nameName of element.
valValue of element.
Warning
Basic exception guarantee.

Definition at line 562 of file element.hpp.

template<typename Container >
template<typename T >
void jbson::basic_element< Container >::write_to_container ( container_type c,
typename container_type::const_iterator  it,
boost::string_ref  name,
element_type  type,
T &&  val,
std::enable_if_t< detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
static

Constructs a BSON element in-place, from compatible data, into a container.

Parameters
cContainer to write to.
itPosition in c to insert data.
nameName of element.
typeType of element.
valValue of element.
Warning
Basic exception guarantee.
Exceptions
invalid_element_typeWhen type is invalid
incompatible_type_conversionWhen type is void, i.e. should not contain data.

Definition at line 594 of file element.hpp.

template<typename Container >
template<typename T >
void jbson::basic_element< Container >::write_to_container ( container_type c,
typename container_type::const_iterator  it,
boost::string_ref  name,
element_type  type,
T &&  val,
std::enable_if_t<!detail::is_valid_element_set_type< container_type, T >::value > *  = nullptr 
)
static

Constructs a BSON element in-place, from incompatible data, into a container.

Parameters
cContainer to write to.
itPosition in c to insert data.
nameName of element.
typeType of element.
valValue of element.

Constructs a basic_element to allow usage of value_set, and calls e.write_to_container(c, it).

Warning
Basic exception guarantee.
Exceptions
invalid_element_typeWhen type is invalid.
incompatible_type_conversionWhen type is void, i.e. should not contain data.
invalid_element_sizeWhen the size of the data differs from that detected.

Definition at line 630 of file element.hpp.

template<class Container >
template<typename OutContainer >
void jbson::basic_element< Container >::write_to_container ( OutContainer &  c,
typename OutContainer::const_iterator  it 
) const

Transforms basic_element to BSON data.

Parameters
[out]cContainer to write to.
itPosition in c to insert data.
Exceptions
invalid_element_typeWhen type of this basic_element is invalid.
invalid_element_sizeWhen the detected size of the data differs from the actual size.
Warning
Basic exception guarantee.

Definition at line 532 of file element.hpp.


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