Doxygen
Variant< Ts > 模板结构体 参考

Implementation of a variant container (similar to C++17's std::variant). 更多...

#include <variant.h>

+ 类 Variant< Ts > 继承关系图:

Public 成员函数

 Variant ()
 The default constructor 更多...
 
 Variant (const Variant< Ts... > &src)
 The copy constructor 更多...
 
 Variant (Variant< Ts... > &&src)
 The move constructor 更多...
 
Variant< Ts... > & operator= (const Variant< Ts... > &src)
 The copy assignment operator 更多...
 
Variant< Ts... > & operator= (Variant< Ts... > &&src)
 The move assignment operator 更多...
 
 ~Variant ()
 The destructor 更多...
 
template<uint8_t index>
constexpr bool is () const
 Returns true iff the variant container holds a specific type. 更多...
 
constexpr bool valid () const
 Returns true iff the Variant holds a valid type. 更多...
 
void invalidate ()
 Invalidate the variant. Will destroy any object that is held. 更多...
 
constexpr uint8_t index () const
 Returns the index of the type held by this variant, or invalid_id() if the variant does not hold any type (i.e. 更多...
 
template<uint8_t index, typename... Args>
void set (Args &&... args)
 Replaces the contents of the variant container by constructing a type T calling the constructor with Args 更多...
 
template<uint8_t index>
Type< index > & get ()
 Return a non-constant reference to the value held by the variant container. 更多...
 
template<uint8_t index>
const Type< index > & get () const
 Returns a constant reference to the value held by the variant container. 更多...
 

Private 类型

using Data = typename std::aligned_storage< data_size, data_align >::type
 the data type for the Variant's internal memory 更多...
 
using HelperT = details::HelperT< Ts... >
 a short hand name for the helper class 更多...
 
template<uint8_t index>
using Type = typename VariantType< index, Ts... >::type
 

静态 Private 成员函数

static uint8_t invalid_id ()
 The id that represents an invalid type 更多...
 

Private 属性

Data m_data
 the actual data 更多...
 
uint8_t m_id
 a unique identifier for the type held by this variant 更多...
 

静态 Private 属性

static const size_t data_size = details::TMax<sizeof(Ts)...>::value
 constant respresenting the maximum size that can hold all types in the template list 更多...
 
static const size_t data_align = details::TMax<alignof(Ts)...>::value
 constant respresenting the maximum alignment requirement for all types in the template list 更多...
 

详细描述

template<typename... Ts>
struct Variant< Ts >

Implementation of a variant container (similar to C++17's std::variant).

It can hold either no instances (e.g. initially or after calling invalidate()), or hold exactly one instance of an object (after calling set()) whose type is one of the variant's template parameters. Each parameter has an index, the first parameter has index 0. It behaves similar to a C union, in that the memory of all possible object types is shared, but unlike a C union it does allow C++ objects with constructors and destructors to be stored and knows what type is stored.

在文件 variant.h176 行定义.

成员类型定义说明

◆ Data

template<typename... Ts>
using Variant< Ts >::Data = typename std::aligned_storage<data_size, data_align>::type
private

the data type for the Variant's internal memory

在文件 variant.h184 行定义.

◆ HelperT

template<typename... Ts>
using Variant< Ts >::HelperT = details::HelperT<Ts...>
private

a short hand name for the helper class

在文件 variant.h187 行定义.

◆ Type

template<typename... Ts>
template<uint8_t index>
using Variant< Ts >::Type = typename VariantType<index,Ts...>::type
private

在文件 variant.h190 行定义.

构造及析构函数说明

◆ Variant() [1/3]

template<typename... Ts>
Variant< Ts >::Variant ( )
inline

The default constructor

在文件 variant.h202 行定义.

202  : m_id(invalid_id())
203  {
204  }

◆ Variant() [2/3]

template<typename... Ts>
Variant< Ts >::Variant ( const Variant< Ts... > &  src)
inline

The copy constructor

在文件 variant.h207 行定义.

207  : m_id(src.m_id)
208  {
209  HelperT::copy(src.m_id, &src.m_data, &m_data);
210  }

◆ Variant() [3/3]

template<typename... Ts>
Variant< Ts >::Variant ( Variant< Ts... > &&  src)
inline

The move constructor

在文件 variant.h213 行定义.

213  : m_id(src.m_id)
214  {
215  HelperT::move(src.m_id, &src.m_data, &m_data);
216  }

◆ ~Variant()

template<typename... Ts>
Variant< Ts >::~Variant ( )
inline

The destructor

在文件 variant.h244 行定义.

245  {
247  }

成员函数说明

◆ get() [1/2]

template<typename... Ts>
template<uint8_t index>
Type<index>& Variant< Ts >::get ( )
inline

Return a non-constant reference to the value held by the variant container.

异常
std::bad_cast()if called on a variant container that does not hold an instance of the type of the variant at index.

在文件 variant.h285 行定义.

286  {
287  if (m_id != index) throw std::bad_cast();
288  return *reinterpret_cast<Type<index>*>(&m_data);
289  }

被这些函数引用 TemplateVariant::call(), TemplateVariant::operator==(), TemplateVariant::toBool(), TemplateVariant::toInt(), TemplateVariant::toList(), TemplateVariant::toString() , 以及 TemplateVariant::toStruct().

◆ get() [2/2]

template<typename... Ts>
template<uint8_t index>
const Type<index>& Variant< Ts >::get ( ) const
inline

Returns a constant reference to the value held by the variant container.

异常
std::bad_cast()if called on a variant container that does not hold an instance of the type of the variant at index.

在文件 variant.h295 行定义.

296  {
297  if (m_id != index) throw std::bad_cast();
298  return *reinterpret_cast<const Type<index>*>(&m_data);
299  }

◆ index()

◆ invalid_id()

template<typename... Ts>
static uint8_t Variant< Ts >::invalid_id ( )
inlinestaticprivate

◆ invalidate()

template<typename... Ts>
void Variant< Ts >::invalidate ( )
inline

Invalidate the variant. Will destroy any object that is held.

在文件 variant.h258 行定义.

259  {
261  m_id = invalid_id();
262  }

◆ is()

template<typename... Ts>
template<uint8_t index>
constexpr bool Variant< Ts >::is ( ) const
inlineconstexpr

Returns true iff the variant container holds a specific type.

模板参数
Tthe type to search for.

在文件 variant.h252 行定义.

252 { return m_id==index; }

被这些函数引用 TemplateVariant::isBool(), TemplateVariant::isFunction(), TemplateVariant::isInt(), TemplateVariant::isList(), TemplateVariant::isString(), TemplateVariant::isStruct() , 以及 TemplateVariant::isWeakStruct().

◆ operator=() [1/2]

template<typename... Ts>
Variant<Ts...>& Variant< Ts >::operator= ( const Variant< Ts... > &  src)
inline

The copy assignment operator

在文件 variant.h219 行定义.

220  {
221  if (this!=&src) // prevent self assignment
222  {
223  // destroy the old value
225  // and copy over the new one
226  m_id = src.m_id;
227  HelperT::copy(src.m_id, &src.m_data, &m_data);
228  }
229  return *this;
230  }

◆ operator=() [2/2]

template<typename... Ts>
Variant<Ts...>& Variant< Ts >::operator= ( Variant< Ts... > &&  src)
inline

The move assignment operator

在文件 variant.h233 行定义.

234  {
235  // destroy the old value
237  // and move in the new one
238  m_id = src.m_id;
239  HelperT::move(src.m_id, &src.m_data, &m_data);
240  return *this;
241  }

◆ set()

template<typename... Ts>
template<uint8_t index, typename... Args>
void Variant< Ts >::set ( Args &&...  args)
inline

Replaces the contents of the variant container by constructing a type T calling the constructor with Args

模板参数
indexthe type to make the variant hold an instance of.
ArgsThe arguments types to pass to the constructor of T.
参数
argsThe argument values

在文件 variant.h274 行定义.

275  {
277  m_id = index;
278  new (&m_data) Type<index>(std::forward<Args>(args)...);
279  }

被这些函数引用 TemplateVariant::TemplateVariant().

◆ valid()

template<typename... Ts>
constexpr bool Variant< Ts >::valid ( ) const
inlineconstexpr

类成员变量说明

◆ data_align

template<typename... Ts>
const size_t Variant< Ts >::data_align = details::TMax<alignof(Ts)...>::value
staticprivate

constant respresenting the maximum alignment requirement for all types in the template list

在文件 variant.h181 行定义.

◆ data_size

template<typename... Ts>
const size_t Variant< Ts >::data_size = details::TMax<sizeof(Ts)...>::value
staticprivate

constant respresenting the maximum size that can hold all types in the template list

在文件 variant.h179 行定义.

◆ m_data

◆ m_id


该结构体的文档由以下文件生成:
Variant::index
constexpr uint8_t index() const
Returns the index of the type held by this variant, or invalid_id() if the variant does not hold any ...
Definition: variant.h:266
Variant::valid
constexpr bool valid() const
Returns true iff the Variant holds a valid type.
Definition: variant.h:255
Variant::invalid_id
static uint8_t invalid_id()
The id that represents an invalid type
Definition: variant.h:193
details::HelperT::copy
static void copy(uint8_t id, const void *src_v, void *dst_v)
Definition: variant.h:137
Variant::m_id
uint8_t m_id
a unique identifier for the type held by this variant
Definition: variant.h:198
details::HelperT::move
static void move(uint8_t id, void *src_v, void *dst_v)
Definition: variant.h:139
details::HelperT::destroy
static void destroy(uint8_t id, void *data)
Definition: variant.h:141
Variant::m_data
Data m_data
the actual data
Definition: variant.h:196