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

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

More...

#include <qi/anyvalue.hpp>

Public Functions

AnyValue()
AnyValue(const AnyValue& b)
AnyValue(const AnyReference& b, bool copy, bool free)
AnyValue(const AutoAnyReference& b)
AnyValue(qi::TypeInterface* type)
AnyReference release()
~AnyValue()
AnyValue& operator=(const AnyReference& b)
AnyValue& operator=(const AnyValue& b)
void reset()
void reset(qi::TypeInterface* type)
template<typename T>
void set(const T& t)
void reset(const AnyReference& src)
void reset(const AnyReference& src, bool copy, bool free)
void swap(AnyValue& b)
AnyReference asReference() const

std::pair<AnyReference, bool> convert(TypeInterface* targetType) const
std::pair<AnyReference, bool> convert(ListTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(StructTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(MapTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(IntTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(FloatTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(RawTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(StringTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(PointerTypeInterface* targetType) const
std::pair<AnyReference, bool> convert(DynamicTypeInterface* targetType) const
template<typename T>
T* ptr(bool check)
bool isValid() const
bool isValue() const
AnyReference convertCopy(TypeInterface* targetType) const
AnyReference clone() const
void destroy()
template<typename T>
T to() const
template<typename T>
T to(const T&) const
int64_t toInt() const
uint64_t toUInt() const
float toFloat() const
double toDouble() const
std::string toString() const
template<typename T>
std::vector<T> toList() const
template<typename K, typename V>
std::map<K, V> toMap() const
AnyObject toObject() const
AnyValue toTuple(bool homogeneous) const
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> asRaw() const
AnyReference content() const
AnyReferenceVector asTupleValuePtr()
AnyReferenceVector asListValuePtr()
std::map<AnyReference, AnyReference> asMapValuePtr()
template<typename K>
AnyReference operator[](const K& key)
AnyReference operator[](const AnyReference& key)
template<typename E, typename K>
E& element(const K& key)
template<typename K>
AnyReference at(const K& key)
template<typename K>
AnyReference at(const K& key) const
AnyReference at(const AnyReference& key)
AnyReference at(const AnyReference& key) const
size_t size() const
template<typename T>
void append(const T& element)
void append(const AnyReference& element)
template<typename K, typename V>
void insert(const K& key, const V& val)
void insert(const AnyReference& key, const AnyReference& val)
template<typename K>
AnyReference find(const K& key)
AnyIterator begin() const
AnyIterator end() const
AnyReference operator*() const
qi::Signature signature(bool resolveDynamic) const
TypeKind kind() const
AnyReference unwrap() const
void update(const AutoAnyReference& b)
template<typename T>
void set(const T& val)
void set(int64_t v)
void set(int32_t v)
void set(uint64_t v)
void set(uint32_t v)
void set(float v)
void set(double v)
void set(const std::string& v)
void setInt(int64_t v)
void setUInt(uint64_t v)
void setFloat(float v)
void setDouble(double v)
void setString(const std::string& v)
void setDynamic(const AnyReference& value)
void setRaw(const char* buffer, size_t size)
void setTuple(const AnyReferenceVector& values)
TypeInterface* type() const
std::vector<TypeInterface*> membersType() const
void* rawValue() const
void to() const

Public Static Functions

AnyValue makeTuple(const AnyReferenceVector& values)
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>
AnyValue makeList(const AnyReferenceVector& values)
AnyValue makeGenericList(const AnyReferenceVector& values)
template<typename K, typename V>
AnyValue makeMap(const std::map<AnyReference, AnyReference>& values)
AnyValue makeGenericMap(const std::map<AnyReference, AnyReference>& values)
template<typename T>
AnyValue make()
AnyValue makeVoid()
template<typename T>
AnyValue from(const T& r)

template<typename T>
AnyReference from(const T& ref)
template<typename T>
AnyReference fromPtr(const T* ptr)

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()
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)
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)
void qi::AnyValue::reset()
void qi::AnyValue::reset(qi::TypeInterface* type)
template<typename T>
void qi::AnyValue::set(const T& t)
void qi::AnyValue::reset(const AnyReference& src)
void qi::AnyValue::reset(const AnyReference& src, bool copy, bool free)
void qi::AnyValue::swap(AnyValue& b)
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.

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::isValid() const
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
void qi::detail::AnyReferenceBase::destroy()

Deletes storage.

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
float qi::detail::AnyReferenceBase::toFloat() 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
T& 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()
float& qi::detail::AnyReferenceBase::asFloat()
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.

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
template<typename T>
void qi::detail::AnyReferenceBase::append(const T& element)
void qi::detail::AnyReferenceBase::append(const AnyReference& element)
template<typename K, typename V>
void qi::detail::AnyReferenceBase::insert(const K& key, const V& val)
void qi::detail::AnyReferenceBase::insert(const AnyReference& key, const AnyReference& 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(int64_t v)
void qi::detail::AnyReferenceBase::set(int32_t v)
void qi::detail::AnyReferenceBase::set(uint64_t v)
void qi::detail::AnyReferenceBase::set(uint32_t v)
void qi::detail::AnyReferenceBase::set(float v)
void qi::detail::AnyReferenceBase::set(double v)
void qi::detail::AnyReferenceBase::set(const std::string& v)
void qi::detail::AnyReferenceBase::setInt(int64_t v)
void qi::detail::AnyReferenceBase::setUInt(uint64_t v)
void qi::detail::AnyReferenceBase::setFloat(float v)
void qi::detail::AnyReferenceBase::setDouble(double v)
void qi::detail::AnyReferenceBase::setString(const std::string& v)
void qi::detail::AnyReferenceBase::setDynamic(const AnyReference& value)
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::rawValue() 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.