summaryrefslogtreecommitdiff
path: root/include/serialize.h
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2024-08-14 02:40:48 +0100
committerSadie Powell <sadie@witchery.services>2024-08-14 05:47:55 +0100
commit528b5938ec71abed396248cf5e00f346e685aaea (patch)
treee923fed32be3fb62f946d44c633cd85ce855550b /include/serialize.h
parent03bee1706383d2766923e5d8edbb90d7ad1948b6 (diff)
Automatically determine SQL column type from the field.
Also add more column types to ensure we are storing data in the best format in the database.
Diffstat (limited to 'include/serialize.h')
-rw-r--r--include/serialize.h38
1 files changed, 30 insertions, 8 deletions
diff --git a/include/serialize.h b/include/serialize.h
index e53295dc0..ff8ed30d3 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -18,22 +18,44 @@
namespace Serialize
{
+ enum class DataType
+ : uint8_t
+ {
+ BOOL,
+ FLOAT,
+ INT,
+ TEXT,
+ UINT,
+ };
+
class Data
{
public:
- enum Type
- {
- DT_TEXT,
- DT_INT
- };
-
virtual ~Data() = default;
virtual std::iostream &operator[](const Anope::string &key) = 0;
+
+ 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 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();