userver: utils::Box< T > Class Template Reference
Loading...
Searching...
No Matches
utils::Box< T > Class Template Reference

#include <userver/utils/box.hpp>

Detailed Description

template<typename T>
class utils::Box< T >

Remote storage for a single item. Implemented as a unique pointer that is never null, except when moved from.

Has the semantics of non-optional T. Copies the content on copy, compares by the contained value.

Use in the following cases:

  • to create recursive types while maintaining value semantics;
  • to hide the implementation of a class in cpp;
  • to prevent the large size or alignment of a field from inflating the size or alignment of an object.

Use utils::UniqueRef instead:

  • to add a non-movable field to a movable object;
  • to own an object of a polymorphic base class.

Usage example:

struct RecursionStarterPack {
std::string structs;
std::string strings;
utils::Box<PackSection> whats_in_this_section;
};
struct PackSection {
std::string box;
std::optional<RecursionStarterPack> here_we_go_again;
};
TEST(UtilsBox, Sample) {
PackSection pack{
"box",
{{
"structs",
"strings",
"box",
std::nullopt,
}},
}},
};
// Classic value semantics test.
auto another_pack = pack;
EXPECT_EQ(another_pack, pack);
another_pack.here_we_go_again->structs = "modified structs";
EXPECT_NE(another_pack, pack);
EXPECT_EQ(pack.here_we_go_again->structs, "structs");
}

Definition at line 56 of file box.hpp.

Public Member Functions

 Box ()
 Allocate a default-constructed value.
 
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_convertible, U &&, T >(), int > = 0>
 Box (U &&arg)
 Allocate a T, copying or moving arg.
 
template<typename... Args, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, Args... >, std::is_constructible, T, Args &&... >(), int > = 0>
 Box (Args &&... args)
 Allocate the value, emplacing it with the given args.
 
 Box (Box &&other) noexcept=default
 
Boxoperator= (Box &&other) noexcept=default
 
 Box (const Box &other)
 
Boxoperator= (const Box &other)
 
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_constructible, T, U >(), std::is_assignable, T &, U >(), int > = 0>
Boxoperator= (U &&other)
 Assigns-through to the contained value.
 
 operator bool () const =delete
 
T * operator-> () noexcept
 
const T * operator-> () const noexcept
 
T & operator* () noexcept
 
const T & operator* () const noexcept
 
bool operator== (const Box &other) const
 
bool operator!= (const Box &other) const
 
bool operator< (const Box &other) const
 
bool operator> (const Box &other) const
 
bool operator<= (const Box &other) const
 
bool operator>= (const Box &other) const
 

Static Public Member Functions

template<typename Factory >
static Box MakeWithFactory (Factory &&factory)
 

Constructor & Destructor Documentation

◆ Box() [1/4]

template<typename T >
utils::Box< T >::Box ( )
inline

Allocate a default-constructed value.

Definition at line 61 of file box.hpp.

◆ Box() [2/4]

template<typename T >
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_convertible, U &&, T >(), int > = 0>
utils::Box< T >::Box ( U && arg)
inline

Allocate a T, copying or moving arg.

Definition at line 74 of file box.hpp.

◆ Box() [3/4]

template<typename T >
template<typename... Args, std::enable_if_t< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, Args... >, std::is_constructible, T, Args &&... >(), int > = 0>
utils::Box< T >::Box ( Args &&... args)
inlineexplicit

Allocate the value, emplacing it with the given args.

Definition at line 86 of file box.hpp.

◆ Box() [4/4]

template<typename T >
utils::Box< T >::Box ( const Box< T > & other)
inline

Definition at line 99 of file box.hpp.

Member Function Documentation

◆ MakeWithFactory()

template<typename T >
template<typename Factory >
static Box utils::Box< T >::MakeWithFactory ( Factory && factory)
inlinestatic

Allocate the value as constructed by the given factory. Allows to save an extra move of the contained value.

Definition at line 92 of file box.hpp.

◆ operator!=()

template<typename T >
bool utils::Box< T >::operator!= ( const Box< T > & other) const
inline

Definition at line 137 of file box.hpp.

◆ operator*() [1/2]

template<typename T >
const T & utils::Box< T >::operator* ( ) const
inlinenoexcept

Definition at line 133 of file box.hpp.

◆ operator*() [2/2]

template<typename T >
T & utils::Box< T >::operator* ( )
inlinenoexcept

Definition at line 132 of file box.hpp.

◆ operator->() [1/2]

template<typename T >
const T * utils::Box< T >::operator-> ( ) const
inlinenoexcept

Definition at line 130 of file box.hpp.

◆ operator->() [2/2]

template<typename T >
T * utils::Box< T >::operator-> ( )
inlinenoexcept

Definition at line 129 of file box.hpp.

◆ operator<()

template<typename T >
bool utils::Box< T >::operator< ( const Box< T > & other) const
inline

Definition at line 139 of file box.hpp.

◆ operator<=()

template<typename T >
bool utils::Box< T >::operator<= ( const Box< T > & other) const
inline

Definition at line 143 of file box.hpp.

◆ operator=() [1/2]

template<typename T >
Box & utils::Box< T >::operator= ( const Box< T > & other)
inline

Definition at line 101 of file box.hpp.

◆ operator=() [2/2]

template<typename T >
template<typename U = T, std::enable_if_t< impl::ConjunctionWithTrait< impl::ConjunctionWithTrait< impl::kArgsAreNotSelf< Box, U >, std::is_constructible, T, U >(), std::is_assignable, T &, U >(), int > = 0>
Box & utils::Box< T >::operator= ( U && other)
inline

Assigns-through to the contained value.

Definition at line 117 of file box.hpp.

◆ operator==()

template<typename T >
bool utils::Box< T >::operator== ( const Box< T > & other) const
inline

Definition at line 135 of file box.hpp.

◆ operator>()

template<typename T >
bool utils::Box< T >::operator> ( const Box< T > & other) const
inline

Definition at line 141 of file box.hpp.

◆ operator>=()

template<typename T >
bool utils::Box< T >::operator>= ( const Box< T > & other) const
inline

Definition at line 145 of file box.hpp.


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