2018-11-16 15:02:43 +00:00
|
|
|
#pragma once
|
|
|
|
#include "ki/pclass/Property.h"
|
2018-12-18 21:35:59 +00:00
|
|
|
#include "ki/pclass/PropertyClass.h"
|
2018-11-16 15:02:43 +00:00
|
|
|
#include "ki/util/exception.h"
|
|
|
|
|
|
|
|
namespace ki
|
|
|
|
{
|
|
|
|
namespace pclass
|
|
|
|
{
|
|
|
|
// Forward declare StaticProperty<ValueT> for our helpers
|
|
|
|
template <typename ValueT>
|
|
|
|
class StaticProperty;
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
namespace detail
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
/**
|
|
|
|
* Provides default implementations for static_object_helper.
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct default_static_object_helper
|
|
|
|
{
|
|
|
|
static ValueT construct(const Type &type)
|
|
|
|
{
|
|
|
|
// In cases where ValueT is not a pointer, and does not derive from PropertyClass,
|
|
|
|
// just call the default constructor.
|
|
|
|
return ValueT();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ValueT copy(const StaticProperty<ValueT> &prop)
|
|
|
|
{
|
|
|
|
// In cases where ValueT is not a pointer, and does not derive from PropertyClass,
|
|
|
|
// just call the copy constructor.
|
|
|
|
return ValueT(prop.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT> &prop, int index)
|
|
|
|
{
|
|
|
|
// By default, assume that ValueT is not a
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling get_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, int index)
|
|
|
|
{
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling set_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is an explicitly-sized array.
|
|
|
|
* Removes construct() and copy() to stop the compiler complaining about
|
|
|
|
* a function returning an array.
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct default_static_object_helper<ValueT[N]>
|
|
|
|
{
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT[N]> &prop, int index)
|
|
|
|
{
|
|
|
|
// By default, assume that ValueT is not a
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling get_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT[N]> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, int index)
|
|
|
|
{
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling set_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-12-21 01:20:52 +00:00
|
|
|
* Provides default implementations of construct(), copy()
|
|
|
|
* get_object(), and set_object() for static_object_helper where
|
|
|
|
* ValueT is a pointer type.
|
|
|
|
* @tparam ValueT The type of the value (as a non-pointer type).
|
2018-12-20 15:39:31 +00:00
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct pointer_static_object_helper
|
|
|
|
{
|
|
|
|
static ValueT *construct(const Type &type)
|
|
|
|
{
|
|
|
|
// The default value of pointers is null
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ValueT *copy(const StaticProperty<ValueT *> &prop)
|
|
|
|
{
|
|
|
|
// The copy constructor for all pointers is to copy the pointer
|
|
|
|
// without creating a new copy of the object it's pointing to.
|
|
|
|
return prop.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT *> &prop, int index)
|
|
|
|
{
|
|
|
|
// By default, assume that ValueT is not a
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling get_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT *> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, int index)
|
|
|
|
{
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling set_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper utility that provides the right implementation of construct(),
|
|
|
|
* copy(), get_object() and set_object(), based on characteristics of type: ValueT.
|
|
|
|
*/
|
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename Enable = void
|
|
|
|
>
|
|
|
|
struct static_object_helper : default_static_object_helper<ValueT>
|
|
|
|
{
|
|
|
|
using default_static_object_helper<ValueT>::construct;
|
|
|
|
using default_static_object_helper<ValueT>::copy;
|
|
|
|
using default_static_object_helper<ValueT>::get_object;
|
|
|
|
using default_static_object_helper<ValueT>::set_object;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - Not a pointer; but
|
|
|
|
* - does derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
: default_static_object_helper<ValueT>
|
|
|
|
{
|
|
|
|
using default_static_object_helper<ValueT>::copy;
|
|
|
|
|
|
|
|
static ValueT construct(const Type &type)
|
|
|
|
{
|
|
|
|
// Derivitives of PropertyClass cannot have a default constructor since
|
|
|
|
// they require their Type and TypeSystem, so we need to pass these
|
|
|
|
// along from what the StaticProperty constructor has been given.
|
|
|
|
return ValueT(type, type.get_type_system());
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT> &prop, int index)
|
|
|
|
{
|
|
|
|
// ValueT does derive from PropertyClass, and we have an instance of ValueT,
|
|
|
|
// so we can cast down to a PropertyClass pointer.
|
|
|
|
return dynamic_cast<const PropertyClass *>(&prop.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, int index)
|
|
|
|
{
|
|
|
|
// Ensure that object is not nullptr
|
|
|
|
if (!object)
|
|
|
|
throw runtime_error("Value cannot be null.");
|
|
|
|
|
|
|
|
// Ensure that object is exactly the type of the property.
|
|
|
|
assert_type_match(prop.get_type(), object->get_type());
|
|
|
|
|
|
|
|
// ValueT does derive from PropertyClass, but we don't store a pointer,
|
|
|
|
// so we need to copy the value in.
|
|
|
|
prop.m_value = *dynamic_cast<ValueT *>(object.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - A pointer; but
|
|
|
|
* - does not derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT *,
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
: pointer_static_object_helper<ValueT>
|
|
|
|
{
|
|
|
|
using pointer_static_object_helper<ValueT>::construct;
|
|
|
|
using pointer_static_object_helper<ValueT>::copy;
|
|
|
|
using pointer_static_object_helper<ValueT>::get_object;
|
|
|
|
using pointer_static_object_helper<ValueT>::set_object;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - A pointer; and
|
|
|
|
* - does derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT *,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
: pointer_static_object_helper<ValueT>
|
|
|
|
{
|
|
|
|
using pointer_static_object_helper<ValueT>::construct;
|
|
|
|
using pointer_static_object_helper<ValueT>::copy;
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT *> &prop, int index)
|
|
|
|
{
|
|
|
|
// ValueT does derive from PropertyClass, and we have a pointer to an instance
|
|
|
|
// of ValueT, so we can cast down to a PropertyClass pointer.
|
|
|
|
return dynamic_cast<const PropertyClass *>(prop.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT *> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, int index)
|
|
|
|
{
|
|
|
|
// Ensure that object inherits the type of the property
|
|
|
|
if (object)
|
|
|
|
assert_type_match(prop.get_type(), object->get_type(), true);
|
|
|
|
|
|
|
|
// ValueT does derive from PropertyClass, and we have a pointer to an instance
|
|
|
|
// of PropertyClass, so cast the pointer up to a ValueT.
|
|
|
|
prop.m_value = dynamic_cast<ValueT *>(object.release());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - An explicitly-sized array; and
|
|
|
|
* - does not derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT[N],
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
: default_static_object_helper<ValueT[N]>
|
|
|
|
{
|
|
|
|
using default_static_object_helper<ValueT[N]>::get_object;
|
|
|
|
using default_static_object_helper<ValueT[N]>::set_object;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - An explicitly-sized array of non-pointer values; and
|
|
|
|
* - does derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT[N],
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
{
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
|
|
|
// ValueT does derive from PropertyClass, and we have an instance of ValueT,
|
|
|
|
// so we can cast down to a PropertyClass pointer.
|
|
|
|
return dynamic_cast<const PropertyClass *>(&prop.m_value[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, const int index)
|
|
|
|
{
|
|
|
|
// Ensure that object is not nullptr
|
|
|
|
if (!object)
|
|
|
|
throw runtime_error("Value cannot be null.");
|
|
|
|
|
|
|
|
// Ensure that object is exactly the type of the property.
|
|
|
|
assert_type_match(prop.get_type(), object->get_type());
|
|
|
|
|
|
|
|
// ValueT does derive from PropertyClass, but we don't store a pointer,
|
|
|
|
// so we need to copy the value in.
|
|
|
|
prop.m_value[index] = *dynamic_cast<ValueT *>(object.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is:
|
|
|
|
* - An explicitly-sized array of pointer values; and
|
|
|
|
* - does derive from PropertyClass
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct static_object_helper<
|
|
|
|
ValueT *[N],
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<PropertyClass, ValueT>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
{
|
|
|
|
static const PropertyClass *get_object(
|
|
|
|
const StaticProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
|
|
|
// ValueT does derive from PropertyClass, and we have an instance of ValueT,
|
|
|
|
// so we can cast down to a PropertyClass pointer.
|
|
|
|
return dynamic_cast<const PropertyClass *>(prop.m_value[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(StaticProperty<ValueT> &prop,
|
|
|
|
std::unique_ptr<PropertyClass> &object, const int index)
|
|
|
|
{
|
|
|
|
// Ensure that object inherits the type of the property
|
|
|
|
if (object)
|
|
|
|
assert_type_match(prop.get_type(), object->get_type(), true);
|
|
|
|
|
|
|
|
// ValueT does derive from PropertyClass, and we have a pointer to an instance
|
|
|
|
// of PropertyClass, so cast the pointer up to a ValueT.
|
|
|
|
prop.m_value[index] = dynamic_cast<ValueT *>(object.release());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper utility that provides the right implementation of
|
|
|
|
* get_value() and set_value() based on whether ValueT is a pointer.
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct static_value_helper
|
|
|
|
{
|
|
|
|
static Value get_value(const StaticProperty<ValueT> &prop, int index)
|
|
|
|
{
|
|
|
|
return Value::make_reference<ValueT>(prop.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_value(StaticProperty<ValueT> &prop, Value value, int index)
|
|
|
|
{
|
2018-12-21 01:20:52 +00:00
|
|
|
Value casted_value = value.as<ValueT>();
|
|
|
|
prop.m_value = casted_value.get<ValueT>();
|
2018-12-20 15:39:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is a pointer.
|
|
|
|
*
|
|
|
|
* Dereference the pointer before creating Value instances.
|
|
|
|
* This is so that the Value stores a pointer to a ValueT instance,
|
|
|
|
* rather than storing a pointer to a pointer.
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct static_value_helper<ValueT *>
|
|
|
|
{
|
|
|
|
static Value get_value(const StaticProperty<ValueT *> &prop, int index)
|
|
|
|
{
|
2018-12-21 01:20:52 +00:00
|
|
|
if (prop.m_value == nullptr)
|
|
|
|
throw runtime_error("Called get_value() but value is nullptr.");
|
2018-12-20 15:39:31 +00:00
|
|
|
return Value::make_reference<ValueT>(*prop.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_value(StaticProperty<ValueT *> &prop, Value value, int index)
|
|
|
|
{
|
2018-12-21 01:20:52 +00:00
|
|
|
Value casted_value = value.as<ValueT>();
|
|
|
|
prop.m_value = casted_value.release<ValueT>();
|
2018-12-20 15:39:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is an explicitly-sized array
|
|
|
|
* of non-pointer values.
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct static_value_helper<ValueT[N]>
|
|
|
|
{
|
|
|
|
static Value get_value(const StaticProperty<ValueT[N]> &prop, const int index)
|
|
|
|
{
|
|
|
|
return Value::make_reference<ValueT>(prop.m_value[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_value(StaticProperty<ValueT[N]> &prop, Value value, const int index)
|
|
|
|
{
|
2018-12-21 01:20:52 +00:00
|
|
|
Value casted_value = value.as<ValueT>();
|
|
|
|
prop.m_value[index] = casted_value.get<ValueT>();
|
2018-12-20 15:39:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when ValueT is an explicitly-sized array
|
|
|
|
* of pointer values.
|
|
|
|
*/
|
|
|
|
template <typename ValueT, int N>
|
|
|
|
struct static_value_helper<ValueT *[N]>
|
|
|
|
{
|
|
|
|
static Value get_value(const StaticProperty<ValueT[N]> &prop, const int index)
|
|
|
|
{
|
|
|
|
return Value::make_reference<ValueT>(*prop.m_value[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_value(StaticProperty<ValueT[N]> &prop, Value value, const int index)
|
|
|
|
{
|
2018-12-21 01:20:52 +00:00
|
|
|
Value casted_value = value.as<ValueT>();
|
|
|
|
prop.m_value[index] = casted_value.release<ValueT>();
|
2018-12-20 15:39:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-21 01:20:52 +00:00
|
|
|
* Base type for StaticProperty.
|
|
|
|
* This is used to remove the amount of repeated code in specializations.
|
2018-11-16 15:02:43 +00:00
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
2018-12-20 15:39:31 +00:00
|
|
|
class IStaticProperty : public IProperty
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
public:
|
2018-12-21 01:20:52 +00:00
|
|
|
using IProperty::IProperty;
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
// Do not allow copy assignment. Once a property has been constructed,
|
|
|
|
// it shouldn't be able to change.
|
|
|
|
virtual IStaticProperty<ValueT> &operator=(const IStaticProperty<ValueT> &that) = delete;
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
constexpr bool is_pointer() const override
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return std::is_pointer<ValueT>::value;
|
2018-11-27 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
constexpr bool is_dynamic() const override
|
2018-12-15 16:51:45 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return false;
|
2018-12-15 16:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void set_element_count(std::size_t size) override
|
2018-12-18 21:35:59 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
throw runtime_error("Tried to call set_element_count() on a property that is not dynamic.");
|
2018-12-18 21:35:59 +00:00
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-12-20 15:39:31 +00:00
|
|
|
* A statically-sized property.
|
2018-11-16 15:02:43 +00:00
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
2018-12-20 15:39:31 +00:00
|
|
|
class StaticProperty : public IStaticProperty<ValueT>
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
// Allow helper utilities access to m_value
|
|
|
|
friend detail::default_static_object_helper<ValueT>;
|
|
|
|
friend detail::pointer_static_object_helper<ValueT>;
|
|
|
|
friend detail::static_object_helper<ValueT>;
|
|
|
|
friend detail::static_value_helper<ValueT>;
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
public:
|
|
|
|
// Do not allow copy assignment. Once a property has been constructed,
|
|
|
|
// it shouldn't be able to change.
|
|
|
|
StaticProperty<ValueT> &operator=(const StaticProperty<ValueT> &that) = delete;
|
2018-11-27 11:51:56 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
StaticProperty(PropertyClass &object,
|
|
|
|
const std::string &name, const Type &type)
|
2018-12-21 01:20:52 +00:00
|
|
|
: IStaticProperty<ValueT>(object, name, type)
|
2018-12-20 15:39:31 +00:00
|
|
|
, m_value(detail::static_object_helper<ValueT>::construct(type))
|
|
|
|
{}
|
2018-12-18 21:35:59 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
StaticProperty(PropertyClass &object,
|
|
|
|
const std::string &name, const Type &type, ValueT value)
|
2018-12-21 01:20:52 +00:00
|
|
|
: IStaticProperty<ValueT>(object, name, type)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
m_value = value;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
2018-11-27 11:51:56 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
StaticProperty(PropertyClass &object, const StaticProperty<ValueT> &that)
|
2018-12-21 01:20:52 +00:00
|
|
|
: IStaticProperty<ValueT>(object, that)
|
2018-12-20 15:39:31 +00:00
|
|
|
, m_value(detail::static_object_helper<ValueT>::copy(that))
|
|
|
|
{}
|
2018-11-27 15:36:57 +00:00
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
std::size_t get_element_count() const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return 1;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
Value get_value(std::size_t index) const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_value_helper<ValueT>::get_value(*this, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void set_value(Value value, const std::size_t index) override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
detail::static_value_helper<ValueT>::set_value(*this, value, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
const PropertyClass *get_object(const std::size_t index) const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_object_helper<ValueT>::get_object(*this, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void set_object(std::unique_ptr<PropertyClass> &object, const std::size_t index) override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_object_helper<ValueT>::set_object(*this, object, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
ValueT &get()
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return m_value;
|
2018-11-27 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
const ValueT &get() const
|
2018-12-15 16:51:45 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return m_value;
|
2018-12-15 16:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
operator ValueT &()
|
2018-12-18 21:35:59 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return m_value;
|
2018-12-18 21:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
operator const ValueT &() const
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return m_value;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
ValueT *operator&()
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return &m_value;
|
2018-11-27 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
const ValueT *operator&() const
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return &m_value;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void operator=(const ValueT &value)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
m_value = value;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
2018-12-20 15:39:31 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
ValueT m_value;
|
2018-11-16 15:02:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-12-20 15:39:31 +00:00
|
|
|
* Specialization of StaticProperty to support explicitly-sized arrays.
|
2018-11-16 15:02:43 +00:00
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
template <typename ValueT, int N>
|
|
|
|
class StaticProperty<ValueT[N]> : public IStaticProperty<ValueT[N]>
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
|
|
|
// Allow helper utilities access to m_value
|
2018-12-20 15:39:31 +00:00
|
|
|
friend detail::static_object_helper<ValueT[N]>;
|
|
|
|
friend detail::static_value_helper<ValueT[N]>;
|
2018-11-16 15:02:43 +00:00
|
|
|
|
|
|
|
public:
|
2018-11-27 11:51:56 +00:00
|
|
|
// Do not allow copy assignment. Once a property has been constructed,
|
|
|
|
// it shouldn't be able to change.
|
2018-12-20 15:39:31 +00:00
|
|
|
StaticProperty<ValueT[N]> &operator=(const StaticProperty<ValueT[N]> &that) = delete;
|
2018-11-27 11:51:56 +00:00
|
|
|
|
2018-11-16 15:02:43 +00:00
|
|
|
StaticProperty(PropertyClass &object,
|
|
|
|
const std::string &name, const Type &type)
|
2018-12-21 01:20:52 +00:00
|
|
|
: IStaticProperty<ValueT[N]>(object, name, type)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
for (auto i = 0; i < N; ++i)
|
|
|
|
m_value[i] = detail::static_object_helper<ValueT>::construct(type);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
StaticProperty(PropertyClass &object, const StaticProperty<ValueT[N]> &that)
|
2018-12-21 01:20:52 +00:00
|
|
|
: IStaticProperty<ValueT[N]>(object, that)
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
for (auto i = 0; i < N; ++i)
|
|
|
|
m_value[i] = that.m_value[i];
|
2018-11-27 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
bool is_array() const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return true;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
std::size_t get_element_count() const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
return N;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
Value get_value(std::size_t index) const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_value_helper<ValueT[N]>::get_value(*this, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void set_value(Value value, const std::size_t index) override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
detail::static_value_helper<ValueT[N]>::set_value(*this, value, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
const PropertyClass *get_object(const std::size_t index) const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_object_helper<ValueT[N]>::get_object(*this, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
void set_object(std::unique_ptr<PropertyClass> &object, const std::size_t index) override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return detail::static_object_helper<ValueT[N]>::set_object(*this, object, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 15:39:31 +00:00
|
|
|
ValueT &operator[](const int index)
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
2018-12-20 15:39:31 +00:00
|
|
|
if (index < 0 || index >= get_element_count())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
return m_value[index];
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-12-20 15:39:31 +00:00
|
|
|
ValueT m_value[N];
|
2018-11-16 15:02:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|