diff options
author | Sadie Powell <sadie@witchery.services> | 2025-03-12 16:52:46 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2025-03-12 16:52:46 +0000 |
commit | 9ec3f6abd6915848e9d3eb33a97ff213ea10aed2 (patch) | |
tree | 17a39dfe3e15f2b081616475991191a82cc9d26f | |
parent | e7b18609f6141b7ad4acb1e233aebd2fd48d775f (diff) |
Refactor and redocument Serialize::Type.
-rw-r--r-- | include/serialize.h | 107 | ||||
-rw-r--r-- | src/serialize.cpp | 15 |
2 files changed, 67 insertions, 55 deletions
diff --git a/include/serialize.h b/include/serialize.h index 43981cc20..440c8030c 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -136,74 +136,101 @@ public: static const std::list<Serializable *> &GetItems(); }; -/* A serializable type. There should be one of these classes for each type - * of class that inherits from Serializable. Used for unserializing objects - * of this type, as it requires a function pointer to a static member function. +/* A serializable type. There should be a single instance of a subclass of this + * for each subclass of Serializable as this is what is used to serialize and + * deserialize data from the database. */ class CoreExport Serialize::Type : public Base { - static std::vector<Anope::string> TypeOrder; - static std::map<Anope::string, Serialize::Type *> Types; - - /* The name of this type, should be a class name */ +private: + /** The name of this type in the database (e.g. NickAlias). */ Anope::string name; - /* Owner of this type. Used for placing objects of this type in separate databases - * based on what module, if any, owns it. + + /** The module which owns this type, or nullptr if it belongs to the core. + * Some database backends use this to put third-party module data into their + * own database. */ Module *owner; - /* The timestamp for this type. All objects of this type are as up to date as - * this timestamp. if curtime == timestamp then we have the most up to date - * version of every object of this type. + /** The time at which this type was last synchronised with the database. + * Only used by live database backends like db_sql_live. */ time_t timestamp = 0; + /* The names of currently registered types in order of registration. */ + static std::vector<Anope::string> TypeOrder; + + /** The currently registered types. */ + static std::map<Anope::string, Serialize::Type *> Types; + +protected: + /** Creates a new serializable type. + * @param n The name of the type . This should match the value passed in the + * constructor of the equivalent Serializable type. + * @param o The module which owns this type, or nullptr if it belongs to the + * core. + */ + Type(const Anope::string &n, Module *o = nullptr); + public: - /* Map of Serializable::id to Serializable objects */ + /* Map of Serializable objects of this type keyed by their object id. */ std::map<uint64_t, Serializable *> objects; - /** Creates a new serializable type - * @param n Type name - * @param owner Owner of this type. Leave NULL for the core. - */ - Type(const Anope::string &n, Module *owner = NULL); + /** Destroys a serializable type. */ ~Type(); - /** Gets the name for this type - * @return The name, eg "NickAlias" - */ - const Anope::string &GetName() const { return this->name; } + /** Checks for and applies any pending object updates for this type. */ + void Check(); - /** Unserialized an object. - * @param obj NULL if this object doesn't yet exist. If this isn't NULL, instead - * update the contents of this object. - * @param data The data to unserialize - * @return The unserialized object. If obj != NULL this should be obj. + /** Attempts to find a serializable type with the specified name. + * @param n The name of the serializable type to find. */ - virtual void Serialize(const Serializable *obj, Serialize::Data &data) const = 0; - virtual Serializable *Unserialize(Serializable *obj, Serialize::Data &data) const = 0; + static Serialize::Type *Find(const Anope::string &n); - /** Check if this object type has any pending changes and update them. + /** Retrieves the name of this type in the database (e.g. NickAlias). */ + inline const auto &GetName() const { return this->name; } + + /** Retrieves the module which owns this type, or nullptr if it belongs to + * the core. Some database backends use this to put third-party module data + * into their own database. */ - void Check(); + inline auto *GetOwner() const { return this->owner; } - /** Gets the timestamp for the object type. That is, the time we know - * all objects of this type are updated at least to. + /** Retrieves the time at which this type was last synchronised with the + * database. Only used by live database backends like db_sql_live. */ - time_t GetTimestamp() const; + inline auto GetTimestamp() const { return this->timestamp; }; - /** Bumps object type timestamp to current time + /** Retrieves the names of currently registered types in order of + * registration. */ - void UpdateTimestamp(); + inline static const auto &GetTypeOrder() { return TypeOrder; } - Module *GetOwner() const { return this->owner; } + /** Retrieves the currently registered types. */ + inline static const auto &GetTypes() { return Types; } - static Serialize::Type *Find(const Anope::string &name); + /** Serializes the specified object to the database. + * @param obj The object to serialise. This is guaranteed to be the correct + * type so you can cast it without any checks. + * @param data The database to serialize to. + */ + virtual void Serialize(const Serializable *obj, Serialize::Data &data) const = 0; - static const std::vector<Anope::string> &GetTypeOrder(); + /** Unserializes the specified object from the database. + * @param obj The object to unserialize into. If the object has not been + * unserialized yet this will be nullptr. This is guaranteed to + * be the correct type so you can cast it without any checks. + * @param data The database to unserialize from. + * @return The object specified in obj or a new object it if was nullptr. + */ + virtual Serializable *Unserialize(Serializable *obj, Serialize::Data &data) const = 0; - static const std::map<Anope::string, Serialize::Type *>& GetTypes(); + /** Updates the time at which this type was last synchronised with the + * database to the current time. Only used by live database backends like + * db_sql_live. + */ + void UpdateTimestamp(); }; /** Should be used to hold lists and other objects of a specific type, diff --git a/src/serialize.cpp b/src/serialize.cpp index e365ba01c..d820157b4 100644 --- a/src/serialize.cpp +++ b/src/serialize.cpp @@ -162,11 +162,6 @@ void Type::Check() FOREACH_MOD(OnSerializeCheck, (this)); } -time_t Type::GetTimestamp() const -{ - return this->timestamp; -} - void Type::UpdateTimestamp() { this->timestamp = Anope::CurTime; @@ -179,13 +174,3 @@ Type *Serialize::Type::Find(const Anope::string &name) return it->second; return NULL; } - -const std::vector<Anope::string> &Type::GetTypeOrder() -{ - return TypeOrder; -} - -const std::map<Anope::string, Serialize::Type *>& Type::GetTypes() -{ - return Types; -} |