diff --git a/include/ki/pclass/Property.h b/include/ki/pclass/Property.h index a186e81..fa8141e 100644 --- a/include/ki/pclass/Property.h +++ b/include/ki/pclass/Property.h @@ -10,19 +10,19 @@ namespace pclass /** * TODO: Documentation */ - class PropertyBase + class IProperty { public: // Do not allow copy assignment. Once a property has been constructed, // 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); - virtual ~PropertyBase() {} + virtual ~IProperty() {} - PropertyBase(PropertyClass &object, - const PropertyBase &that); + IProperty(PropertyClass &object, + const IProperty &that); const PropertyClass &get_instance() const; std::string get_name() const; @@ -51,19 +51,19 @@ namespace pclass /** * TODO: Documentation */ - class DynamicPropertyBase : public PropertyBase + class IDynamicProperty : public IProperty { public: // Do not allow copy assignment. Once a property has been constructed, // 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); - virtual ~DynamicPropertyBase() {} + virtual ~IDynamicProperty() {} - DynamicPropertyBase(PropertyClass &object, - const DynamicPropertyBase &that); + IDynamicProperty(PropertyClass &object, + const IDynamicProperty &that); bool is_dynamic() const override; virtual std::size_t get_element_count() const = 0; diff --git a/include/ki/pclass/PropertyClass.h b/include/ki/pclass/PropertyClass.h index 90c0e86..2f8e2a8 100644 --- a/include/ki/pclass/PropertyClass.h +++ b/include/ki/pclass/PropertyClass.h @@ -19,7 +19,7 @@ : base(that) #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) \ _KI_PCLASS_CONSTRUCTOR(derived) \ @@ -60,7 +60,7 @@ namespace pclass */ class PropertyClass { - friend PropertyBase; + friend IProperty; public: explicit PropertyClass(const Type &type, const TypeSystem &type_system); @@ -77,7 +77,7 @@ namespace pclass virtual void on_created() const {} protected: - void add_property(PropertyBase &prop); + void add_property(IProperty &prop); private: const Type *m_type; diff --git a/include/ki/pclass/PropertyList.h b/include/ki/pclass/PropertyList.h index 3424a48..a0102f2 100644 --- a/include/ki/pclass/PropertyList.h +++ b/include/ki/pclass/PropertyList.h @@ -8,7 +8,7 @@ namespace ki namespace pclass { class PropertyClass; - class PropertyBase; + class IProperty; /** * TODO: Documentation @@ -26,8 +26,8 @@ namespace pclass friend PropertyList; public: - PropertyBase &operator*() const; - PropertyBase *operator->() const; + IProperty &operator*() const; + IProperty *operator->() const; iterator &operator++(); iterator operator++(int); bool operator==(const iterator &that) const; @@ -48,8 +48,8 @@ namespace pclass friend PropertyList; public: - const PropertyBase &operator*() const; - const PropertyBase *operator->() const; + const IProperty &operator*() const; + const IProperty *operator->() const; const_iterator &operator++(); const_iterator operator++(int); bool operator==(const const_iterator &that) const; @@ -67,14 +67,14 @@ namespace pclass bool has_property(const std::string &name) const; bool has_property(hash_t hash) const; - PropertyBase &get_property(int index); - const PropertyBase &get_property(int index) const; + IProperty &get_property(int index); + const IProperty &get_property(int index) const; - PropertyBase &get_property(const std::string &name); - const PropertyBase &get_property(const std::string &name) const; + IProperty &get_property(const std::string &name); + const IProperty &get_property(const std::string &name) const; - PropertyBase &get_property(hash_t hash); - const PropertyBase &get_property(hash_t hash) const; + IProperty &get_property(hash_t hash); + const IProperty &get_property(hash_t hash) const; iterator begin(); const_iterator begin() const; @@ -83,12 +83,12 @@ namespace pclass const_iterator end() const; protected: - void add_property(PropertyBase &prop); + void add_property(IProperty &prop); private: - std::vector m_properties; - std::map m_property_name_lookup; - std::map m_property_hash_lookup; + std::vector m_properties; + std::map m_property_name_lookup; + std::map m_property_hash_lookup; }; } } diff --git a/include/ki/pclass/StaticProperty.h b/include/ki/pclass/StaticProperty.h index 611dc69..7bb44e2 100644 --- a/include/ki/pclass/StaticProperty.h +++ b/include/ki/pclass/StaticProperty.h @@ -326,7 +326,7 @@ namespace pclass * TODO: Documentation */ template - class StaticProperty : public PropertyBase + class StaticProperty : public IProperty { // Allow helper utilities access to m_value friend value_object_helper; @@ -339,19 +339,19 @@ namespace pclass StaticProperty(PropertyClass &object, const std::string &name, const Type &type) - : PropertyBase(object, name, type) + : IProperty(object, name, type) , m_value(value_helper::construct(type)) {} StaticProperty(PropertyClass &object, const std::string &name, const Type &type, ValueT value) - : PropertyBase(object, name, type) + : IProperty(object, name, type) { m_value = value; } StaticProperty(PropertyClass &object, const StaticProperty &that) - : PropertyBase(object, that) + : IProperty(object, that) , m_value(value_helper::copy(that)) {} diff --git a/include/ki/pclass/TypeSystem.h b/include/ki/pclass/TypeSystem.h index c5bb227..efc5824 100644 --- a/include/ki/pclass/TypeSystem.h +++ b/include/ki/pclass/TypeSystem.h @@ -21,7 +21,6 @@ namespace pclass ~TypeSystem(); const HashCalculator &get_hash_calculator() const; - void set_hash_calculator(HashCalculator *hash_calculator); bool has_type(const std::string &name) const; bool has_type(hash_t hash) const; @@ -51,7 +50,7 @@ namespace pclass } template - EnumType *define_enum(const std::string &name) + EnumType &define_enum(const std::string &name) { /* auto *type = new EnumType(name, this); @@ -70,6 +69,12 @@ namespace pclass protected: void define_type(Type *type); + private: + TypeList m_types; + TypeNameMap m_type_name_lookup; + TypeHashMap m_type_hash_lookup; + HashCalculator *m_hash_calculator; + template ClassType &define_class( const std::string &name, const Type *base_class) @@ -83,12 +88,6 @@ namespace pclass define_type(type); return *type; } - - private: - TypeList m_types; - TypeNameMap m_type_name_lookup; - TypeHashMap m_type_hash_lookup; - HashCalculator *m_hash_calculator; }; } } diff --git a/include/ki/pclass/VectorProperty.h b/include/ki/pclass/VectorProperty.h index 9efd761..2836fed 100644 --- a/include/ki/pclass/VectorProperty.h +++ b/include/ki/pclass/VectorProperty.h @@ -300,8 +300,11 @@ namespace pclass }; /// @endcond + /** + * + */ template - class VectorProperty : public std::vector, public DynamicPropertyBase + class VectorProperty : public std::vector, public IDynamicProperty { public: // Do not allow copy assignment. Once a property has been constructed, @@ -310,12 +313,12 @@ namespace pclass VectorProperty(PropertyClass &object, const std::string &name, const Type &type) - : DynamicPropertyBase(object, name, type) + : IDynamicProperty(object, name, type) {} VectorProperty(PropertyClass &object, const VectorProperty &that) - : DynamicPropertyBase(object, that) + : IDynamicProperty(object, that) { // Copy vector values into this vector for (auto i = 0; i < this->size(); i++) diff --git a/include/ki/pclass/types/ClassType.h b/include/ki/pclass/types/ClassType.h index 49fc575..98829cb 100644 --- a/include/ki/pclass/types/ClassType.h +++ b/include/ki/pclass/types/ClassType.h @@ -12,24 +12,31 @@ namespace pclass /** * TODO: Documentation */ - class ClassTypeBase : public Type + class IClassType : public Type { public: - ClassTypeBase(const std::string &name, + IClassType(const std::string &name, 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; + protected: + virtual const PropertyClass &get_object_from_value(const Value &value) const = 0; + virtual PropertyClass &get_object_from_value(Value &value) const = 0; + private: - const ClassTypeBase *m_base_class; + const IClassType *m_base_class; }; /** - * TODO: Documentation - */ + * TODO: Documentation + */ template - class ClassType : public ClassTypeBase + class ClassType : public IClassType { // Ensure that ClassT inherits PropertyClass static_assert(std::is_base_of::value, "ClassT must inherit PropertyClass!"); @@ -37,27 +44,21 @@ namespace pclass public: ClassType(const std::string &name, const Type *base_class, const TypeSystem &type_system) - : ClassTypeBase(name, base_class, type_system) {} + : IClassType(name, base_class, type_system) {} PropertyClass *instantiate() const override { 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(value.get()); - const auto &properties = object.get_properties(); - for (auto it = properties.begin(); it != properties.end(); ++it) - it->write_value_to(stream); + return dynamic_cast(value.get()); } - void read_from(BitStream &stream, Value &value) const override + PropertyClass& get_object_from_value(Value& value) const override { - auto &object = dynamic_cast(value.get()); - auto &properties = object.get_properties(); - for (auto it = properties.begin(); it != properties.end(); ++it) - it->read_value_from(stream); + return dynamic_cast(value.get()); } }; } diff --git a/include/ki/pclass/types/EnumType.h b/include/ki/pclass/types/EnumType.h index 468f700..40ed56b 100644 --- a/include/ki/pclass/types/EnumType.h +++ b/include/ki/pclass/types/EnumType.h @@ -26,7 +26,7 @@ namespace pclass static_assert(std::is_enum::value, "EnumT must be an enum!"); public: - EnumType(std::string name, hash_t hash, TypeSystem *type_system); + EnumType(const std::string &name, const TypeSystem &type_system); }; } -} \ No newline at end of file +} diff --git a/include/ki/pclass/types/Type.h b/include/ki/pclass/types/Type.h index 40438df..a6e8c81 100644 --- a/include/ki/pclass/types/Type.h +++ b/include/ki/pclass/types/Type.h @@ -18,8 +18,6 @@ namespace pclass */ class Type { - friend class TypeSystem; - public: enum class kind { @@ -42,7 +40,7 @@ namespace pclass }; Type(const std::string &name, const TypeSystem &type_system); - virtual ~Type() { } + virtual ~Type() {} std::string get_name() const; hash_t get_hash() const; @@ -60,12 +58,6 @@ namespace pclass std::string m_name; hash_t m_hash; const TypeSystem &m_type_system; - - /** - * Called by a TypeSystem instance when it's HashCalculator - * is changed. - */ - virtual void update_hash(); }; typedef std::vector TypeList; diff --git a/include/ki/serialization/SerializerBinary.h b/include/ki/serialization/SerializerBinary.h index 21e287c..2c02503 100644 --- a/include/ki/serialization/SerializerBinary.h +++ b/include/ki/serialization/SerializerBinary.h @@ -41,7 +41,7 @@ namespace serialization * @param is_file Determines whether or not to write type sizes, and property headers. * @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); virtual ~SerializerBinary() {} @@ -51,11 +51,11 @@ namespace serialization protected: virtual void presave_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; 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: const pclass::TypeSystem *m_type_system; diff --git a/include/ki/util/BitStream.h b/include/ki/util/BitStream.h index 5e88780..23041ef 100644 --- a/include/ki/util/BitStream.h +++ b/include/ki/util/BitStream.h @@ -15,7 +15,7 @@ namespace ki /** * */ - class BitBufferBase + class IBitBuffer { friend BitBufferSegment; @@ -53,7 +53,7 @@ namespace ki void set_bit(int bit); }; - virtual ~BitBufferBase() {}; + virtual ~IBitBuffer() {}; /** * @returns @@ -153,9 +153,9 @@ namespace ki }; /** - * + * TODO: Documentation */ - class BitBuffer : public BitBufferBase + class BitBuffer : public IBitBuffer { public: 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; /** - * @copydoc BitBufferBase::read(buffer_pos, uint8_t) + * @copydoc IBitBuffer::read(buffer_pos, uint8_t) */ template ValueT read(const buffer_pos position, const uint8_t bits = bitsizeof::value) const { - return BitBufferBase::read(position, bits); + return IBitBuffer::read(position, bits); } /** - * @copydoc BitBufferBase::write(ValueT, buffer_pos, uint8_t) + * @copydoc IBitBuffer::write(ValueT, buffer_pos, uint8_t) */ template void write(const ValueT value, const buffer_pos position, const uint8_t bits = bitsizeof::value) { - return BitBufferBase::write(value, position, bits); + return IBitBuffer::write(value, position, bits); } protected: @@ -205,12 +205,12 @@ namespace ki }; /** - * + * TODO: Documentation */ - class BitBufferSegment : public BitBufferBase + class BitBufferSegment : public IBitBuffer { 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; void resize(std::size_t new_size) override; @@ -218,23 +218,23 @@ namespace ki BitBufferSegment *segment(buffer_pos from, std::size_t bitsize) override; /** - * @copydoc BitBufferBase::read(buffer_pos, uint8_t) + * @copydoc IBitBuffer::read(buffer_pos, uint8_t) */ template ValueT read(const buffer_pos position, const uint8_t bits = bitsizeof::value) const { - return BitBufferBase::read(position, bits); + return IBitBuffer::read(position, bits); } /** - * @copydoc BitBufferBase::write(ValueT, buffer_pos, uint8_t) + * @copydoc IBitBuffer::write(ValueT, buffer_pos, uint8_t) */ template void write(const ValueT value, const buffer_pos position, const uint8_t bits = bitsizeof::value) { - return BitBufferBase::write(value, position, bits); + return IBitBuffer::write(value, position, bits); } protected: @@ -242,7 +242,7 @@ namespace ki void write(uint64_t value, buffer_pos position, uint8_t bits) override; private: - BitBufferBase *m_buffer; + IBitBuffer *m_buffer; buffer_pos m_from; std::size_t m_bitsize; }; @@ -255,7 +255,7 @@ namespace ki public: using stream_pos = BitBuffer::buffer_pos; - explicit BitStream(BitBufferBase &buffer); + explicit BitStream(IBitBuffer &buffer); virtual ~BitStream(); BitStream(const BitStream &that); @@ -281,7 +281,7 @@ namespace ki /** * @returns The BitBuffer that the stream is reading/writing to. */ - BitBufferBase &buffer() const; + IBitBuffer &buffer() const; /** * Reads a value from the buffer. @@ -333,7 +333,7 @@ namespace ki void write_copy(uint8_t *src, std::size_t bitsize); private: - BitBufferBase *m_buffer; + IBitBuffer *m_buffer; stream_pos m_position; /** diff --git a/src/pclass/ClassType.cpp b/src/pclass/ClassType.cpp index 34958c1..0224101 100644 --- a/src/pclass/ClassType.cpp +++ b/src/pclass/ClassType.cpp @@ -6,7 +6,7 @@ namespace ki { namespace pclass { - ClassTypeBase::ClassTypeBase(const std::string& name, + IClassType::IClassType(const std::string& name, const Type* base_class, const TypeSystem& type_system) : Type(name, type_system) { @@ -19,16 +19,32 @@ namespace pclass if (base_class->get_kind() != kind::CLASS) throw runtime_error("base_class must be a class type!"); - // Cast the base class up to a ClassTypeBase pointer - m_base_class = dynamic_cast(base_class); + // Cast the base class up to a IClassType pointer + m_base_class = dynamic_cast(base_class); if (!m_base_class) - throw runtime_error("base_class must inherit ClassTypeBase!"); + throw runtime_error("base_class must inherit IClassType!"); } else 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 // if they report that they do since these checks are to make sure that objects diff --git a/src/pclass/Property.cpp b/src/pclass/Property.cpp index 4d81e18..9365737 100644 --- a/src/pclass/Property.cpp +++ b/src/pclass/Property.cpp @@ -7,7 +7,7 @@ namespace ki { namespace pclass { - PropertyBase::PropertyBase(PropertyClass &object, + IProperty::IProperty(PropertyClass &object, const std::string &name, const Type &type) { m_instance = &object; @@ -23,8 +23,8 @@ namespace pclass object.add_property(*this); } - PropertyBase::PropertyBase(PropertyClass &object, - const PropertyBase &that) + IProperty::IProperty(PropertyClass &object, + const IProperty &that) { m_instance = &object; m_name = that.m_name; @@ -36,81 +36,81 @@ namespace pclass object.add_property(*this); } - const PropertyClass &PropertyBase::get_instance() const + const PropertyClass &IProperty::get_instance() const { return *m_instance; } - std::string PropertyBase::get_name() const + std::string IProperty::get_name() const { return m_name; } - hash_t PropertyBase::get_name_hash() const + hash_t IProperty::get_name_hash() const { return m_name_hash; } - hash_t PropertyBase::get_full_hash() const + hash_t IProperty::get_full_hash() const { return m_full_hash; } - const Type &PropertyBase::get_type() const + const Type &IProperty::get_type() const { return *m_type; } - bool PropertyBase::is_pointer() const + bool IProperty::is_pointer() const { return false; } - bool PropertyBase::is_dynamic() const + bool IProperty::is_dynamic() const { return false; } - DynamicPropertyBase::DynamicPropertyBase(PropertyClass &object, + IDynamicProperty::IDynamicProperty(PropertyClass &object, const std::string& name, const Type& type) - : PropertyBase(object, name, type) + : IProperty(object, name, type) {} - DynamicPropertyBase::DynamicPropertyBase(PropertyClass &object, - const DynamicPropertyBase &that) - : PropertyBase(object, that) + IDynamicProperty::IDynamicProperty(PropertyClass &object, + const IDynamicProperty &that) + : IProperty(object, that) {} - bool DynamicPropertyBase::is_dynamic() const + bool IDynamicProperty::is_dynamic() const { return true; } - Value DynamicPropertyBase::get_value() const + Value IDynamicProperty::get_value() const { // The caller must specify an index 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 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 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 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 throw runtime_error("Called read_value_from() on a dynamic property. Use read_value_from(index) instead."); diff --git a/src/pclass/PropertyClass.cpp b/src/pclass/PropertyClass.cpp index fb432e2..6b99548 100644 --- a/src/pclass/PropertyClass.cpp +++ b/src/pclass/PropertyClass.cpp @@ -42,7 +42,7 @@ namespace pclass return new PropertyClass(*this); } - void PropertyClass::add_property(PropertyBase &prop) + void PropertyClass::add_property(IProperty &prop) { m_properties.add_property(prop); } diff --git a/src/pclass/PropertyList.cpp b/src/pclass/PropertyList.cpp index 2201c71..fcc3b46 100644 --- a/src/pclass/PropertyList.cpp +++ b/src/pclass/PropertyList.cpp @@ -8,12 +8,12 @@ namespace ki { namespace pclass { - PropertyBase &PropertyList::iterator::operator*() const + IProperty &PropertyList::iterator::operator*() const { return m_list->get_property(m_index); } - PropertyBase *PropertyList::iterator::operator->() const + IProperty *PropertyList::iterator::operator->() const { return &operator*(); } @@ -48,12 +48,12 @@ namespace pclass m_index = index; } - const PropertyBase &PropertyList::const_iterator::operator*() const + const IProperty &PropertyList::const_iterator::operator*() const { return m_list->get_property(m_index); } - const PropertyBase *PropertyList::const_iterator::operator->() const + const IProperty *PropertyList::const_iterator::operator->() const { return &operator*(); } @@ -105,7 +105,7 @@ namespace pclass != 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()) return *m_properties[index]; @@ -116,7 +116,7 @@ namespace pclass 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); if (it != m_property_name_lookup.end()) @@ -127,7 +127,7 @@ namespace pclass 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); if (it != m_property_hash_lookup.end()) @@ -140,7 +140,7 @@ namespace pclass 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()) return *m_properties[index]; @@ -151,7 +151,7 @@ namespace pclass 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); if (it != m_property_name_lookup.end()) @@ -162,7 +162,7 @@ namespace pclass 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); if (it != m_property_hash_lookup.end()) @@ -195,7 +195,7 @@ namespace pclass 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 if (has_property(prop.get_name())) diff --git a/src/pclass/Type.cpp b/src/pclass/Type.cpp index 76e877e..b60344f 100644 --- a/src/pclass/Type.cpp +++ b/src/pclass/Type.cpp @@ -60,13 +60,6 @@ namespace pclass 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( const Type &expected, const Type &actual, @@ -77,7 +70,7 @@ namespace pclass if (allow_inheritance && expected.get_kind() == Type::kind::CLASS) { - const auto &actual_class = dynamic_cast(actual); + const auto &actual_class = dynamic_cast(actual); if (actual_class.inherits(expected)) return; } diff --git a/src/pclass/TypeSystem.cpp b/src/pclass/TypeSystem.cpp index 1c5f4ec..35376be 100644 --- a/src/pclass/TypeSystem.cpp +++ b/src/pclass/TypeSystem.cpp @@ -90,26 +90,9 @@ namespace pclass // Make sure the hash calculator isn't null if (m_hash_calculator == nullptr) throw runtime_error("TypeSystem::get_hash_calculator() called but hash calculator is null."); - 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 { return m_type_name_lookup.find(name) != m_type_name_lookup.end(); diff --git a/src/serialization/SerializerBinary.cpp b/src/serialization/SerializerBinary.cpp index 8028353..5162a50 100644 --- a/src/serialization/SerializerBinary.cpp +++ b/src/serialization/SerializerBinary.cpp @@ -6,16 +6,16 @@ namespace ki { namespace serialization { - SerializerBinary::SerializerBinary(const pclass::TypeSystem* type_system, + SerializerBinary::SerializerBinary(const pclass::TypeSystem &type_system, const bool is_file, const flags flags) { - m_type_system = type_system; + m_type_system = &type_system; m_is_file = is_file; m_flags = flags; m_root_object = nullptr; } - void SerializerBinary::save(const pclass::PropertyClass* object, BitStream &stream) + void SerializerBinary::save(const pclass::PropertyClass *object, BitStream &stream) { // Write the serializer flags if (FLAG_IS_SET(m_flags, flags::WRITE_SERIALIZER_FLAGS)) @@ -164,7 +164,7 @@ namespace serialization 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 const auto start_pos = stream.tell(); @@ -181,9 +181,9 @@ namespace serialization auto &property_type = prop.get_type(); if (prop.is_dynamic()) { - // Cast the property to a DynamicPropertyBase + // Cast the property to a IDynamicProperty const auto &dynamic_property = - dynamic_cast(prop); + dynamic_cast(prop); // Write the number of elements stream.write(dynamic_property.get_element_count()); @@ -239,7 +239,7 @@ namespace serialization BitStream &stream, const std::size_t size) { // 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); stream.seek(stream.tell() + size * 8, false); @@ -380,13 +380,13 @@ namespace serialization 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(); if (prop.is_dynamic()) { auto &dynamic_property = - dynamic_cast(prop); + dynamic_cast(prop); // How many elements are there in this dynamic property? const auto element_count = stream.read(); diff --git a/src/util/BitStream.cpp b/src/util/BitStream.cpp index fcab661..bdec477 100644 --- a/src/util/BitStream.cpp +++ b/src/util/BitStream.cpp @@ -6,39 +6,39 @@ 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; 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; 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); } - uint32_t BitBufferBase::buffer_pos::as_bits() const + uint32_t IBitBuffer::buffer_pos::as_bits() const { 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; } - uint8_t BitBufferBase::buffer_pos::get_bit() const + uint8_t IBitBuffer::buffer_pos::get_bit() const { return m_bit; } - void BitBufferBase::buffer_pos::set_bit(int bit) + void IBitBuffer::buffer_pos::set_bit(int bit) { if (bit < 0) { @@ -55,7 +55,7 @@ namespace ki m_bit = bit; } - BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator+( + IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator+( const buffer_pos &rhs) const { 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 { 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 { 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 { 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) { m_byte += rhs.m_byte; @@ -95,7 +95,7 @@ namespace ki return *this; } - BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator-=( + IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator-=( const buffer_pos rhs) { m_byte -= rhs.m_byte; @@ -103,51 +103,51 @@ namespace ki 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); 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); return *this; } - BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator++() + IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator++() { set_bit(m_bit + 1); return *this; } - BitBufferBase::buffer_pos& BitBufferBase::buffer_pos::operator--() + IBitBuffer::buffer_pos& IBitBuffer::buffer_pos::operator--() { set_bit(m_bit - 1); return *this; } - BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator++(int increment) + IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator++(int increment) { auto copy(*this); ++(*this); return copy; } - BitBufferBase::buffer_pos BitBufferBase::buffer_pos::operator--(int increment) + IBitBuffer::buffer_pos IBitBuffer::buffer_pos::operator--(int increment) { auto copy(*this); --(*this); return copy; } - BitBufferSegment *BitBufferBase::segment( + BitBufferSegment *IBitBuffer::segment( const buffer_pos from, const std::size_t 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) { 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 { auto bits_left = bitsize; @@ -242,7 +242,8 @@ namespace ki 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 - 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 // 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 // already written const uint8_t written_bits = bits - unwritten_bits; - const auto bit_mask = static_cast((1 << bit_count) - 1) << written_bits; + auto bit_mask_abs = static_cast((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 - 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(); + bit_mask_abs <<= position.get_bit(); // Write the bits into the byte we're currently at - m_buffer[position.get_byte()] |= value_byte; + auto ¤t_value = m_buffer[position.get_byte()]; + current_value = current_value & (~bit_mask_abs & 0xFF) | value_byte; unwritten_bits -= bit_count; // 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) { m_buffer = &buffer; @@ -343,7 +347,7 @@ namespace ki m_buffer->write(value, m_from + position, bits); } - BitStream::BitStream(BitBufferBase &buffer) + BitStream::BitStream(IBitBuffer &buffer) { m_buffer = &buffer; m_position = stream_pos(0, 0); @@ -367,7 +371,7 @@ namespace ki return *this; } - BitBufferBase::buffer_pos BitStream::tell() const + IBitBuffer::buffer_pos BitStream::tell() const { return m_position; } @@ -384,7 +388,7 @@ namespace ki return m_buffer->size(); } - BitBufferBase &BitStream::buffer() const + IBitBuffer &BitStream::buffer() const { return *m_buffer; }