etc: Change naming convention for base classes (prefix with "I")

This commit is contained in:
Joshua Scott 2018-12-01 17:16:40 +00:00
parent 34bc3d30bc
commit 6a213c18d4
19 changed files with 189 additions and 198 deletions

View File

@ -10,19 +10,19 @@ namespace pclass
/** /**
* TODO: Documentation * TODO: Documentation
*/ */
class PropertyBase class IProperty
{ {
public: public:
// Do not allow copy assignment. Once a property has been constructed, // Do not allow copy assignment. Once a property has been constructed,
// it shouldn't be able to change. // it shouldn't be able to change.
PropertyBase & operator=(const PropertyBase &that) = delete; virtual IProperty &operator=(const IProperty &that) = delete;
PropertyBase(PropertyClass &object, IProperty(PropertyClass &object,
const std::string &name, const Type &type); const std::string &name, const Type &type);
virtual ~PropertyBase() {} virtual ~IProperty() {}
PropertyBase(PropertyClass &object, IProperty(PropertyClass &object,
const PropertyBase &that); const IProperty &that);
const PropertyClass &get_instance() const; const PropertyClass &get_instance() const;
std::string get_name() const; std::string get_name() const;
@ -51,19 +51,19 @@ namespace pclass
/** /**
* TODO: Documentation * TODO: Documentation
*/ */
class DynamicPropertyBase : public PropertyBase class IDynamicProperty : public IProperty
{ {
public: public:
// Do not allow copy assignment. Once a property has been constructed, // Do not allow copy assignment. Once a property has been constructed,
// it shouldn't be able to change. // it shouldn't be able to change.
DynamicPropertyBase & operator=(const DynamicPropertyBase &that) = delete; IDynamicProperty & operator=(const IDynamicProperty &that) = delete;
DynamicPropertyBase(PropertyClass &object, IDynamicProperty(PropertyClass &object,
const std::string &name, const Type &type); const std::string &name, const Type &type);
virtual ~DynamicPropertyBase() {} virtual ~IDynamicProperty() {}
DynamicPropertyBase(PropertyClass &object, IDynamicProperty(PropertyClass &object,
const DynamicPropertyBase &that); const IDynamicProperty &that);
bool is_dynamic() const override; bool is_dynamic() const override;
virtual std::size_t get_element_count() const = 0; virtual std::size_t get_element_count() const = 0;

View File

@ -19,7 +19,7 @@
: base(that) : base(that)
#define DERIVED_PCLASS(derived, base) class derived : public base #define DERIVED_PCLASS(derived, base) class derived : public base
#define PCLASS(n) DERIVED_PCLASS(n, _KI_PCLASS) #define PCLASS(derived) DERIVED_PCLASS(derived, _KI_PCLASS)
#define PCLASS_CONSTRUCTOR(derived) \ #define PCLASS_CONSTRUCTOR(derived) \
_KI_PCLASS_CONSTRUCTOR(derived) \ _KI_PCLASS_CONSTRUCTOR(derived) \
@ -60,7 +60,7 @@ namespace pclass
*/ */
class PropertyClass class PropertyClass
{ {
friend PropertyBase; friend IProperty;
public: public:
explicit PropertyClass(const Type &type, const TypeSystem &type_system); explicit PropertyClass(const Type &type, const TypeSystem &type_system);
@ -77,7 +77,7 @@ namespace pclass
virtual void on_created() const {} virtual void on_created() const {}
protected: protected:
void add_property(PropertyBase &prop); void add_property(IProperty &prop);
private: private:
const Type *m_type; const Type *m_type;

View File

@ -8,7 +8,7 @@ namespace ki
namespace pclass namespace pclass
{ {
class PropertyClass; class PropertyClass;
class PropertyBase; class IProperty;
/** /**
* TODO: Documentation * TODO: Documentation
@ -26,8 +26,8 @@ namespace pclass
friend PropertyList; friend PropertyList;
public: public:
PropertyBase &operator*() const; IProperty &operator*() const;
PropertyBase *operator->() const; IProperty *operator->() const;
iterator &operator++(); iterator &operator++();
iterator operator++(int); iterator operator++(int);
bool operator==(const iterator &that) const; bool operator==(const iterator &that) const;
@ -48,8 +48,8 @@ namespace pclass
friend PropertyList; friend PropertyList;
public: public:
const PropertyBase &operator*() const; const IProperty &operator*() const;
const PropertyBase *operator->() const; const IProperty *operator->() const;
const_iterator &operator++(); const_iterator &operator++();
const_iterator operator++(int); const_iterator operator++(int);
bool operator==(const const_iterator &that) const; bool operator==(const const_iterator &that) const;
@ -67,14 +67,14 @@ namespace pclass
bool has_property(const std::string &name) const; bool has_property(const std::string &name) const;
bool has_property(hash_t hash) const; bool has_property(hash_t hash) const;
PropertyBase &get_property(int index); IProperty &get_property(int index);
const PropertyBase &get_property(int index) const; const IProperty &get_property(int index) const;
PropertyBase &get_property(const std::string &name); IProperty &get_property(const std::string &name);
const PropertyBase &get_property(const std::string &name) const; const IProperty &get_property(const std::string &name) const;
PropertyBase &get_property(hash_t hash); IProperty &get_property(hash_t hash);
const PropertyBase &get_property(hash_t hash) const; const IProperty &get_property(hash_t hash) const;
iterator begin(); iterator begin();
const_iterator begin() const; const_iterator begin() const;
@ -83,12 +83,12 @@ namespace pclass
const_iterator end() const; const_iterator end() const;
protected: protected:
void add_property(PropertyBase &prop); void add_property(IProperty &prop);
private: private:
std::vector<PropertyBase *> m_properties; std::vector<IProperty *> m_properties;
std::map<std::string, PropertyBase *> m_property_name_lookup; std::map<std::string, IProperty *> m_property_name_lookup;
std::map<hash_t, PropertyBase *> m_property_hash_lookup; std::map<hash_t, IProperty *> m_property_hash_lookup;
}; };
} }
} }

View File

@ -326,7 +326,7 @@ namespace pclass
* TODO: Documentation * TODO: Documentation
*/ */
template <typename ValueT> template <typename ValueT>
class StaticProperty : public PropertyBase class StaticProperty : public IProperty
{ {
// Allow helper utilities access to m_value // Allow helper utilities access to m_value
friend value_object_helper<ValueT>; friend value_object_helper<ValueT>;
@ -339,19 +339,19 @@ namespace pclass
StaticProperty(PropertyClass &object, StaticProperty(PropertyClass &object,
const std::string &name, const Type &type) const std::string &name, const Type &type)
: PropertyBase(object, name, type) : IProperty(object, name, type)
, m_value(value_helper<ValueT>::construct(type)) , m_value(value_helper<ValueT>::construct(type))
{} {}
StaticProperty(PropertyClass &object, StaticProperty(PropertyClass &object,
const std::string &name, const Type &type, ValueT value) const std::string &name, const Type &type, ValueT value)
: PropertyBase(object, name, type) : IProperty(object, name, type)
{ {
m_value = value; m_value = value;
} }
StaticProperty(PropertyClass &object, const StaticProperty<ValueT> &that) StaticProperty(PropertyClass &object, const StaticProperty<ValueT> &that)
: PropertyBase(object, that) : IProperty(object, that)
, m_value(value_helper<ValueT>::copy(that)) , m_value(value_helper<ValueT>::copy(that))
{} {}

View File

@ -21,7 +21,6 @@ namespace pclass
~TypeSystem(); ~TypeSystem();
const HashCalculator &get_hash_calculator() const; const HashCalculator &get_hash_calculator() const;
void set_hash_calculator(HashCalculator *hash_calculator);
bool has_type(const std::string &name) const; bool has_type(const std::string &name) const;
bool has_type(hash_t hash) const; bool has_type(hash_t hash) const;
@ -51,7 +50,7 @@ namespace pclass
} }
template <typename EnumT> template <typename EnumT>
EnumType<EnumT> *define_enum(const std::string &name) EnumType<EnumT> &define_enum(const std::string &name)
{ {
/* /*
auto *type = new EnumType<EnumT>(name, this); auto *type = new EnumType<EnumT>(name, this);
@ -70,6 +69,12 @@ namespace pclass
protected: protected:
void define_type(Type *type); void define_type(Type *type);
private:
TypeList m_types;
TypeNameMap m_type_name_lookup;
TypeHashMap m_type_hash_lookup;
HashCalculator *m_hash_calculator;
template <class ClassT> template <class ClassT>
ClassType<ClassT> &define_class( ClassType<ClassT> &define_class(
const std::string &name, const Type *base_class) const std::string &name, const Type *base_class)
@ -83,12 +88,6 @@ namespace pclass
define_type(type); define_type(type);
return *type; return *type;
} }
private:
TypeList m_types;
TypeNameMap m_type_name_lookup;
TypeHashMap m_type_hash_lookup;
HashCalculator *m_hash_calculator;
}; };
} }
} }

View File

@ -300,8 +300,11 @@ namespace pclass
}; };
/// @endcond /// @endcond
/**
*
*/
template <typename ValueT> template <typename ValueT>
class VectorProperty : public std::vector<ValueT>, public DynamicPropertyBase class VectorProperty : public std::vector<ValueT>, public IDynamicProperty
{ {
public: public:
// Do not allow copy assignment. Once a property has been constructed, // Do not allow copy assignment. Once a property has been constructed,
@ -310,12 +313,12 @@ namespace pclass
VectorProperty(PropertyClass &object, VectorProperty(PropertyClass &object,
const std::string &name, const Type &type) const std::string &name, const Type &type)
: DynamicPropertyBase(object, name, type) : IDynamicProperty(object, name, type)
{} {}
VectorProperty(PropertyClass &object, VectorProperty(PropertyClass &object,
const VectorProperty<ValueT> &that) const VectorProperty<ValueT> &that)
: DynamicPropertyBase(object, that) : IDynamicProperty(object, that)
{ {
// Copy vector values into this vector // Copy vector values into this vector
for (auto i = 0; i < this->size(); i++) for (auto i = 0; i < this->size(); i++)

View File

@ -12,24 +12,31 @@ namespace pclass
/** /**
* TODO: Documentation * TODO: Documentation
*/ */
class ClassTypeBase : public Type class IClassType : public Type
{ {
public: public:
ClassTypeBase(const std::string &name, IClassType(const std::string &name,
const Type *base_class, const TypeSystem &type_system); const Type *base_class, const TypeSystem &type_system);
virtual ~ClassTypeBase() {} virtual ~IClassType() {}
void write_to(BitStream &stream, const Value &value) const override;
void read_from(BitStream &stream, Value &value) const override;
bool inherits(const Type &type) const; bool inherits(const Type &type) const;
protected:
virtual const PropertyClass &get_object_from_value(const Value &value) const = 0;
virtual PropertyClass &get_object_from_value(Value &value) const = 0;
private: private:
const ClassTypeBase *m_base_class; const IClassType *m_base_class;
}; };
/** /**
* TODO: Documentation * TODO: Documentation
*/ */
template <class ClassT> template <class ClassT>
class ClassType : public ClassTypeBase class ClassType : public IClassType
{ {
// Ensure that ClassT inherits PropertyClass // Ensure that ClassT inherits PropertyClass
static_assert(std::is_base_of<PropertyClass, ClassT>::value, "ClassT must inherit PropertyClass!"); static_assert(std::is_base_of<PropertyClass, ClassT>::value, "ClassT must inherit PropertyClass!");
@ -37,27 +44,21 @@ namespace pclass
public: public:
ClassType(const std::string &name, ClassType(const std::string &name,
const Type *base_class, const TypeSystem &type_system) const Type *base_class, const TypeSystem &type_system)
: ClassTypeBase(name, base_class, type_system) {} : IClassType(name, base_class, type_system) {}
PropertyClass *instantiate() const override PropertyClass *instantiate() const override
{ {
return new ClassT(*this, get_type_system()); return new ClassT(*this, get_type_system());
} }
void write_to(BitStream &stream, const Value &value) const override const PropertyClass& get_object_from_value(const Value& value) const override
{ {
const auto &object = dynamic_cast<const PropertyClass &>(value.get<ClassT>()); return dynamic_cast<const PropertyClass &>(value.get<ClassT>());
const auto &properties = object.get_properties();
for (auto it = properties.begin(); it != properties.end(); ++it)
it->write_value_to(stream);
} }
void read_from(BitStream &stream, Value &value) const override PropertyClass& get_object_from_value(Value& value) const override
{ {
auto &object = dynamic_cast<PropertyClass &>(value.get<ClassT>()); return dynamic_cast<PropertyClass &>(value.get<ClassT>());
auto &properties = object.get_properties();
for (auto it = properties.begin(); it != properties.end(); ++it)
it->read_value_from(stream);
} }
}; };
} }

View File

@ -26,7 +26,7 @@ namespace pclass
static_assert(std::is_enum<EnumT>::value, "EnumT must be an enum!"); static_assert(std::is_enum<EnumT>::value, "EnumT must be an enum!");
public: public:
EnumType(std::string name, hash_t hash, TypeSystem *type_system); EnumType(const std::string &name, const TypeSystem &type_system);
}; };
} }
} }

View File

@ -18,8 +18,6 @@ namespace pclass
*/ */
class Type class Type
{ {
friend class TypeSystem;
public: public:
enum class kind enum class kind
{ {
@ -60,12 +58,6 @@ namespace pclass
std::string m_name; std::string m_name;
hash_t m_hash; hash_t m_hash;
const TypeSystem &m_type_system; const TypeSystem &m_type_system;
/**
* Called by a TypeSystem instance when it's HashCalculator
* is changed.
*/
virtual void update_hash();
}; };
typedef std::vector<Type *> TypeList; typedef std::vector<Type *> TypeList;

View File

@ -41,7 +41,7 @@ namespace serialization
* @param is_file Determines whether or not to write type sizes, and property headers. * @param is_file Determines whether or not to write type sizes, and property headers.
* @param flags Determines how serialized data is formatted. * @param flags Determines how serialized data is formatted.
*/ */
explicit SerializerBinary(const pclass::TypeSystem *type_system, explicit SerializerBinary(const pclass::TypeSystem &type_system,
bool is_file, flags flags); bool is_file, flags flags);
virtual ~SerializerBinary() {} virtual ~SerializerBinary() {}
@ -51,11 +51,11 @@ namespace serialization
protected: protected:
virtual void presave_object(const pclass::PropertyClass *object, BitStream &stream) const; virtual void presave_object(const pclass::PropertyClass *object, BitStream &stream) const;
void save_object(const pclass::PropertyClass *object, BitStream &stream) const; void save_object(const pclass::PropertyClass *object, BitStream &stream) const;
void save_property(const pclass::PropertyBase &prop, BitStream &stream) const; void save_property(const pclass::IProperty &prop, BitStream &stream) const;
virtual void preload_object(pclass::PropertyClass *&dest, BitStream &stream) const; virtual void preload_object(pclass::PropertyClass *&dest, BitStream &stream) const;
void load_object(pclass::PropertyClass *&dest, BitStream &stream) const; void load_object(pclass::PropertyClass *&dest, BitStream &stream) const;
void load_property(pclass::PropertyBase &prop, BitStream &stream) const; void load_property(pclass::IProperty &prop, BitStream &stream) const;
private: private:
const pclass::TypeSystem *m_type_system; const pclass::TypeSystem *m_type_system;

View File

@ -15,7 +15,7 @@ namespace ki
/** /**
* *
*/ */
class BitBufferBase class IBitBuffer
{ {
friend BitBufferSegment; friend BitBufferSegment;
@ -53,7 +53,7 @@ namespace ki
void set_bit(int bit); void set_bit(int bit);
}; };
virtual ~BitBufferBase() {}; virtual ~IBitBuffer() {};
/** /**
* @returns * @returns
@ -153,9 +153,9 @@ namespace ki
}; };
/** /**
* * TODO: Documentation
*/ */
class BitBuffer : public BitBufferBase class BitBuffer : public IBitBuffer
{ {
public: public:
explicit BitBuffer(std::size_t buffer_size = KI_BITBUFFER_DEFAULT_SIZE); explicit BitBuffer(std::size_t buffer_size = KI_BITBUFFER_DEFAULT_SIZE);
@ -176,23 +176,23 @@ namespace ki
void resize(std::size_t new_size) override; void resize(std::size_t new_size) override;
/** /**
* @copydoc BitBufferBase::read<ValueT>(buffer_pos, uint8_t) * @copydoc IBitBuffer::read<ValueT>(buffer_pos, uint8_t)
*/ */
template <typename ValueT> template <typename ValueT>
ValueT read(const buffer_pos position, ValueT read(const buffer_pos position,
const uint8_t bits = bitsizeof<ValueT>::value) const const uint8_t bits = bitsizeof<ValueT>::value) const
{ {
return BitBufferBase::read<ValueT>(position, bits); return IBitBuffer::read<ValueT>(position, bits);
} }
/** /**
* @copydoc BitBufferBase::write<ValueT>(ValueT, buffer_pos, uint8_t) * @copydoc IBitBuffer::write<ValueT>(ValueT, buffer_pos, uint8_t)
*/ */
template <typename ValueT> template <typename ValueT>
void write(const ValueT value, const buffer_pos position, void write(const ValueT value, const buffer_pos position,
const uint8_t bits = bitsizeof<ValueT>::value) const uint8_t bits = bitsizeof<ValueT>::value)
{ {
return BitBufferBase::write<ValueT>(value, position, bits); return IBitBuffer::write<ValueT>(value, position, bits);
} }
protected: protected:
@ -205,12 +205,12 @@ namespace ki
}; };
/** /**
* * TODO: Documentation
*/ */
class BitBufferSegment : public BitBufferBase class BitBufferSegment : public IBitBuffer
{ {
public: public:
BitBufferSegment(BitBufferBase &buffer, buffer_pos from, std::size_t bitsize); BitBufferSegment(IBitBuffer &buffer, buffer_pos from, std::size_t bitsize);
std::size_t size() const override; std::size_t size() const override;
void resize(std::size_t new_size) override; void resize(std::size_t new_size) override;
@ -218,23 +218,23 @@ namespace ki
BitBufferSegment *segment(buffer_pos from, std::size_t bitsize) override; BitBufferSegment *segment(buffer_pos from, std::size_t bitsize) override;
/** /**
* @copydoc BitBufferBase::read<ValueT>(buffer_pos, uint8_t) * @copydoc IBitBuffer::read<ValueT>(buffer_pos, uint8_t)
*/ */
template <typename ValueT> template <typename ValueT>
ValueT read(const buffer_pos position, ValueT read(const buffer_pos position,
const uint8_t bits = bitsizeof<ValueT>::value) const const uint8_t bits = bitsizeof<ValueT>::value) const
{ {
return BitBufferBase::read<ValueT>(position, bits); return IBitBuffer::read<ValueT>(position, bits);
} }
/** /**
* @copydoc BitBufferBase::write<ValueT>(ValueT, buffer_pos, uint8_t) * @copydoc IBitBuffer::write<ValueT>(ValueT, buffer_pos, uint8_t)
*/ */
template <typename ValueT> template <typename ValueT>
void write(const ValueT value, const buffer_pos position, void write(const ValueT value, const buffer_pos position,
const uint8_t bits = bitsizeof<ValueT>::value) const uint8_t bits = bitsizeof<ValueT>::value)
{ {
return BitBufferBase::write<ValueT>(value, position, bits); return IBitBuffer::write<ValueT>(value, position, bits);
} }
protected: protected:
@ -242,7 +242,7 @@ namespace ki
void write(uint64_t value, buffer_pos position, uint8_t bits) override; void write(uint64_t value, buffer_pos position, uint8_t bits) override;
private: private:
BitBufferBase *m_buffer; IBitBuffer *m_buffer;
buffer_pos m_from; buffer_pos m_from;
std::size_t m_bitsize; std::size_t m_bitsize;
}; };
@ -255,7 +255,7 @@ namespace ki
public: public:
using stream_pos = BitBuffer::buffer_pos; using stream_pos = BitBuffer::buffer_pos;
explicit BitStream(BitBufferBase &buffer); explicit BitStream(IBitBuffer &buffer);
virtual ~BitStream(); virtual ~BitStream();
BitStream(const BitStream &that); BitStream(const BitStream &that);
@ -281,7 +281,7 @@ namespace ki
/** /**
* @returns The BitBuffer that the stream is reading/writing to. * @returns The BitBuffer that the stream is reading/writing to.
*/ */
BitBufferBase &buffer() const; IBitBuffer &buffer() const;
/** /**
* Reads a value from the buffer. * Reads a value from the buffer.
@ -333,7 +333,7 @@ namespace ki
void write_copy(uint8_t *src, std::size_t bitsize); void write_copy(uint8_t *src, std::size_t bitsize);
private: private:
BitBufferBase *m_buffer; IBitBuffer *m_buffer;
stream_pos m_position; stream_pos m_position;
/** /**

View File

@ -6,7 +6,7 @@ namespace ki
{ {
namespace pclass namespace pclass
{ {
ClassTypeBase::ClassTypeBase(const std::string& name, IClassType::IClassType(const std::string& name,
const Type* base_class, const TypeSystem& type_system) const Type* base_class, const TypeSystem& type_system)
: Type(name, type_system) : Type(name, type_system)
{ {
@ -19,16 +19,32 @@ namespace pclass
if (base_class->get_kind() != kind::CLASS) if (base_class->get_kind() != kind::CLASS)
throw runtime_error("base_class must be a class type!"); throw runtime_error("base_class must be a class type!");
// Cast the base class up to a ClassTypeBase pointer // Cast the base class up to a IClassType pointer
m_base_class = dynamic_cast<const ClassTypeBase *>(base_class); m_base_class = dynamic_cast<const IClassType *>(base_class);
if (!m_base_class) if (!m_base_class)
throw runtime_error("base_class must inherit ClassTypeBase!"); throw runtime_error("base_class must inherit IClassType!");
} }
else else
m_base_class = nullptr; m_base_class = nullptr;
} }
bool ClassTypeBase::inherits(const Type &type) const void IClassType::write_to(BitStream &stream, const Value &value) const
{
const auto &object = get_object_from_value(value);
const auto &properties = object.get_properties();
for (auto it = properties.begin(); it != properties.end(); ++it)
it->write_value_to(stream);
}
void IClassType::read_from(BitStream &stream, Value &value) const
{
auto &object = get_object_from_value(value);
auto &properties = object.get_properties();
for (auto it = properties.begin(); it != properties.end(); ++it)
it->read_value_from(stream);
}
bool IClassType::inherits(const Type &type) const
{ {
// Types do not technically inherit from themselves, but it is more useful // Types do not technically inherit from themselves, but it is more useful
// if they report that they do since these checks are to make sure that objects // if they report that they do since these checks are to make sure that objects

View File

@ -7,7 +7,7 @@ namespace ki
{ {
namespace pclass namespace pclass
{ {
PropertyBase::PropertyBase(PropertyClass &object, IProperty::IProperty(PropertyClass &object,
const std::string &name, const Type &type) const std::string &name, const Type &type)
{ {
m_instance = &object; m_instance = &object;
@ -23,8 +23,8 @@ namespace pclass
object.add_property(*this); object.add_property(*this);
} }
PropertyBase::PropertyBase(PropertyClass &object, IProperty::IProperty(PropertyClass &object,
const PropertyBase &that) const IProperty &that)
{ {
m_instance = &object; m_instance = &object;
m_name = that.m_name; m_name = that.m_name;
@ -36,81 +36,81 @@ namespace pclass
object.add_property(*this); object.add_property(*this);
} }
const PropertyClass &PropertyBase::get_instance() const const PropertyClass &IProperty::get_instance() const
{ {
return *m_instance; return *m_instance;
} }
std::string PropertyBase::get_name() const std::string IProperty::get_name() const
{ {
return m_name; return m_name;
} }
hash_t PropertyBase::get_name_hash() const hash_t IProperty::get_name_hash() const
{ {
return m_name_hash; return m_name_hash;
} }
hash_t PropertyBase::get_full_hash() const hash_t IProperty::get_full_hash() const
{ {
return m_full_hash; return m_full_hash;
} }
const Type &PropertyBase::get_type() const const Type &IProperty::get_type() const
{ {
return *m_type; return *m_type;
} }
bool PropertyBase::is_pointer() const bool IProperty::is_pointer() const
{ {
return false; return false;
} }
bool PropertyBase::is_dynamic() const bool IProperty::is_dynamic() const
{ {
return false; return false;
} }
DynamicPropertyBase::DynamicPropertyBase(PropertyClass &object, IDynamicProperty::IDynamicProperty(PropertyClass &object,
const std::string& name, const Type& type) const std::string& name, const Type& type)
: PropertyBase(object, name, type) : IProperty(object, name, type)
{} {}
DynamicPropertyBase::DynamicPropertyBase(PropertyClass &object, IDynamicProperty::IDynamicProperty(PropertyClass &object,
const DynamicPropertyBase &that) const IDynamicProperty &that)
: PropertyBase(object, that) : IProperty(object, that)
{} {}
bool DynamicPropertyBase::is_dynamic() const bool IDynamicProperty::is_dynamic() const
{ {
return true; return true;
} }
Value DynamicPropertyBase::get_value() const Value IDynamicProperty::get_value() const
{ {
// The caller must specify an index // The caller must specify an index
throw runtime_error("Called get_value() on a dynamic property. Use get_value(index) instead."); throw runtime_error("Called get_value() on a dynamic property. Use get_value(index) instead.");
} }
const PropertyClass *DynamicPropertyBase::get_object() const const PropertyClass *IDynamicProperty::get_object() const
{ {
// The caller must specify an index // The caller must specify an index
throw runtime_error("Called get_object() on a dynamic property. Use get_object(index) instead."); throw runtime_error("Called get_object() on a dynamic property. Use get_object(index) instead.");
} }
void DynamicPropertyBase::set_object(PropertyClass *object) void IDynamicProperty::set_object(PropertyClass *object)
{ {
// The caller must specify an index // The caller must specify an index
throw runtime_error("Called set_object() on a dynamic property. Use set_object(index) instead."); throw runtime_error("Called set_object() on a dynamic property. Use set_object(index) instead.");
} }
void DynamicPropertyBase::write_value_to(BitStream &stream) const void IDynamicProperty::write_value_to(BitStream &stream) const
{ {
// The caller must specify an index // The caller must specify an index
throw runtime_error("Called write_value_to() on a dynamic property. Use write_value_to(index) instead."); throw runtime_error("Called write_value_to() on a dynamic property. Use write_value_to(index) instead.");
} }
void DynamicPropertyBase::read_value_from(BitStream &stream) void IDynamicProperty::read_value_from(BitStream &stream)
{ {
// The caller must specify an index // The caller must specify an index
throw runtime_error("Called read_value_from() on a dynamic property. Use read_value_from(index) instead."); throw runtime_error("Called read_value_from() on a dynamic property. Use read_value_from(index) instead.");

View File

@ -42,7 +42,7 @@ namespace pclass
return new PropertyClass(*this); return new PropertyClass(*this);
} }
void PropertyClass::add_property(PropertyBase &prop) void PropertyClass::add_property(IProperty &prop)
{ {
m_properties.add_property(prop); m_properties.add_property(prop);
} }

View File

@ -8,12 +8,12 @@ namespace ki
{ {
namespace pclass namespace pclass
{ {
PropertyBase &PropertyList::iterator::operator*() const IProperty &PropertyList::iterator::operator*() const
{ {
return m_list->get_property(m_index); return m_list->get_property(m_index);
} }
PropertyBase *PropertyList::iterator::operator->() const IProperty *PropertyList::iterator::operator->() const
{ {
return &operator*(); return &operator*();
} }
@ -48,12 +48,12 @@ namespace pclass
m_index = index; m_index = index;
} }
const PropertyBase &PropertyList::const_iterator::operator*() const const IProperty &PropertyList::const_iterator::operator*() const
{ {
return m_list->get_property(m_index); return m_list->get_property(m_index);
} }
const PropertyBase *PropertyList::const_iterator::operator->() const const IProperty *PropertyList::const_iterator::operator->() const
{ {
return &operator*(); return &operator*();
} }
@ -105,7 +105,7 @@ namespace pclass
!= m_property_hash_lookup.end(); != m_property_hash_lookup.end();
} }
PropertyBase &PropertyList::get_property(const int index) IProperty &PropertyList::get_property(const int index)
{ {
if (index >= 0 && index < m_properties.size()) if (index >= 0 && index < m_properties.size())
return *m_properties[index]; return *m_properties[index];
@ -116,7 +116,7 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
PropertyBase& PropertyList::get_property(const std::string& name) IProperty& PropertyList::get_property(const std::string& name)
{ {
const auto it = m_property_name_lookup.find(name); const auto it = m_property_name_lookup.find(name);
if (it != m_property_name_lookup.end()) if (it != m_property_name_lookup.end())
@ -127,7 +127,7 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
PropertyBase& PropertyList::get_property(const hash_t hash) IProperty& PropertyList::get_property(const hash_t hash)
{ {
const auto it = m_property_hash_lookup.find(hash); const auto it = m_property_hash_lookup.find(hash);
if (it != m_property_hash_lookup.end()) if (it != m_property_hash_lookup.end())
@ -140,7 +140,7 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
const PropertyBase &PropertyList::get_property(const int index) const const IProperty &PropertyList::get_property(const int index) const
{ {
if (index >= 0 && index < m_properties.size()) if (index >= 0 && index < m_properties.size())
return *m_properties[index]; return *m_properties[index];
@ -151,7 +151,7 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
const PropertyBase& PropertyList::get_property(const std::string &name) const const IProperty& PropertyList::get_property(const std::string &name) const
{ {
const auto it = m_property_name_lookup.find(name); const auto it = m_property_name_lookup.find(name);
if (it != m_property_name_lookup.end()) if (it != m_property_name_lookup.end())
@ -162,7 +162,7 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
const PropertyBase &PropertyList::get_property(const hash_t hash) const const IProperty &PropertyList::get_property(const hash_t hash) const
{ {
const auto it = m_property_hash_lookup.find(hash); const auto it = m_property_hash_lookup.find(hash);
if (it != m_property_hash_lookup.end()) if (it != m_property_hash_lookup.end())
@ -195,7 +195,7 @@ namespace pclass
return const_iterator(*this, m_properties.size()); return const_iterator(*this, m_properties.size());
} }
void PropertyList::add_property(PropertyBase &prop) void PropertyList::add_property(IProperty &prop)
{ {
// Make sure a property with the same name as another isn't being added // Make sure a property with the same name as another isn't being added
if (has_property(prop.get_name())) if (has_property(prop.get_name()))

View File

@ -60,13 +60,6 @@ namespace pclass
throw runtime_error(oss.str()); throw runtime_error(oss.str());
} }
void Type::update_hash()
{
m_hash = m_type_system
.get_hash_calculator()
.calculate_type_hash(m_name);
}
void assert_type_match( void assert_type_match(
const Type &expected, const Type &expected,
const Type &actual, const Type &actual,
@ -77,7 +70,7 @@ namespace pclass
if (allow_inheritance && if (allow_inheritance &&
expected.get_kind() == Type::kind::CLASS) expected.get_kind() == Type::kind::CLASS)
{ {
const auto &actual_class = dynamic_cast<const ClassTypeBase &>(actual); const auto &actual_class = dynamic_cast<const IClassType &>(actual);
if (actual_class.inherits(expected)) if (actual_class.inherits(expected))
return; return;
} }

View File

@ -90,26 +90,9 @@ namespace pclass
// Make sure the hash calculator isn't null // Make sure the hash calculator isn't null
if (m_hash_calculator == nullptr) if (m_hash_calculator == nullptr)
throw runtime_error("TypeSystem::get_hash_calculator() called but hash calculator is null."); throw runtime_error("TypeSystem::get_hash_calculator() called but hash calculator is null.");
return *m_hash_calculator; return *m_hash_calculator;
} }
void TypeSystem::set_hash_calculator(HashCalculator* hash_calculator)
{
// Update the hash calculator
m_hash_calculator = hash_calculator;
// Iterate through all types and recalculate their hash
m_type_hash_lookup.clear();
for (auto it = m_types.begin(); it != m_types.end(); ++it)
{
// Add the new hash to lookup
auto *type = *it;
type->update_hash();
m_type_hash_lookup[type->get_hash()] = type;
}
}
bool TypeSystem::has_type(const std::string &name) const bool TypeSystem::has_type(const std::string &name) const
{ {
return m_type_name_lookup.find(name) != m_type_name_lookup.end(); return m_type_name_lookup.find(name) != m_type_name_lookup.end();

View File

@ -6,10 +6,10 @@ namespace ki
{ {
namespace serialization namespace serialization
{ {
SerializerBinary::SerializerBinary(const pclass::TypeSystem* type_system, SerializerBinary::SerializerBinary(const pclass::TypeSystem &type_system,
const bool is_file, const flags flags) const bool is_file, const flags flags)
{ {
m_type_system = type_system; m_type_system = &type_system;
m_is_file = is_file; m_is_file = is_file;
m_flags = flags; m_flags = flags;
m_root_object = nullptr; m_root_object = nullptr;
@ -164,7 +164,7 @@ namespace serialization
stream.seek(BitStream::stream_pos(stream.tell().as_bytes(), 0)); stream.seek(BitStream::stream_pos(stream.tell().as_bytes(), 0));
} }
void SerializerBinary::save_property(const pclass::PropertyBase &prop, BitStream &stream) const void SerializerBinary::save_property(const pclass::IProperty &prop, BitStream &stream) const
{ {
// Remember where we started writing the property data // Remember where we started writing the property data
const auto start_pos = stream.tell(); const auto start_pos = stream.tell();
@ -181,9 +181,9 @@ namespace serialization
auto &property_type = prop.get_type(); auto &property_type = prop.get_type();
if (prop.is_dynamic()) if (prop.is_dynamic())
{ {
// Cast the property to a DynamicPropertyBase // Cast the property to a IDynamicProperty
const auto &dynamic_property = const auto &dynamic_property =
dynamic_cast<const pclass::DynamicPropertyBase &>(prop); dynamic_cast<const pclass::IDynamicProperty &>(prop);
// Write the number of elements // Write the number of elements
stream.write<uint32_t>(dynamic_property.get_element_count()); stream.write<uint32_t>(dynamic_property.get_element_count());
@ -239,7 +239,7 @@ namespace serialization
BitStream &stream, const std::size_t size) BitStream &stream, const std::size_t size)
{ {
// Create a new stream that reads a segment of the stream given to us // Create a new stream that reads a segment of the stream given to us
BitBufferBase *buffer = stream.buffer().segment(stream.tell(), size * 8); IBitBuffer *buffer = stream.buffer().segment(stream.tell(), size * 8);
auto segment_stream = BitStream(*buffer); auto segment_stream = BitStream(*buffer);
stream.seek(stream.tell() + size * 8, false); stream.seek(stream.tell() + size * 8, false);
@ -380,13 +380,13 @@ namespace serialization
stream.seek(BitStream::stream_pos(stream.tell().as_bytes(), 0), false); stream.seek(BitStream::stream_pos(stream.tell().as_bytes(), 0), false);
} }
void SerializerBinary::load_property(pclass::PropertyBase &prop, BitStream &stream) const void SerializerBinary::load_property(pclass::IProperty &prop, BitStream &stream) const
{ {
auto &property_type = prop.get_type(); auto &property_type = prop.get_type();
if (prop.is_dynamic()) if (prop.is_dynamic())
{ {
auto &dynamic_property = auto &dynamic_property =
dynamic_cast<pclass::DynamicPropertyBase &>(prop); dynamic_cast<pclass::IDynamicProperty &>(prop);
// How many elements are there in this dynamic property? // How many elements are there in this dynamic property?
const auto element_count = stream.read<uint32_t>(); const auto element_count = stream.read<uint32_t>();

View File

@ -6,39 +6,39 @@
namespace ki namespace ki
{ {
BitBufferBase::buffer_pos::buffer_pos(const uint32_t byte, const int bit) IBitBuffer::buffer_pos::buffer_pos(const uint32_t byte, const int bit)
{ {
m_byte = byte; m_byte = byte;
set_bit(bit); set_bit(bit);
} }
BitBufferBase::buffer_pos::buffer_pos(const buffer_pos& cp) IBitBuffer::buffer_pos::buffer_pos(const buffer_pos& cp)
{ {
m_byte = cp.m_byte; m_byte = cp.m_byte;
set_bit(cp.m_bit); set_bit(cp.m_bit);
} }
uint32_t BitBufferBase::buffer_pos::as_bytes() const uint32_t IBitBuffer::buffer_pos::as_bytes() const
{ {
return m_byte + (m_bit > 0 ? 1 : 0); return m_byte + (m_bit > 0 ? 1 : 0);
} }
uint32_t BitBufferBase::buffer_pos::as_bits() const uint32_t IBitBuffer::buffer_pos::as_bits() const
{ {
return (m_byte * 8) + m_bit; return (m_byte * 8) + m_bit;
} }
uint32_t BitBufferBase::buffer_pos::get_byte() const uint32_t IBitBuffer::buffer_pos::get_byte() const
{ {
return m_byte; return m_byte;
} }
uint8_t BitBufferBase::buffer_pos::get_bit() const uint8_t IBitBuffer::buffer_pos::get_bit() const
{ {
return m_bit; return m_bit;
} }
void BitBufferBase::buffer_pos::set_bit(int bit) void IBitBuffer::buffer_pos::set_bit(int bit)
{ {
if (bit < 0) if (bit < 0)
{ {
@ -55,7 +55,7 @@ namespace ki
m_bit = bit; m_bit = bit;
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator+( IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator+(
const buffer_pos &rhs) const const buffer_pos &rhs) const
{ {
return buffer_pos( return buffer_pos(
@ -63,7 +63,7 @@ namespace ki
); );
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator-( IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator-(
const buffer_pos &rhs) const const buffer_pos &rhs) const
{ {
return buffer_pos( return buffer_pos(
@ -71,7 +71,7 @@ namespace ki
); );
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator+( IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator+(
const int& rhs) const const int& rhs) const
{ {
return buffer_pos( return buffer_pos(
@ -79,7 +79,7 @@ namespace ki
); );
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator-( IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator-(
const int& rhs) const const int& rhs) const
{ {
return buffer_pos( return buffer_pos(
@ -87,7 +87,7 @@ namespace ki
); );
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator+=( IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator+=(
const buffer_pos rhs) const buffer_pos rhs)
{ {
m_byte += rhs.m_byte; m_byte += rhs.m_byte;
@ -95,7 +95,7 @@ namespace ki
return *this; return *this;
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator-=( IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator-=(
const buffer_pos rhs) const buffer_pos rhs)
{ {
m_byte -= rhs.m_byte; m_byte -= rhs.m_byte;
@ -103,51 +103,51 @@ namespace ki
return *this; return *this;
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator+=(const int bits) IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator+=(const int bits)
{ {
set_bit(m_bit + bits); set_bit(m_bit + bits);
return *this; return *this;
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator-=(const int bits) IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator-=(const int bits)
{ {
set_bit(m_bit - bits); set_bit(m_bit - bits);
return *this; return *this;
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator++() IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator++()
{ {
set_bit(m_bit + 1); set_bit(m_bit + 1);
return *this; return *this;
} }
BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator--() IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator--()
{ {
set_bit(m_bit - 1); set_bit(m_bit - 1);
return *this; return *this;
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator++(int increment) IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator++(int increment)
{ {
auto copy(*this); auto copy(*this);
++(*this); ++(*this);
return copy; return copy;
} }
BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator--(int increment) IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator--(int increment)
{ {
auto copy(*this); auto copy(*this);
--(*this); --(*this);
return copy; return copy;
} }
BitBufferSegment *BitBufferBase::segment( BitBufferSegment *IBitBuffer::segment(
const buffer_pos from, const std::size_t bitsize) const buffer_pos from, const std::size_t bitsize)
{ {
return new BitBufferSegment(*this, from, bitsize); return new BitBufferSegment(*this, from, bitsize);
} }
void BitBufferBase::write_copy(uint8_t *src, void IBitBuffer::write_copy(uint8_t *src,
buffer_pos position, const std::size_t bitsize) buffer_pos position, const std::size_t bitsize)
{ {
auto bits_left = bitsize; auto bits_left = bitsize;
@ -160,7 +160,7 @@ namespace ki
} }
} }
void BitBufferBase::read_copy(uint8_t *dst, void IBitBuffer::read_copy(uint8_t *dst,
buffer_pos position, const std::size_t bitsize) const buffer_pos position, const std::size_t bitsize) const
{ {
auto bits_left = bitsize; auto bits_left = bitsize;
@ -242,7 +242,8 @@ namespace ki
const uint8_t bit_mask = (1 << bit_count) - 1 << position.get_bit(); const uint8_t bit_mask = (1 << bit_count) - 1 << position.get_bit();
// Read the bits from the current byte and position them on the least-signficant bit // Read the bits from the current byte and position them on the least-signficant bit
const uint8_t bits_value = (m_buffer[position.get_byte()] & bit_mask) >> position.get_bit(); const auto &buffer_value = m_buffer[position.get_byte()];
const uint8_t bits_value = (buffer_value & bit_mask) >> position.get_bit();
// Position the value of the bits we just read based on how many bits of the value // Position the value of the bits we just read based on how many bits of the value
// we've already read // we've already read
@ -274,14 +275,17 @@ namespace ki
// Find the bit-mask based on how many bits are being written, and how many bits we've // Find the bit-mask based on how many bits are being written, and how many bits we've
// already written // already written
const uint8_t written_bits = bits - unwritten_bits; const uint8_t written_bits = bits - unwritten_bits;
const auto bit_mask = static_cast<uint64_t>((1 << bit_count) - 1) << written_bits; auto bit_mask_abs = static_cast<uint64_t>((1 << bit_count) - 1);
const auto bit_mask_rel = bit_mask_abs << written_bits;
// Get the bits from the value and position them at the current bit position // Get the bits from the value and position them at the current bit position
uint8_t value_byte = ((value & bit_mask) >> written_bits) & 0xFF; uint8_t value_byte = ((value & bit_mask_rel) >> written_bits) & 0xFF;
value_byte <<= position.get_bit(); value_byte <<= position.get_bit();
bit_mask_abs <<= position.get_bit();
// Write the bits into the byte we're currently at // Write the bits into the byte we're currently at
m_buffer[position.get_byte()] |= value_byte; auto &current_value = m_buffer[position.get_byte()];
current_value = current_value & (~bit_mask_abs & 0xFF) | value_byte;
unwritten_bits -= bit_count; unwritten_bits -= bit_count;
// Move forward the number of bits we just wrote // Move forward the number of bits we just wrote
@ -289,7 +293,7 @@ namespace ki
} }
} }
BitBufferSegment::BitBufferSegment(BitBufferBase& buffer, BitBufferSegment::BitBufferSegment(IBitBuffer& buffer,
const buffer_pos from, const std::size_t bitsize) const buffer_pos from, const std::size_t bitsize)
{ {
m_buffer = &buffer; m_buffer = &buffer;
@ -343,7 +347,7 @@ namespace ki
m_buffer->write(value, m_from + position, bits); m_buffer->write(value, m_from + position, bits);
} }
BitStream::BitStream(BitBufferBase &buffer) BitStream::BitStream(IBitBuffer &buffer)
{ {
m_buffer = &buffer; m_buffer = &buffer;
m_position = stream_pos(0, 0); m_position = stream_pos(0, 0);
@ -367,7 +371,7 @@ namespace ki
return *this; return *this;
} }
BitBufferBase::buffer_pos BitStream::tell() const IBitBuffer::buffer_pos BitStream::tell() const
{ {
return m_position; return m_position;
} }
@ -384,7 +388,7 @@ namespace ki
return m_buffer->size(); return m_buffer->size();
} }
BitBufferBase &BitStream::buffer() const IBitBuffer &BitStream::buffer() const
{ {
return *m_buffer; return *m_buffer;
} }