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::_insert
qi::detail::AnyReferenceBase::asFloat
qi::detail::AnyReferenceBase::ptr
qi::detail::AnyReferenceBase::destroy
qi::detail::AnyReferenceBase::_element
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::_append
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::rawValue
qi::detail::AnyReferenceBase::setDouble
qi::detail::AnyReferenceBase::fromPtr
qi::detail::AnyReferenceBase::setDynamic
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::asUInt8
qi::detail::AnyReferenceBase::asInt64
qi::detail::AnyReferenceBase::clone
qi::detail::AnyReferenceBase::to
qi::detail::AnyReferenceBase::signature
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::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::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
-
void
(const AnyReference& b)operator=
-
void
(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
-
std::pair<AnyReference, bool>
(TypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(ListTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(StructTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(MapTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(IntTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(FloatTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(RawTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(StringTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(PointerTypeInterface* targetType) constconvert
-
std::pair<AnyReference, bool>
(DynamicTypeInterface* targetType) constconvert
- template<typename T>
-
T*
(bool check)ptr
-
bool
() constisValid
-
bool
() constisValue
-
AnyReference
(TypeInterface* targetType) constconvertCopy
-
AnyReference
(const AnyReference& key, bool throwOnFailure)_element
-
void
(const AnyReference& element)_append
-
void
(const AnyReference& key, const AnyReference& val)_insert
-
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
-
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
-
AnyReference
() constcontent
-
AnyReferenceVector
()asTupleValuePtr
-
AnyReferenceVector
()asListValuePtr
-
std::map<AnyReference, AnyReference>
()asMapValuePtr
- template<typename K>
-
AnyReference
(const K& key)operator[]
- template<typename E, typename K>
-
E&
(const K& key)element
-
size_t
() constsize
- template<typename T>
-
void
(const T& element)append
- template<typename K, typename V>
-
void
(const K& key, const V& val)insert
- template<typename K>
-
AnyReference
(const K& key)find
-
AnyIterator
() constbegin
-
AnyIterator
() constend
-
AnyReference
() constoperator*
-
qi::Signature
(bool resolveDynamic) constsignature
-
TypeKind
() constkind
-
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 char* buffer, size_t size)setRaw
-
void
(const AnyReferenceVector& values)setTuple
-
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
- 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.
- template<typename T>
-
static AnyValue
qi::AnyValue::
from
(const T& r)¶
-
qi::AnyValue::
AnyValue
()¶
-
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
()¶
-
AnyReference
qi::AnyValue::
asReference
()const
¶
- template<typename T>
-
static AnyReference
qi::detail::AnyReferenceBase::
from
(const T& ref)¶ Construction and assign. Construct a AnyValue with storage pointing to ptr.
- template<typename T>
-
static AnyReference
qi::detail::AnyReferenceBase::
fromPtr
(const T* ptr)¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(TypeInterface* targetType)const
¶ Brief:
Returns: the pair (convertedValue, trueIfCopiedAndNeedsDestroy)
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(ListTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(StructTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(MapTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(IntTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(FloatTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(RawTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(StringTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(PointerTypeInterface* targetType)const
¶
-
std::pair<AnyReference, bool>
qi::detail::AnyReferenceBase::
convert
(DynamicTypeInterface* 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
¶ The following methods return a typed copy of the stored value, converting if necessary. They throw in case of conversion failure.
- 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
¶
-
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.
-
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.
- template<typename E, typename K>
-
E&
qi::detail::AnyReferenceBase::
element
(const K& key)¶ Call operator[](key).as<E>, element type must match E.
-
size_t
qi::detail::AnyReferenceBase::
size
()const
¶
- 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
¶
-
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::
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
- template<typename T>
-
T
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.