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.
Global Classes
class qi::AnyValue
Functions (class qi::AnyValue)
qi::detail::AnyReferenceBase::setString
qi::detail::AnyReferenceBase::asDouble
qi::detail::AnyReferenceBase::toDouble
qi::detail::AnyReferenceBase::asInt64
qi::detail::AnyReferenceBase::asString
qi::detail::AnyReferenceBase::asMapValuePtr
qi::detail::AnyReferenceBase::convertCopy
qi::detail::AnyReferenceBase::setUInt
qi::detail::AnyReferenceBase::clone
qi::detail::AnyReferenceBase::append
qi::detail::AnyReferenceBase::operator[]
qi::detail::AnyReferenceBase::asRaw
qi::detail::AnyReferenceBase::begin
qi::detail::AnyReferenceBase::toList
qi::detail::AnyReferenceBase::to
qi::detail::AnyReferenceBase::find
qi::detail::AnyReferenceBase::content
qi::detail::AnyReferenceBase::signature
qi::detail::AnyReferenceBase::toString
qi::detail::AnyReferenceBase::rawValue
qi::detail::AnyReferenceBase::asUInt64
qi::detail::AnyReferenceBase::toMap
qi::detail::AnyReferenceBase::ptr
qi::detail::AnyReferenceBase::asFloat
qi::detail::AnyReferenceBase::destroy
qi::detail::AnyReferenceBase::toUInt
qi::detail::AnyReferenceBase::asListValuePtr
qi::detail::AnyReferenceBase::setDouble
qi::detail::AnyReferenceBase::fromPtr
qi::detail::AnyReferenceBase::unwrap
qi::detail::AnyReferenceBase::asInt32
qi::detail::AnyReferenceBase::setDynamic
qi::detail::AnyReferenceBase::element
qi::detail::AnyReferenceBase::asInt16
qi::detail::AnyReferenceBase::size
qi::detail::AnyReferenceBase::insert
qi::detail::AnyReferenceBase::setInt
qi::detail::AnyReferenceBase::asUInt32
qi::detail::AnyReferenceBase::toTuple
qi::detail::AnyReferenceBase::membersType
qi::detail::AnyReferenceBase::isValue
qi::detail::AnyReferenceBase::asTupleValuePtr
qi::detail::AnyReferenceBase::asInt8
qi::detail::AnyReferenceBase::toObject
qi::detail::AnyReferenceBase::kind
qi::detail::AnyReferenceBase::convert
qi::detail::AnyReferenceBase::operator*
qi::detail::AnyReferenceBase::setRaw
qi::detail::AnyReferenceBase::set
qi::detail::AnyReferenceBase::end
qi::detail::AnyReferenceBase::asUInt16
qi::detail::AnyReferenceBase::from
qi::detail::AnyReferenceBase::toInt
qi::detail::AnyReferenceBase::isValid
qi::detail::AnyReferenceBase::type
qi::detail::AnyReferenceBase::toFloat
qi::detail::AnyReferenceBase::as
qi::detail::AnyReferenceBase::at
qi::detail::AnyReferenceBase::update
qi::detail::AnyReferenceBase::asUInt8
qi::detail::AnyReferenceBase::setFloat
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
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
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
#include <qi/anyvalue.hpp>
qi::detail::AnyReferenceBase
qi::AnyIterator
AnyValue
()AnyValue
(const AnyValue& b)AnyValue
(const AnyReference& b, bool copy, bool free)AnyValue
(const AutoAnyReference& b)AnyValue
(qi::TypeInterface* type)release
()~AnyValue
()operator=
(const AnyReference& b)operator=
(const AnyValue& b)reset
()reset
(qi::TypeInterface* type)set
(const T& t)reset
(const AnyReference& src)reset
(const AnyReference& src, bool copy, bool free)swap
(AnyValue& b)asReference
() constconvert
(TypeInterface* targetType) constconvert
(ListTypeInterface* targetType) constconvert
(StructTypeInterface* targetType) constconvert
(MapTypeInterface* targetType) constconvert
(IntTypeInterface* targetType) constconvert
(FloatTypeInterface* targetType) constconvert
(RawTypeInterface* targetType) constconvert
(StringTypeInterface* targetType) constconvert
(PointerTypeInterface* targetType) constconvert
(DynamicTypeInterface* targetType) constptr
(bool check)isValid
() constisValue
() constconvertCopy
(TypeInterface* targetType) constclone
() constdestroy
()to
() constto
(const T&) consttoInt
() consttoUInt
() consttoFloat
() consttoDouble
() consttoString
() consttoList
() consttoMap
() consttoObject
() consttoTuple
(bool homogeneous) constas
()asInt64
()asUInt64
()asInt32
()asUInt32
()asInt16
()asUInt16
()asInt8
()asUInt8
()asDouble
()asFloat
()asString
()asRaw
() constcontent
() constasTupleValuePtr
()asListValuePtr
()asMapValuePtr
()operator[]
(const K& key)operator[]
(const AnyReference& key)element
(const K& key)at
(const K& key)at
(const K& key) constat
(const AnyReference& key)at
(const AnyReference& key) constsize
() constappend
(const T& element)append
(const AnyReference& element)insert
(const K& key, const V& val)insert
(const AnyReference& key, const AnyReference& val)find
(const K& key)begin
() constend
() constoperator*
() constsignature
(bool resolveDynamic) constkind
() constunwrap
() constupdate
(const AutoAnyReference& b)set
(const T& val)set
(int64_t v)set
(int32_t v)set
(uint64_t v)set
(uint32_t v)set
(float v)set
(double v)set
(const std::string& v)setInt
(int64_t v)setUInt
(uint64_t v)setFloat
(float v)setDouble
(double v)setString
(const std::string& v)setDynamic
(const AnyReference& value)setRaw
(const char* buffer, size_t size)setTuple
(const AnyReferenceVector& values)type
() constmembersType
() constrawValue
() constto
() constmakeTuple
(const AnyReferenceVector& values)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)makeList
(const AnyReferenceVector& values)makeGenericList
(const AnyReferenceVector& values)makeMap
(const std::map<AnyReference, AnyReference>& values)makeGenericMap
(const std::map<AnyReference, AnyReference>& values)make
()makeVoid
()from
(const T& r)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.
qi::AnyValue::
makeTuple
(const AnyReferenceVector& values)¶The following functions construct a AnyValue from containers of AnyReference.
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)¶qi::AnyValue::
makeList
(const AnyReferenceVector& values)¶qi::AnyValue::
makeGenericList
(const AnyReferenceVector& values)¶qi::AnyValue::
makeMap
(const std::map<AnyReference, AnyReference>& values)¶qi::AnyValue::
makeGenericMap
(const std::map<AnyReference, AnyReference>& values)¶qi::AnyValue::
make
()¶Create and return a AnyValue of type T.
qi::AnyValue::
makeVoid
()¶Construct a void AnyValue: defined, but with no data.
qi::AnyValue::
from
(const T& r)¶qi::AnyValue::
AnyValue
()¶qi::AnyValue::
AnyValue
(const AnyValue& b)¶qi::AnyValue::
AnyValue
(const AnyReference& b, bool copy, bool free)¶qi::AnyValue::
AnyValue
(const AutoAnyReference& b)¶qi::AnyValue::
AnyValue
(qi::TypeInterface* type)¶qi::AnyValue::
release
()¶Brief:
Returns: | the contained value, and reset the AnyValue. |
---|
qi::AnyValue::
~AnyValue
()¶qi::AnyValue::
operator=
(const AnyReference& b)¶qi::AnyValue::
operator=
(const AnyValue& b)¶qi::AnyValue::
reset
()¶qi::AnyValue::
reset
(qi::TypeInterface* type)¶qi::AnyValue::
set
(const T& t)¶qi::AnyValue::
reset
(const AnyReference& src)¶qi::AnyValue::
reset
(const AnyReference& src, bool copy, bool free)¶qi::AnyValue::
swap
(AnyValue& b)¶qi::AnyValue::
asReference
() const
¶qi::detail::AnyReferenceBase::
from
(const T& ref)¶Construction and assign. Construct a AnyValue with storage pointing to ptr.
qi::detail::AnyReferenceBase::
fromPtr
(const T* ptr)¶qi::detail::AnyReferenceBase::
convert
(TypeInterface* targetType) const
¶Brief:
Returns: | the pair (convertedValue, trueIfCopiedAndNeedsDestroy) |
---|
qi::detail::AnyReferenceBase::
convert
(ListTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(StructTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(MapTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(IntTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(FloatTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(RawTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(StringTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(PointerTypeInterface* targetType) const
¶qi::detail::AnyReferenceBase::
convert
(DynamicTypeInterface* targetType) const
¶Brief:
Parameters: |
|
---|---|
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.
qi::detail::AnyReferenceBase::
isValid
() const
¶qi::detail::AnyReferenceBase::
isValue
() const
¶Brief:
Returns: | true if value is valid and not void |
---|
qi::detail::AnyReferenceBase::
convertCopy
(TypeInterface* targetType) const
¶Helper function that converts and always clone.
qi::detail::AnyReferenceBase::
clone
() const
¶qi::detail::AnyReferenceBase::
destroy
()¶Deletes storage.
qi::detail::AnyReferenceBase::
to
() const
¶The following methods return a typed copy of the stored value, converting if necessary. They throw in case of conversion failure.
qi::detail::AnyReferenceBase::
to
(const T&) const
¶Similar to previous method, but uses a dummy value to get the target type.
qi::detail::AnyReferenceBase::
toInt
() const
¶qi::detail::AnyReferenceBase::
toUInt
() const
¶qi::detail::AnyReferenceBase::
toFloat
() const
¶qi::detail::AnyReferenceBase::
toDouble
() const
¶qi::detail::AnyReferenceBase::
toString
() const
¶qi::detail::AnyReferenceBase::
toList
() const
¶qi::detail::AnyReferenceBase::
toMap
() const
¶qi::detail::AnyReferenceBase::
toObject
() const
¶qi::detail::AnyReferenceBase::
toTuple
(bool homogeneous) const
¶Brief:
Parameters: |
|
---|
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.
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.
qi::detail::AnyReferenceBase::
asInt64
()¶qi::detail::AnyReferenceBase::
asUInt64
()¶qi::detail::AnyReferenceBase::
asInt32
()¶qi::detail::AnyReferenceBase::
asUInt32
()¶qi::detail::AnyReferenceBase::
asInt16
()¶qi::detail::AnyReferenceBase::
asUInt16
()¶qi::detail::AnyReferenceBase::
asInt8
()¶qi::detail::AnyReferenceBase::
asUInt8
()¶qi::detail::AnyReferenceBase::
asDouble
()¶qi::detail::AnyReferenceBase::
asFloat
()¶qi::detail::AnyReferenceBase::
asString
()¶qi::detail::AnyReferenceBase::
asRaw
() const
¶Brief:
Returns: | a pair of (char*, size) corresponding to the raw buffer. No copy made. |
---|
qi::detail::AnyReferenceBase::
content
() const
¶Brief:
Returns: | contained AnyValue or throw if type is not dynamic. |
---|
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.
qi::detail::AnyReferenceBase::
asListValuePtr
()¶qi::detail::AnyReferenceBase::
asMapValuePtr
()¶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.
qi::detail::AnyReferenceBase::
operator[]
(const AnyReference& key)¶qi::detail::AnyReferenceBase::
element
(const K& key)¶Call operator[](key).as<E>, element type must match E.
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)
qi::detail::AnyReferenceBase::
at
(const K& key) const
¶qi::detail::AnyReferenceBase::
at
(const AnyReference& key)¶qi::detail::AnyReferenceBase::
at
(const AnyReference& key) const
¶qi::detail::AnyReferenceBase::
size
() const
¶qi::detail::AnyReferenceBase::
append
(const T& element)¶qi::detail::AnyReferenceBase::
append
(const AnyReference& element)¶qi::detail::AnyReferenceBase::
insert
(const K& key, const V& val)¶qi::detail::AnyReferenceBase::
insert
(const AnyReference& key, const AnyReference& val)¶qi::detail::AnyReferenceBase::
find
(const K& key)¶Similar to operator[](), but return an empty AnyValue If the key is not present.
qi::detail::AnyReferenceBase::
begin
() const
¶Return an iterator on the beginning of the container.
qi::detail::AnyReferenceBase::
end
() const
¶Return an iterator on the end of the container.
qi::detail::AnyReferenceBase::
operator*
() const
¶Dereference pointer, iterator or dynamic.
qi::detail::AnyReferenceBase::
signature
(bool resolveDynamic = false) const
¶qi::detail::AnyReferenceBase::
kind
() const
¶qi::detail::AnyReferenceBase::
unwrap
() const
¶qi::detail::AnyReferenceBase::
update
(const AutoAnyReference& b)¶TODO: update == set (remove one) Update the value with the one in b
qi::detail::AnyReferenceBase::
set
(const T& val)¶Update the value to val, which will be converted if required.
qi::detail::AnyReferenceBase::
set
(int64_t v)¶qi::detail::AnyReferenceBase::
set
(int32_t v)¶qi::detail::AnyReferenceBase::
set
(uint64_t v)¶qi::detail::AnyReferenceBase::
set
(uint32_t v)¶qi::detail::AnyReferenceBase::
set
(float v)¶qi::detail::AnyReferenceBase::
set
(double v)¶qi::detail::AnyReferenceBase::
set
(const std::string& v)¶qi::detail::AnyReferenceBase::
setInt
(int64_t v)¶qi::detail::AnyReferenceBase::
setUInt
(uint64_t v)¶qi::detail::AnyReferenceBase::
setFloat
(float v)¶qi::detail::AnyReferenceBase::
setDouble
(double v)¶qi::detail::AnyReferenceBase::
setString
(const std::string& v)¶qi::detail::AnyReferenceBase::
setDynamic
(const AnyReference& value)¶qi::detail::AnyReferenceBase::
setRaw
(const char* buffer, size_t size)¶set the value of the raw buffer, a copy will be made.
qi::detail::AnyReferenceBase::
setTuple
(const AnyReferenceVector& values)¶set the values of the tuple. A copy will be made.
qi::detail::AnyReferenceBase::
type
() const
¶qi::detail::AnyReferenceBase::
membersType
() const
¶Brief:
Returns: | list of tuple elements type, or throw if not a tuple |
---|
qi::detail::AnyReferenceBase::
rawValue
() const
¶qi::detail::AnyReferenceBase::
to
() const
The following methods return a typed copy of the stored value, converting if necessary. They throw in case of conversion failure.