2018-11-16 15:02:43 +00:00
|
|
|
#pragma once
|
2018-12-21 01:20:52 +00:00
|
|
|
#include <string>
|
2019-01-01 20:54:57 +00:00
|
|
|
#include "ki/pclass/Type.h"
|
2018-12-21 01:20:52 +00:00
|
|
|
#include "ki/pclass/HashCalculator.h"
|
|
|
|
#include "ki/pclass/Value.h"
|
|
|
|
#include "ki/util/BitStream.h"
|
2018-11-16 15:02:43 +00:00
|
|
|
|
|
|
|
namespace ki
|
|
|
|
{
|
|
|
|
namespace pclass
|
|
|
|
{
|
|
|
|
class PropertyClass;
|
|
|
|
|
|
|
|
/**
|
2019-01-01 20:54:57 +00:00
|
|
|
* A base class for properties.
|
|
|
|
* Provides access to meta information such as name, and type
|
|
|
|
* of instance members, and a mechanism to get/set their values.
|
2018-11-16 15:02:43 +00:00
|
|
|
*/
|
2018-12-01 17:16:40 +00:00
|
|
|
class IProperty
|
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-01 17:16:40 +00:00
|
|
|
virtual IProperty &operator=(const IProperty &that) = delete;
|
2018-11-27 11:51:56 +00:00
|
|
|
|
2018-12-01 17:16:40 +00:00
|
|
|
IProperty(PropertyClass &object,
|
2018-11-16 15:02:43 +00:00
|
|
|
const std::string &name, const Type &type);
|
2018-12-01 17:16:40 +00:00
|
|
|
IProperty(PropertyClass &object,
|
|
|
|
const IProperty &that);
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2019-01-01 20:54:57 +00:00
|
|
|
virtual ~IProperty() = default;
|
2018-12-13 22:12:04 +00:00
|
|
|
|
2018-11-16 15:02:43 +00:00
|
|
|
std::string get_name() const;
|
|
|
|
hash_t get_name_hash() const;
|
|
|
|
hash_t get_full_hash() const;
|
|
|
|
const Type &get_type() const;
|
|
|
|
|
2019-01-01 20:54:57 +00:00
|
|
|
/**
|
|
|
|
* @returns A reference to the instance of PropertyClass that this property
|
|
|
|
* belongs to.
|
|
|
|
*/
|
|
|
|
const PropertyClass &get_instance() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns Whether or not the property's value type is a pointer.
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
virtual bool is_pointer() const;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns Whether or not the property's size is dynamic.
|
|
|
|
*/
|
2018-11-16 15:02:43 +00:00
|
|
|
virtual bool is_dynamic() const;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns Whether or not the property's value type is capable of
|
|
|
|
* holding more than one value.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual bool is_array() const;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns The number of values that the property is holding.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual std::size_t get_element_count() const;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param[in] size The new number of elements.
|
|
|
|
* @throws ki::runtime_error If the property is not dynamically sized.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual void set_element_count(std::size_t size) = 0;
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2019-01-01 20:54:57 +00:00
|
|
|
/**
|
|
|
|
* @param[in] index The index of the element to retrieve the value from.
|
|
|
|
* @returns A reference to the value at the specified index, as a Value instance.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual Value get_value(std::size_t index = 0) const = 0;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param[in] value The new value.
|
|
|
|
* @param[in] index The index of the element to modify.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual void set_value(Value value, std::size_t index = 0) = 0;
|
2018-12-18 21:35:59 +00:00
|
|
|
|
2019-01-01 20:54:57 +00:00
|
|
|
/**
|
|
|
|
* @param[in] index The index of the object element to retrieve.
|
|
|
|
* @returns A pointer to the instance of PropertyClass at the specified index.
|
|
|
|
* @throws ki::runtime_error If the property's value type is not an object,
|
|
|
|
* as in, it does not inherit PropertyClass.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual const PropertyClass *get_object(std::size_t index = 0) const = 0;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param[in] object A pointer to the new object value.
|
|
|
|
* @param[in] index The index of the object element to modify.
|
|
|
|
* @throws ki::runtime_error If the property's value type is not an object,
|
|
|
|
* as in, it does not inherit PropertyClass.
|
|
|
|
*/
|
2018-12-20 15:39:31 +00:00
|
|
|
virtual void set_object(std::unique_ptr<PropertyClass> &object, std::size_t index = 0) = 0;
|
2018-11-16 15:02:43 +00:00
|
|
|
|
2019-01-01 20:54:57 +00:00
|
|
|
/**
|
|
|
|
* Write the value of this property's specified element to a BitStream.
|
|
|
|
* @param[in] stream The stream to write to.
|
|
|
|
* @param[in] index The index of the element to retrieve the value from
|
|
|
|
*/
|
2018-12-21 01:20:52 +00:00
|
|
|
virtual void write_value_to(BitStream &stream, std::size_t index = 0) const;
|
2019-01-01 20:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a value from a BitStream into the specified element of this property.
|
|
|
|
* @param[in] stream The stream to read from.
|
|
|
|
* @param[in] index The index of the element to read a value into.
|
|
|
|
*/
|
2018-12-21 01:20:52 +00:00
|
|
|
virtual void read_value_from(BitStream &stream, std::size_t index = 0);
|
2018-11-16 15:02:43 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-27 15:36:57 +00:00
|
|
|
const PropertyClass *m_instance;
|
2018-11-16 15:02:43 +00:00
|
|
|
std::string m_name;
|
|
|
|
hash_t m_name_hash;
|
|
|
|
hash_t m_full_hash;
|
|
|
|
const Type *m_type;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|