summaryrefslogtreecommitdiff
path: root/include/serialize.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/serialize.h')
-rw-r--r--include/serialize.h109
1 files changed, 64 insertions, 45 deletions
diff --git a/include/serialize.h b/include/serialize.h
index 09e8f0b14..ff8ed30d3 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -9,8 +9,7 @@
* Based on the original code of Services by Andy Church.
*/
-#ifndef SERIALIZE_H
-#define SERIALIZE_H
+#pragma once
#include <sstream>
@@ -19,23 +18,44 @@
namespace Serialize
{
+ enum class DataType
+ : uint8_t
+ {
+ BOOL,
+ FLOAT,
+ INT,
+ TEXT,
+ UINT,
+ };
+
class Data
{
- public:
- enum Type
- {
- DT_TEXT,
- DT_INT
- };
+ public:
+ virtual ~Data() = default;
+
+ virtual std::iostream &operator[](const Anope::string &key) = 0;
- virtual ~Data() { }
+ template <typename T>
+ void Store(const Anope::string &key, const T &value)
+ {
+ using Type = std::remove_cv_t<std::remove_reference_t<T>>;
+
+ if constexpr (std::is_same_v<Type, bool>)
+ SetType(key, DataType::BOOL);
+ else if constexpr (std::is_floating_point_v<Type>)
+ SetType(key, DataType::FLOAT);
+ else if constexpr (std::is_integral_v<Type> && std::is_signed_v<Type>)
+ SetType(key, DataType::INT);
+ else if constexpr (std::is_integral_v<Type> && std::is_unsigned_v<Type>)
+ SetType(key, DataType::UINT);
+
+ this->operator[](key) << value;
+ }
- virtual std::iostream& operator[](const Anope::string &key) = 0;
- virtual std::set<Anope::string> KeySet() const { throw CoreException("Not supported"); }
virtual size_t Hash() const { throw CoreException("Not supported"); }
- virtual void SetType(const Anope::string &key, Type t) { }
- virtual Type GetType(const Anope::string &key) const { return DT_TEXT; }
+ virtual void SetType(const Anope::string &key, DataType dt) { }
+ virtual DataType GetType(const Anope::string &key) const { return DataType::TEXT; }
};
extern void RegisterTypes();
@@ -50,9 +70,10 @@ namespace Serialize
* abstract data types (Serialize::Data), and then reconstructed or
* updated later at any time.
*/
-class CoreExport Serializable : public virtual Base
+class CoreExport Serializable
+ : public virtual Base
{
- private:
+private:
/* A list of every serializable item in Anope.
* Some of these are static and constructed at runtime,
* so this list must be on the heap, as it is not always
@@ -65,24 +86,24 @@ class CoreExport Serializable : public virtual Base
/* Iterator into serializable_items */
std::list<Serializable *>::iterator s_iter;
/* The hash of the last serialized form of this object committed to the database */
- size_t last_commit;
+ size_t last_commit = 0;
/* The last time this object was committed to the database */
- time_t last_commit_time;
+ time_t last_commit_time = 0;
- protected:
+protected:
Serializable(const Anope::string &serialize_type);
Serializable(const Serializable &);
Serializable &operator=(const Serializable &);
- public:
+public:
virtual ~Serializable();
/* Unique ID (per type, not globally) for this object */
- uint64_t id;
+ uint64_t id = 0;
/* Only used by redis, to ignore updates */
- unsigned short redis_ignore;
+ unsigned short redis_ignore = 0;
/** Marks the object as potentially being updated "soon".
*/
@@ -97,7 +118,7 @@ class CoreExport Serializable : public virtual Base
/** Get the type of serializable object this is
* @return The serializable object type
*/
- Serialize::Type* GetSerializableType() const { return this->s_type; }
+ Serialize::Type *GetSerializableType() const { return this->s_type; }
virtual void Serialize(Serialize::Data &data) const = 0;
@@ -108,9 +129,10 @@ class CoreExport Serializable : public virtual Base
* of class that inherits from Serializable. Used for unserializing objects
* of this type, as it requires a function pointer to a static member function.
*/
-class CoreExport Serialize::Type : public Base
+class CoreExport Serialize::Type final
+ : public Base
{
- typedef Serializable* (*unserialize_func)(Serializable *obj, Serialize::Data &);
+ typedef Serializable *(*unserialize_func)(Serializable *obj, Serialize::Data &);
static std::vector<Anope::string> TypeOrder;
static std::map<Anope::string, Serialize::Type *> Types;
@@ -127,9 +149,9 @@ class CoreExport Serialize::Type : public Base
* this timestamp. if curtime == timestamp then we have the most up to date
* version of every object of this type.
*/
- time_t timestamp;
+ time_t timestamp = 0;
- public:
+public:
/* Map of Serializable::id to Serializable objects */
std::map<uint64_t, Serializable *> objects;
@@ -167,7 +189,7 @@ class CoreExport Serialize::Type : public Base
*/
void UpdateTimestamp();
- Module* GetOwner() const { return this->owner; }
+ Module *GetOwner() const { return this->owner; }
static Serialize::Type *Find(const Anope::string &name);
@@ -187,7 +209,7 @@ class Serialize::Checker
{
Anope::string name;
T obj;
- mutable ::Reference<Serialize::Type> type;
+ mutable ::Reference<Serialize::Type> type = nullptr;
inline void Check() const
{
@@ -197,26 +219,26 @@ class Serialize::Checker
type->Check();
}
- public:
- Checker(const Anope::string &n) : name(n), type(NULL) { }
+public:
+ Checker(const Anope::string &n) : name(n) { }
- inline const T* operator->() const
+ inline const T *operator->() const
{
this->Check();
return &this->obj;
}
- inline T* operator->()
+ inline T *operator->()
{
this->Check();
return &this->obj;
}
- inline const T& operator*() const
+ inline const T &operator*() const
{
this->Check();
return this->obj;
}
- inline T& operator*()
+ inline T &operator*()
{
this->Check();
return this->obj;
@@ -241,15 +263,14 @@ class Serialize::Checker
* destructed.
*/
template<typename T>
-class Serialize::Reference : public ReferenceBase
+class Serialize::Reference final
+ : public ReferenceBase
{
- protected:
- T *ref;
+protected:
+ T *ref = nullptr;
- public:
- Reference() : ref(NULL)
- {
- }
+public:
+ Reference() = default;
Reference(T *obj) : ref(obj)
{
@@ -305,7 +326,7 @@ class Serialize::Reference : public ReferenceBase
return NULL;
}
- inline T* operator*() const
+ inline T *operator*() const
{
if (!this->invalid)
{
@@ -318,7 +339,7 @@ class Serialize::Reference : public ReferenceBase
return NULL;
}
- inline T* operator->() const
+ inline T *operator->() const
{
if (!this->invalid)
{
@@ -331,5 +352,3 @@ class Serialize::Reference : public ReferenceBase
return NULL;
}
};
-
-#endif // SERIALIZE_H