2018-11-16 15:02:43 +00:00
|
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
|
|
#include "ki/pclass/Property.h"
|
|
|
|
#include "ki/util/exception.h"
|
|
|
|
|
|
|
|
namespace ki
|
|
|
|
{
|
|
|
|
namespace pclass
|
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
// Forward declare for our helpers
|
2018-11-16 15:02:43 +00:00
|
|
|
template <typename ValueT>
|
2018-11-27 11:51:56 +00:00
|
|
|
class VectorProperty;
|
|
|
|
|
|
|
|
/// @cond DOXYGEN_SKIP
|
|
|
|
/**
|
2018-11-27 15:36:57 +00:00
|
|
|
* A helper utility that provides the right implementation of copy(),
|
|
|
|
* get_object() and set_object(), based on characteristics of type: ValueT.
|
2018-11-27 11:51:56 +00:00
|
|
|
*/
|
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename IsPointerEnable = void,
|
|
|
|
typename IsBaseEnable = void
|
|
|
|
>
|
|
|
|
struct vector_value_object_helper
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
static ValueT copy(VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
|
|
|
|
// In cases where ValueT is not a pointer, and does not derive from PropertyClass,
|
|
|
|
// just call the copy constructor.
|
2018-11-27 11:51:56 +00:00
|
|
|
return ValueT(prop.at(index));
|
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static const PropertyClass *get_object(const VectorProperty<ValueT> &prop, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
// ValueT does not derive from PropertyClass, and so, this property is not
|
|
|
|
// storing an object.
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling get_object() on a property that does not store an object."
|
|
|
|
);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void set_object(VectorProperty<ValueT> &prop, PropertyClass *object, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
// ValueT does not derive from PropertyClass, and so, this property is not
|
|
|
|
// storing an object.
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling set_object() on a property that does not store an object."
|
|
|
|
);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct vector_value_object_helper<
|
|
|
|
ValueT,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_pointer<ValueT>::value
|
|
|
|
>::type,
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_base_of<
|
|
|
|
PropertyClass,
|
|
|
|
typename std::remove_pointer<ValueT>::type
|
|
|
|
>::value
|
|
|
|
>::type
|
|
|
|
>
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
static ValueT copy(VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
|
|
|
|
// The copy constructor for all pointers is to copy the pointer
|
|
|
|
// without creating a new copy of the object it's pointing to.
|
2018-11-27 11:51:56 +00:00
|
|
|
return prop.at(index);
|
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static const PropertyClass *get_object(const VectorProperty<ValueT> &prop, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
// ValueT does not derive from PropertyClass, and so, this property is not
|
|
|
|
// storing an object.
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling get_object() on a property that does not store an object."
|
|
|
|
);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void set_object(VectorProperty<ValueT> &prop, PropertyClass *object, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
// ValueT does not derive from PropertyClass, and so, this property is not
|
|
|
|
// storing an object.
|
|
|
|
throw runtime_error(
|
|
|
|
"Tried calling set_object() on a property that does not store an object."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <typename ValueT>
|
|
|
|
struct vector_value_object_helper<
|
|
|
|
ValueT,
|
|
|
|
typename std::enable_if<
|
2018-11-27 15:36:57 +00:00
|
|
|
std::is_pointer<ValueT>::value
|
2018-11-27 11:51:56 +00:00
|
|
|
>::type,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<
|
|
|
|
PropertyClass,
|
|
|
|
typename std::remove_pointer<ValueT>::type
|
|
|
|
>::value
|
|
|
|
>::type
|
|
|
|
>
|
|
|
|
{
|
|
|
|
static ValueT copy(VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// 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.at(index);
|
2018-11-27 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(const VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
2018-11-16 15:02:43 +00:00
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
|
|
|
// 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<PropertyClass *>(prop.at(index));
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void set_object(VectorProperty<ValueT> &prop, PropertyClass *object, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
2018-11-16 15:02:43 +00:00
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
|
|
|
// 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.at(index) = dynamic_cast<ValueT>(object);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
template <typename ValueT>
|
2018-11-27 11:51:56 +00:00
|
|
|
struct vector_value_object_helper<
|
2018-11-16 15:02:43 +00:00
|
|
|
ValueT,
|
|
|
|
typename std::enable_if<
|
2018-11-27 15:36:57 +00:00
|
|
|
!std::is_pointer<ValueT>::value
|
2018-11-27 11:51:56 +00:00
|
|
|
>::type,
|
|
|
|
typename std::enable_if<
|
|
|
|
std::is_base_of<
|
|
|
|
PropertyClass,
|
|
|
|
typename std::remove_pointer<ValueT>::type
|
|
|
|
>::value
|
2018-11-16 15:02:43 +00:00
|
|
|
>::type
|
2018-11-27 11:51:56 +00:00
|
|
|
>
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
static ValueT copy(VectorProperty<ValueT> &prop, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
|
|
|
|
// Derivitives of PropertyClass implement a clone method that returns
|
|
|
|
// a pointer to a copy.
|
|
|
|
ValueT *value_ptr = dynamic_cast<ValueT *>(prop.at(index).copy());
|
|
|
|
ValueT value = *value_ptr;
|
|
|
|
delete value_ptr;
|
|
|
|
return value;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static const PropertyClass *get_object(const VectorProperty<ValueT> &prop, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
2018-11-16 15:02:43 +00:00
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
|
|
|
// ValueT does derive from PropertyClass, and we have an instance of ValueT,
|
|
|
|
// so we can cast down to a PropertyClass pointer.
|
|
|
|
return dynamic_cast<PropertyClass *>(&prop.at(index));
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void set_object(VectorProperty<ValueT> &prop, PropertyClass *object, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
2018-11-16 15:02:43 +00:00
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
|
|
|
// 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.at(index) = dynamic_cast<ValueT>(*object);
|
|
|
|
delete object;
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
template <
|
|
|
|
typename ValueT,
|
2018-11-27 11:51:56 +00:00
|
|
|
typename IsPointerEnable = void
|
2018-11-16 15:02:43 +00:00
|
|
|
>
|
2018-11-27 11:51:56 +00:00
|
|
|
struct vector_value_rw_helper
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
static void write_value_to(const VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
prop.get_type().write_to(stream, prop.at(index));
|
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void read_value_from(VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
|
|
|
prop.get_type().read_from(stream, Value(prop.at(index)));
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
template <typename ValueT>
|
2018-11-27 11:51:56 +00:00
|
|
|
struct vector_value_rw_helper<
|
2018-11-16 15:02:43 +00:00
|
|
|
ValueT,
|
2018-11-27 11:51:56 +00:00
|
|
|
typename std::enable_if<std::is_pointer<ValueT>::value>::type
|
|
|
|
>
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 11:51:56 +00:00
|
|
|
static void write_value_to(const VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
prop.get_type().write_to(stream, *prop.at(index));
|
|
|
|
}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
static void read_value_from(VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
2018-11-27 15:36:57 +00:00
|
|
|
// Ensure index is within bounds
|
2018-11-27 11:51:56 +00:00
|
|
|
if (index < 0 || index >= prop.size())
|
2018-11-16 15:02:43 +00:00
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 15:36:57 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
prop.get_type().read_from(stream, Value(*prop.at(index)));
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
template <typename ValueT>
|
2018-11-27 11:51:56 +00:00
|
|
|
struct vector_value_helper
|
|
|
|
{
|
|
|
|
static ValueT copy(VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
|
|
|
return vector_value_object_helper<ValueT>::copy(prop, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PropertyClass *get_object(const VectorProperty<ValueT> &prop, const int index)
|
|
|
|
{
|
|
|
|
return vector_value_object_helper<ValueT>::get_object(prop, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_object(VectorProperty<ValueT> &prop, PropertyClass *object, const int index)
|
|
|
|
{
|
|
|
|
vector_value_object_helper<ValueT>::set_object(prop, object, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_value_to(const VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
|
|
|
{
|
|
|
|
vector_value_rw_helper<ValueT>::write_value_to(prop, stream, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_value_from(VectorProperty<ValueT> &prop, BitStream &stream, const int index)
|
|
|
|
{
|
|
|
|
vector_value_rw_helper<ValueT>::read_value_from(prop, stream, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @endcond
|
|
|
|
|
2018-12-01 17:16:40 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-11-27 11:51:56 +00:00
|
|
|
template <typename ValueT>
|
2018-12-01 17:16:40 +00:00
|
|
|
class VectorProperty : public std::vector<ValueT>, public IDynamicProperty
|
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.
|
|
|
|
VectorProperty<ValueT> &operator=(const VectorProperty<ValueT> &that) = delete;
|
|
|
|
|
2018-11-16 15:02:43 +00:00
|
|
|
VectorProperty(PropertyClass &object,
|
|
|
|
const std::string &name, const Type &type)
|
2018-12-01 17:16:40 +00:00
|
|
|
: IDynamicProperty(object, name, type)
|
2018-11-27 11:51:56 +00:00
|
|
|
{}
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2018-11-27 11:51:56 +00:00
|
|
|
VectorProperty(PropertyClass &object,
|
|
|
|
const VectorProperty<ValueT> &that)
|
2018-12-01 17:16:40 +00:00
|
|
|
: IDynamicProperty(object, that)
|
2018-11-27 11:51:56 +00:00
|
|
|
{
|
|
|
|
// Copy vector values into this vector
|
|
|
|
for (auto i = 0; i < this->size(); i++)
|
|
|
|
this->push_back(vector_value_helper<ValueT>::copy(*this, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_pointer() const override
|
|
|
|
{
|
|
|
|
return std::is_pointer<ValueT>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t get_element_count() const override
|
|
|
|
{
|
|
|
|
return this->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_element_count(const std::size_t size) override
|
|
|
|
{
|
|
|
|
this->resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value get_value(int index) const override
|
2018-11-16 15:02:43 +00:00
|
|
|
{
|
|
|
|
if (index < 0 || index >= this->size())
|
|
|
|
throw runtime_error("Index out of bounds.");
|
2018-11-27 11:51:56 +00:00
|
|
|
return this->at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PropertyClass *get_object(const int index) const override
|
|
|
|
{
|
|
|
|
return vector_value_helper<ValueT>::get_object(*this, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_object(PropertyClass *object, int index) override
|
|
|
|
{
|
|
|
|
return vector_value_helper<ValueT>::set_object(*this, object, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_value_to(BitStream &stream, const int index) const override
|
|
|
|
{
|
|
|
|
vector_value_helper<ValueT>::write_value_to(*this, stream, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_value_from(BitStream &stream, const int index) override
|
|
|
|
{
|
|
|
|
vector_value_helper<ValueT>::read_value_from(*this, stream, index);
|
2018-11-16 15:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|