qi::AnyValue¶
The class qi::AnyValue
is a dynamic value which can contain anything. It
works like a normal variable and allows you to store any type and get it back,
but also use the variable without converting it back to its real type.
Summary¶
class qi::AnyValue
Functions (class qi::AnyValue)
-
qi::detail::AnyReferenceBase::operator[]
qi::detail::AnyReferenceBase::asInt16
qi::detail::AnyReferenceBase::asString
qi::detail::AnyReferenceBase::convertCopy
qi::detail::AnyReferenceBase::append
qi::detail::AnyReferenceBase::update
qi::detail::AnyReferenceBase::setOptional
qi::detail::AnyReferenceBase::asFloat
qi::detail::AnyReferenceBase::ptr
qi::detail::AnyReferenceBase::destroy
qi::detail::AnyReferenceBase::resetOptional
qi::detail::AnyReferenceBase::asInt32
qi::detail::AnyReferenceBase::setString
qi::detail::AnyReferenceBase::membersType
qi::detail::AnyReferenceBase::toObject
qi::detail::AnyReferenceBase::end
qi::detail::AnyReferenceBase::from
qi::detail::AnyReferenceBase::toUInt
qi::detail::AnyReferenceBase::asUInt64
qi::detail::AnyReferenceBase::setFloat
qi::detail::AnyReferenceBase::setTuple
qi::detail::AnyReferenceBase::toList
qi::detail::AnyReferenceBase::setUInt
qi::detail::AnyReferenceBase::asRaw
qi::detail::AnyReferenceBase::type
qi::detail::AnyReferenceBase::signature
qi::detail::AnyReferenceBase::rawValue
qi::detail::AnyReferenceBase::setDouble
qi::detail::AnyReferenceBase::fromPtr
qi::detail::AnyReferenceBase::setDynamic
qi::detail::AnyReferenceBase::toOptional
qi::detail::AnyReferenceBase::element
qi::detail::AnyReferenceBase::toTuple
qi::detail::AnyReferenceBase::isValue
qi::detail::AnyReferenceBase::kind
qi::detail::AnyReferenceBase::set
qi::detail::AnyReferenceBase::as
qi::detail::AnyReferenceBase::at
qi::detail::AnyReferenceBase::asUInt8
qi::detail::AnyReferenceBase::asInt64
qi::detail::AnyReferenceBase::clone
qi::detail::AnyReferenceBase::to
qi::detail::AnyReferenceBase::asUInt32
qi::detail::AnyReferenceBase::toString
qi::detail::AnyReferenceBase::toMap
qi::detail::AnyReferenceBase::asListValuePtr
qi::detail::AnyReferenceBase::toInt
qi::detail::AnyReferenceBase::content
qi::detail::AnyReferenceBase::insert
qi::detail::AnyReferenceBase::asMapValuePtr
qi::detail::AnyReferenceBase::asOptional
qi::detail::AnyReferenceBase::asTupleValuePtr
qi::detail::AnyReferenceBase::asUInt16
qi::detail::AnyReferenceBase::isValid
qi::detail::AnyReferenceBase::begin
qi::detail::AnyReferenceBase::asDouble
qi::detail::AnyReferenceBase::toDouble
qi::detail::AnyReferenceBase::size
qi::detail::AnyReferenceBase::operator*
qi::detail::AnyReferenceBase::find
qi::detail::AnyReferenceBase::optionalHasValue
qi::detail::AnyReferenceBase::unwrap
qi::detail::AnyReferenceBase::setInt
qi::detail::AnyReferenceBase::asInt8
qi::detail::AnyReferenceBase::convert
qi::detail::AnyReferenceBase::setRaw
qi::detail::AnyReferenceBase::toFloat
Global Classes
Detailed Description¶
Storing and retrieving a value¶
Storing and retrieving a value is easy enough:
#include <qi/anyvalue.hpp>
struct MyStruct {
int canard;
};
MyStruct s;
s.canard = 12;
// this creates a copy and stores it in value
qi::AnyValue value = qi::AnyValue::from(s);
MyStruct& s2 = value.as<MyStruct>();
std::cout << s2.canard << std::endl; // 12
// would throw
//value.as<int>();
Warning
When you use as()
, you must specify the exact same type that was passed to
the AnyValue, you can’t store an int
and retrieve an unsigned int
.
Note
As you can see, the type does not need to be registered in the type system for this to work.
If you don’t know the exact type stored, you can still use to()
which
supports standard conversions:
// type can be changed
value = qi::AnyValue::from(42.3);
std::cout << value.to<int>() << std::endl; // 42
Using the value with standard types¶
You can use a value without extracting its contents when it’s built from standard types like built-in types, vectors, maps. It also supports common operators.
std::vector<int> v;
v.push_back(12);
v.push_back(24);
v.push_back(42);
// creates a copy
qi::AnyValue value = qi::AnyValue::from(v);
std::cout << value[2].as<int>() << std::endl; // 42
Using the value with registered types¶
The same thing can be achieved with registered types.
Graph::Point p(12, 42);
qi::AnyValue value = qi::AnyValue::from(p);
// you can't use names (x and y) here because structs are registered as
// tuples with annotations in the type system
std::cout << value[0].as<int>() << ", " << value[1].as<int>()
<< std::endl; // 12, 42
Reference¶
qi::AnyValue Class Reference¶
Introduction¶
#include <qi/anyvalue.hpp>
- Inherits:
qi::detail::AnyReferenceBase
- Inherited by:
qi::AnyIterator
Public Functions¶
-
()AnyValue
-
(const AnyValue& b)AnyValue
-
(const AnyReference& b, bool copy, bool free)AnyValue
-
(const AutoAnyReference& b)AnyValue
-
(qi::TypeInterface* type)AnyValue
-
AnyReference
()release
-
()~AnyValue
-
AnyValue&
(const AnyReference& b)operator=
-
AnyValue&
(const AnyValue& b)operator=
-
void
()reset
-
void
(qi::TypeInterface* type)reset
- template<typename T>
-
void
(const T& t)set
-
void
(const AnyReference& src)reset
-
void
(const AnyReference& src, bool copy, bool free)reset
-
void
(AnyValue& b)swap
-
AnyReference
() constasReference
-
UniqueAnyReference
(TypeInterface* targetType) constconvert
-
UniqueAnyReference
(ListTypeInterface* targetType) constconvert
-
UniqueAnyReference
(StructTypeInterface* targetType) constconvert
-
UniqueAnyReference
(MapTypeInterface* targetType) constconvert
-
UniqueAnyReference
(IntTypeInterface* targetType) constconvert
-
UniqueAnyReference
(FloatTypeInterface* targetType) constconvert
-
UniqueAnyReference
(RawTypeInterface* targetType) constconvert
-
UniqueAnyReference
(StringTypeInterface* targetType) constconvert
-
UniqueAnyReference
(PointerTypeInterface* targetType) constconvert
-
UniqueAnyReference
(DynamicTypeInterface* targetType) constconvert
-
UniqueAnyReference
(OptionalTypeInterface* targetType) constconvert
- template<typename T>
-
T*
(bool check)ptr
-
bool
() constisValid
-
bool
() constisValue
-
AnyReference
(TypeInterface* targetType) constconvertCopy
-
AnyReference
() constclone
-
void
()destroy
- template<typename T>
-
T
() constto
- template<typename T>
-
T
(const T&) constto
-
int64_t
() consttoInt
-
uint64_t
() consttoUInt
-
float
() consttoFloat
-
double
() consttoDouble
-
std::string
() consttoString
- template<typename T>
-
std::vector<T>
() consttoList
- template<typename K, typename V>
-
std::map<K, V>
() consttoMap
-
AnyObject
() consttoObject
- template<typename T>
-
boost::optional<T>
() consttoOptional
-
AnyValue
(bool homogeneous) consttoTuple
- template<typename T>
-
T&
()as
-
int64_t&
()asInt64
-
uint64_t&
()asUInt64
-
int32_t&
()asInt32
-
uint32_t&
()asUInt32
-
int16_t&
()asInt16
-
uint16_t&
()asUInt16
-
int8_t&
()asInt8
-
uint8_t&
()asUInt8
-
double&
()asDouble
-
float&
()asFloat
-
std::string&
()asString
-
std::pair<char*, size_t>
() constasRaw
-
boost::optional<AnyReference>
() constasOptional
-
AnyReference
() constcontent
-
AnyReferenceVector
()asTupleValuePtr
-
AnyReferenceVector
()asListValuePtr
-
std::map<AnyReference, AnyReference>
()asMapValuePtr
- template<typename K>
-
AnyReference
(const K& key)operator[]
-
AnyReference
(const AnyReference& key)operator[]
- template<typename E, typename K>
-
E&
(const K& key)element
- template<typename K>
-
AnyReference
(const K& key)at
- template<typename K>
-
AnyReference
(const K& key) constat
-
AnyReference
(const AnyReference& key)at
-
AnyReference
(const AnyReference& key) constat
-
size_t
() constsize
-
bool
() constoptionalHasValue
- template<typename T>
-
void
(const T& element)append
-
void
(const AnyReference& element)append
- template<typename K, typename V>
-
void
(const K& key, const V& val)insert
-
void
(const AnyReference& key, const AnyReference& val)insert
- template<typename K>
-
AnyReference
(const K& key)find
-
AnyIterator
() constbegin
-
AnyIterator
() constend
-
AnyReference
() constoperator*
-
qi::Signature
(bool resolveDynamic) constsignature
-
TypeKind
() constkind
-
AnyReference
() constunwrap
-
void
(const AutoAnyReference& b)update
- template<typename T>
-
void
(const T& val)set
-
void
(int64_t v)set
-
void
(int32_t v)set
-
void
(uint64_t v)set
-
void
(uint32_t v)set
-
void
(float v)set
-
void
(double v)set
-
void
(const std::string& v)set
-
void
(int64_t v)setInt
-
void
(uint64_t v)setUInt
-
void
(float v)setFloat
-
void
(double v)setDouble
-
void
(const std::string& v)setString
-
void
(const AnyReference& value)setDynamic
-
void
(const boost::optional<AnyReference>& opt)setOptional
-
void
(const AnyReference& opt)setOptional
-
void
(const char* buffer, size_t size)setRaw
-
void
(const AnyReferenceVector& values)setTuple
-
void
()resetOptional
-
TypeInterface*
() consttype
-
std::vector<TypeInterface*>
() constmembersType
-
void*
() constrawValue
-
void
() constto
Public Static Functions¶
-
AnyValue
(const AnyReferenceVector& values)makeTuple
-
AnyValue
(const AutoAnyReference& v0, const AutoAnyReference& v1, const AutoAnyReference& v2, const AutoAnyReference& v3, const AutoAnyReference& v4, const AutoAnyReference& v5, const AutoAnyReference& v6, const AutoAnyReference& v7, const AutoAnyReference& v8, const AutoAnyReference& v9)makeTupleFromValue
- template<typename T>
-
AnyValue
(const AnyReferenceVector& values)makeList
-
AnyValue
(const AnyReferenceVector& values)makeGenericList
- template<typename K, typename V>
-
AnyValue
(const std::map<AnyReference, AnyReference>& values)makeMap
-
AnyValue
(const std::map<AnyReference, AnyReference>& values)makeGenericMap
- template<typename T>
-
AnyValue
()make
-
AnyValue
()makeVoid
- template<typename T>
-
AnyValue
(const T& r)from
Detailed Description¶
Represent any value supported by the typesystem. when constructed or set the value is copied. as a pointer to the real value. to convert the value if needed and copy to the required type.
Functions Documentation¶
-
static AnyValue
qi::AnyValue::
makeTuple
(const AnyReferenceVector& values)¶ The following functions construct a AnyValue from containers of AnyReference.
-
static AnyValue
qi::AnyValue::
makeTupleFromValue
(const AutoAnyReference& v0, const AutoAnyReference& v1, const AutoAnyReference& v2, const AutoAnyReference& v3, const AutoAnyReference& v4, const AutoAnyReference& v5, const AutoAnyReference& v6, const AutoAnyReference& v7, const AutoAnyReference& v8, const AutoAnyReference& v9)¶
- template<typename T>
-
static AnyValue
qi::AnyValue::
makeList
(const AnyReferenceVector& values)¶
-
static AnyValue
qi::AnyValue::
makeGenericList
(const AnyReferenceVector& values)¶
- template<typename K, typename V>
-
static AnyValue
qi::AnyValue::
makeMap
(const std::map<AnyReference, AnyReference>& values)¶
-
static AnyValue
qi::AnyValue::
makeGenericMap
(const std::map<AnyReference, AnyReference>& values)¶
- template<typename T>
-
static AnyValue
qi::AnyValue::
make
()¶ Create and return a AnyValue of type T.
-
static AnyValue
qi::AnyValue::
makeVoid
()¶ Construct a void AnyValue: defined, but with no data.
- template<typename T>
-
static AnyValue
qi::AnyValue::
from
(const T& r)¶
-
qi::AnyValue::
AnyValue
()¶ A default-constructed AnyValue is invalid, and equivalent to an unset optional value. It cannot be converted to any other type, and will yield exceptions instead.
-
qi::AnyValue::
AnyValue
(const AnyValue& b)¶
-
qi::AnyValue::
AnyValue
(const AutoAnyReference& b)¶
-
qi::AnyValue::
AnyValue
(qi::TypeInterface* type)¶
-
AnyReference
qi::AnyValue::
release
()¶ Brief:
Returns: the contained value, and reset the AnyValue.
-
qi::AnyValue::
~AnyValue
()¶
-
AnyValue&
qi::AnyValue::
operator=
(const AnyReference& b)¶
-
AnyValue&
qi::AnyValue::
operator=
(const AnyValue& b)¶
-
AnyReference
qi::AnyValue::
asReference
()const
¶
- template<typename T>
-
static AnyReference
qi::detail::AnyReferenceBase::
from
(const T& ref)¶ Construction and assign. Wraps the given value into an AnyReference, allowing an introspectable, type-erased access to the value.
- template<typename T>
-
static AnyReference
qi::detail::AnyReferenceBase::
fromPtr
(const T* ptr)¶ Construct a AnyValue with storage pointing to ptr.
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(TypeInterface* targetType)const
¶ Attempts the conversion of the value behind the reference to the given type. Converted value is invalid if conversion failed.
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(ListTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(StructTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(MapTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(IntTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(FloatTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(RawTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(StringTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(PointerTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(DynamicTypeInterface* targetType)const
¶
-
UniqueAnyReference
qi::detail::AnyReferenceBase::
convert
(OptionalTypeInterface* targetType)const
¶
- template<typename T>
-
Brief:
Parameters: - check -- if false, does not validate type before converting
Returns: a pointer to the value as a T or 0 if value is not a T.
qi::detail::AnyReferenceBase::
ptr
(bool check = true)¶ Return the typed pointer behind a AnyReference. T must be the type of the value.
-
bool
qi::detail::AnyReferenceBase::
isValue
()const
¶ Brief:
Returns: true if value is valid and not void
-
AnyReference
qi::detail::AnyReferenceBase::
convertCopy
(TypeInterface* targetType)const
¶ Helper function that converts and always clone.
-
AnyReference
qi::detail::AnyReferenceBase::
clone
()const
¶
- template<typename T>
-
T
qi::detail::AnyReferenceBase::
to
(const T&)const
¶ Similar to previous method, but uses a dummy value to get the target type.
-
int64_t
qi::detail::AnyReferenceBase::
toInt
()const
¶
-
uint64_t
qi::detail::AnyReferenceBase::
toUInt
()const
¶
-
double
qi::detail::AnyReferenceBase::
toDouble
()const
¶
-
std::string
qi::detail::AnyReferenceBase::
toString
()const
¶
- template<typename T>
-
std::vector<T>
qi::detail::AnyReferenceBase::
toList
()const
¶
- template<typename K, typename V>
-
std::map<K, V>
qi::detail::AnyReferenceBase::
toMap
()const
¶
-
AnyObject
qi::detail::AnyReferenceBase::
toObject
()const
¶
- template<typename T>
-
boost::optional<T>
qi::detail::AnyReferenceBase::
toOptional
()const
¶
-
AnyValue
qi::detail::AnyReferenceBase::
toTuple
(bool homogeneous)const
¶ Brief:
Parameters: - homogeneous – if true, all tuple elements will be of the type of the list element type. If false, the effective type of elements of kind dynamic will be used.
Convert the value to a tuple. If value is currently a tuple, it will be returned. If value is a list its elements will become the tuple components.
- template<typename T>
-
Brief:
Returns: a typed reference to the underlying value qi::detail::AnyReferenceBase::
as
()¶ Read and update functions The following functions access or modify the existing value. They never change the storage location or type. They will fail by throwing an exception if the requested operation is incompatible with the current value type.
-
int64_t&
qi::detail::AnyReferenceBase::
asInt64
()¶
-
uint64_t&
qi::detail::AnyReferenceBase::
asUInt64
()¶
-
int32_t&
qi::detail::AnyReferenceBase::
asInt32
()¶
-
uint32_t&
qi::detail::AnyReferenceBase::
asUInt32
()¶
-
int16_t&
qi::detail::AnyReferenceBase::
asInt16
()¶
-
uint16_t&
qi::detail::AnyReferenceBase::
asUInt16
()¶
-
int8_t&
qi::detail::AnyReferenceBase::
asInt8
()¶
-
uint8_t&
qi::detail::AnyReferenceBase::
asUInt8
()¶
-
double&
qi::detail::AnyReferenceBase::
asDouble
()¶
-
std::string&
qi::detail::AnyReferenceBase::
asString
()¶
-
std::pair<char*, size_t>
qi::detail::AnyReferenceBase::
asRaw
()const
¶ Brief:
Returns: a pair of (char*, size) corresponding to the raw buffer. No copy made.
-
boost::optional<AnyReference>
qi::detail::AnyReferenceBase::
asOptional
()const
¶
-
AnyReference
qi::detail::AnyReferenceBase::
content
()const
¶ Brief:
Returns: contained AnyValue or throw if type is not dynamic.
-
AnyReferenceVector
qi::detail::AnyReferenceBase::
asTupleValuePtr
()¶ Container partial unboxing. The following functions unbox the container-part of the value. The values in the contairer are exposed as AnyReference. The values can be modified using the set and as function families, But the container itself is a copy.
-
AnyReferenceVector
qi::detail::AnyReferenceBase::
asListValuePtr
()¶
-
std::map<AnyReference, AnyReference>
qi::detail::AnyReferenceBase::
asMapValuePtr
()¶
- template<typename K>
-
AnyReference
qi::detail::AnyReferenceBase::
operator[]
(const K& key)¶ In-place container manipulation. Return a reference to container element at index or key idx. Use set methods on the result for inplace modification. Behavior depends on the container kind: List or tuple: The key must be of integral type. Boundary checks are performed.Map: The key must be of a convertible type to the container key type. If the key is not found in the container, a new default-valued Element will be created, inserted. and returned. the returned value is only valid until owning container is changed.
-
AnyReference
qi::detail::AnyReferenceBase::
operator[]
(const AnyReference& key)¶
- template<typename E, typename K>
-
E&
qi::detail::AnyReferenceBase::
element
(const K& key)¶ Call operator[](key).as<E>, element type must match E.
- template<typename K>
-
AnyReference
qi::detail::AnyReferenceBase::
at
(const K& key)¶ Similar to operator[], but Map container type is not modified if the key does not exist. Returns an empty AnyReference if the key is invalid (out of bounds for list/tuple or key not found for the map)
- template<typename K>
-
AnyReference
qi::detail::AnyReferenceBase::
at
(const K& key)const
¶
-
AnyReference
qi::detail::AnyReferenceBase::
at
(const AnyReference& key)¶
-
AnyReference
qi::detail::AnyReferenceBase::
at
(const AnyReference& key)const
¶
-
size_t
qi::detail::AnyReferenceBase::
size
()const
¶
-
bool
qi::detail::AnyReferenceBase::
optionalHasValue
()const
¶ Returns true if the optional has a value.
- template<typename K, typename V>
-
void
qi::detail::AnyReferenceBase::
insert
(const K& key, const V& val)¶
- template<typename K>
-
AnyReference
qi::detail::AnyReferenceBase::
find
(const K& key)¶ Similar to operator[](), but return an empty AnyValue If the key is not present.
-
AnyIterator
qi::detail::AnyReferenceBase::
begin
()const
¶ Return an iterator on the beginning of the container.
-
AnyIterator
qi::detail::AnyReferenceBase::
end
()const
¶ Return an iterator on the end of the container.
-
AnyReference
qi::detail::AnyReferenceBase::
operator*
()const
¶ Dereference pointer, iterator or dynamic.
-
qi::Signature
qi::detail::AnyReferenceBase::
signature
(bool resolveDynamic = false)const
¶
-
TypeKind
qi::detail::AnyReferenceBase::
kind
()const
¶
-
AnyReference
qi::detail::AnyReferenceBase::
unwrap
()const
¶
-
void
qi::detail::AnyReferenceBase::
update
(const AutoAnyReference& b)¶ TODO: update == set (remove one) Update the value with the one in b
- template<typename T>
-
void
qi::detail::AnyReferenceBase::
set
(const T& val)¶ Update the value to val, which will be converted if required.
-
void
qi::detail::AnyReferenceBase::
set
(const std::string& v)¶
-
void
qi::detail::AnyReferenceBase::
setString
(const std::string& v)¶
-
void
qi::detail::AnyReferenceBase::
setOptional
(const boost::optional<AnyReference>& opt)¶ Sets the value of the optional. A copy will be made.
-
void
qi::detail::AnyReferenceBase::
setRaw
(const char* buffer, size_t size)¶ set the value of the raw buffer, a copy will be made.
-
void
qi::detail::AnyReferenceBase::
setTuple
(const AnyReferenceVector& values)¶ set the values of the tuple. A copy will be made.
-
TypeInterface*
qi::detail::AnyReferenceBase::
type
()const
¶
-
std::vector<TypeInterface*>
qi::detail::AnyReferenceBase::
membersType
()const
¶ Brief:
Returns: list of tuple elements type, or throw if not a tuple
-
void
qi::detail::AnyReferenceBase::
to
()const