37 #ifndef H_OGF_BASIC_TYPES_ANY_H
38 #define H_OGF_BASIC_TYPES_ANY_H
41 #include <OGF/gom/services/life_cycle.h>
49 #ifdef GEO_COMPILER_CLANG
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wcast-align"
69 enum { BUFFER_SIZE = 40 };
76 Any() : value_(nullptr), in_buffer_(false), meta_type_(nullptr) {
92 return value_ ==
nullptr;
107 value_(nullptr), in_buffer_(false), meta_type_(nullptr) {
130 if(value_ !=
nullptr) {
131 convert_to_string(meta_type_, result, value_);
156 MetaType* new_type = resolve_meta_type<T>();
157 if(new_type == meta_type_) {
162 meta_type_ = new_type;
164 #if defined(GEO_COMPILER_GCC)
168 #pragma GCC diagnostic push
169 #pragma GCC diagnostic ignored "-Wplacement-new"
170 #elif defined(GEO_COMPILER_MSVC)
172 #pragma warning(push)
173 #pragma warning(disable:4127)
176 if(
sizeof(T) <= BUFFER_SIZE) {
185 #if defined(GEO_COMPILER_GCC)
186 #pragma GCC diagnostic pop
187 #elif defined(GEO_COMPILER_MSVC)
201 set_value<std::string>(std::string(value));
211 if(value_ ==
nullptr) {
215 if(meta_type_ == resolve_meta_type<T>()) {
216 value = value_as<T>();
220 if(meta_type_ == resolve_meta_type<std::string>()) {
221 const std::string& string_value = value_as<std::string>();
223 resolve_meta_type<T>(),
249 if(get_value<index_t>(value)) {
254 if(get_value<signed_index_t>(tmp)) {
261 if(get_value<float>(tmp)) {
269 if(get_value<double>(tmp)) {
285 if(get_value<signed_index_t>(value)) {
290 if(get_value<index_t>(tmp)) {
297 if(get_value<float>(tmp)) {
305 if(get_value<double>(tmp)) {
321 if(get_value<float>(value)) {
326 if(get_value<double>(tmp)) {
333 if(get_value<index_t>(tmp)) {
340 if(get_value<signed_index_t>(tmp)) {
355 if(get_value<double>(value)) {
360 if(get_value<float>(tmp)) {
367 if(get_value<index_t>(tmp)) {
374 if(get_value<signed_index_t>(tmp)) {
398 if(!is_pointer_type(meta_type())) {
400 <<
"Invalid Any to pointer conversion: not pointer type "
404 << meta_type_name(meta_type())
424 value = (T*)(value_as<Memory::pointer>());
435 meta_type_ = meta_type;
436 if(life_cycle()->object_size() <= BUFFER_SIZE) {
439 life_cycle()->construct(value_);
442 value_ = life_cycle()->new_object();
453 if(value_ !=
nullptr && this->meta_type() == meta_type) {
454 life_cycle()->assign(value_, addr);
457 if(value_ !=
nullptr) {
460 meta_type_ = meta_type;
461 if(life_cycle()->object_size() <= BUFFER_SIZE) {
464 life_cycle()->copy_construct(value_, addr);
467 value_ = life_cycle()->new_object(addr);
480 if(value_ ==
nullptr) {
483 if(meta_type == this->meta_type()) {
484 life_cycle()->assign(addr, value_);
488 return copy_convert_to(addr, meta_type);
546 const MetaType* derived_pointer_type,
561 const std::string& typeid_name
570 resolve_meta_type_by_typeid_name(
typeid(T).name());
594 return *(T*)(value_);
601 if(value_ ==
nullptr) {
605 life_cycle()->destroy(value_);
608 life_cycle()->delete_object(value_);
611 meta_type_ =
nullptr;
625 if(life_cycle()->object_size() <= BUFFER_SIZE) {
628 life_cycle()->copy_construct(value_, rhs.value_);
631 value_ = life_cycle()->new_object(rhs.value_);
635 static std::string meta_type_name(
const MetaType* mt);
646 #ifdef GEO_COMPILER_CLANG
647 #pragma GCC diagnostic pop
#define geo_debug_assert(x)
Verifies that a condition is met.
A class that stores a variable of arbitrary type.
bool get_value(signed_index_t &value) const
Gets the stored value (signed_index_t overload).
bool get_value(T *&value) const
Gets the stored value (pointers overload).
static void convert_to_string(MetaType *meta_type, std::string &string, Memory::pointer value)
Converts an object of a given type into a string.
void set_value(const T &value)
Sets the value of this Any.
const T & value_as() const
Gets the value as a specific type.
std::string as_string() const
Gets a string representation.
LifeCycle * life_cycle() const
Gets the LifeCycle.
static MetaType * pointed_type(const MetaType *mtype)
Gets the deferenced type.
void set_value(const char *value)
Sets the value of this Any (overload for string literals).
Any & operator=(const Any &rhs)
Any affectation operator.
MetaType * meta_type() const
Gets the MetaType of the stored value.
void destroy()
Deallocates the stored variable.
void reset()
Resets this Any to the initial null value.
bool copy_convert_to(Memory::pointer addr, MetaType *meta_type) const
Tentatively converts the value stored in this Any to a type and if successful, store it at a specifie...
static MetaType * resolve_meta_type_by_typeid_name(const std::string &typeid_name)
Finds a MetaType by typeid name.
bool get_value(T &value) const
Gets the stored value.
static bool pointer_can_be_casted_to(const MetaType *derived_pointer_type, const MetaType *base_pointer_type)
Tests whether a pointer type can be casted to an object type.
void copy(const Any &rhs)
Copies another Any.
bool get_value(std::string &value) const
Gets the stored value (std::string overload).
void copy_from(Memory::pointer addr, MetaType *meta_type)
Assigns a value from a specifed address and specified type to this Any.
bool copy_to(Memory::pointer addr, MetaType *meta_type) const
Copies the value stored in this Any at a specified address.
bool get_value(index_t &value) const
Gets the stored value (index_t overload).
static MetaType * resolve_meta_type()
Gets the MetaType associated with a type.
static bool is_pointer_type(const MetaType *mtype)
Tests whether a MetaType is a pointer type.
bool get_value(float &value) const
Gets the stored value (float overload).
bool is_null() const
Tests whether this Any is null.
void create(MetaType *meta_type)
Initializes this Any with the default value of a specified MetaType.
Any(const Any &rhs)
Any copy constructor.
bool get_value(double &value) const
Gets the stored value (double overload).
static void convert_from_string(MetaType *meta_type, const std::string &string, Memory::pointer value)
Converts a string into an object of a given type.
Manages the life cycle of an object.
unsigned char byte
Unsigned byte type.
byte * pointer
Pointer to unsigned byte(s)
void copy(void *to, const void *from, size_t size)
Copies a memory block.
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
geo_index_t index_t
The type for storing and manipulating indices.
Global Graphite namespace.
Definitions common to all include files in the gom library.