All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::utility::any Class Reference

A class that contains a type erased value. More...

Public Member Functions

 any () noexcept=default
 Constructs any with no value. More...
 
template<typename T >
 any (T &&actual) noexcept
 Constructs any with a value. More...
 
 any (const any &other)
 Copy constructor.
 
 any (any &&other)
 Move constructor. Must not throw.
 
anyoperator= (const any &other)
 Copy assignment.
 
anyoperator= (any &&other)
 Move assignment. The value of other is removed.
 
 operator bool () const
 Checks if any is in a proper state. More...
 
template<typename T >
bool is () const
 Checks if the value of any is a certain type. More...
 
template<typename T >
const Decayed< T > & as () const
 Casts any to access the contained value. More...
 
template<typename T >
Decayed< T > & as ()
 

Related Functions

(Note that these are not member functions.)

template<typename T >
auto any_cast (const any &object) -> decltype(object.as< T >())
 Helper function to cast any. More...
 

Detailed Description

any is a class that can contain most type of variables assuming they are copy constructible. This is similar to other dynamic typing languages where you could assign any type to a variable. The closest C++ alternative to this behaviour would be using a void* but any does this in a safer manner without having void pointers.

Example:

#include <gears/utility/any.hpp>
#include <iostream>
#include <vector>
namespace util = gears::utility;
int main() {
std::vector<util::any> stuff = { "hello", "world", '!', 1.0f, 200.0, 10 };
for(auto&& i : stuff) {
if(i.is<const char*>()) {
std::cout << i.as<const char*>() << ' ';
}
if(i.is<char>()) {
std::cout << i.as<char>() << '\n';
}
if(i.is<int>()) {
std::cout << i.as<int>() * 150 << '\n';
}
}
}

Output:

hello world !
1500

Definition at line 82 of file any.hpp.

Constructor & Destructor Documentation

gears::utility::any::any ( )
defaultnoexcept

Constructs any with no value. Accessing it with its empty state is undefined behaviour.

template<typename T >
gears::utility::any::any ( T &&  actual)
inlinenoexcept

Constructs any with a value. After construction, any holds the value contained and can be accessed with is with the type of the variable provided. The type passed must be copy constructible or else a compiler error will be thrown.

Parameters
actualValue to construct any with.

Definition at line 128 of file any.hpp.

Member Function Documentation

template<typename T >
const Decayed<T>& gears::utility::any::as ( ) const
inline

Casts any to access the contained value. In order to check if the type is valid use is.

Template Parameters
TType to cast to.
Exceptions
gears::utility::bad_any_castThrown if the type is invalid.
Returns
The value contained if they type is valid.

Definition at line 200 of file any.hpp.

template<typename T >
bool gears::utility::any::is ( ) const
inline

Checks if the value of any is a certain type. A specific gotcha with this function is thinking the value of a string literal is std::string, it is actually const char*. This function should be called before calling as.

Template Parameters
TType to check for.
Returns
true if the internal value is of type T, false otherwise.

Definition at line 184 of file any.hpp.

gears::utility::any::operator bool ( ) const
inlineexplicit

Checks if any is in a proper state, i.e. it contain a value.

Returns
true is any contains a value, false otherwise.

Definition at line 168 of file any.hpp.

Friends And Related Function Documentation

template<typename T >
auto any_cast ( const any object) -> decltype(object.as<T>())
related

Helper function to cast any to another type.

Parameters
objectThe object to cast.
Template Parameters
TThe type to cast to.
Exceptions
gears::utility::bad_any_castThrown if the type is invalid.
Returns
The value contained in the object if the type is valid.

Definition at line 228 of file any.hpp.