]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/serialize.h
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / include / serialize.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019-2020 Sadie Powell <sadie@witchery.services>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 /** Base class for serializable elements. */
23 class CoreExport Serializable
24 {
25  protected:
26         Serializable() { }
27
28  public:
29         /** Encapsulates a chunk of serialised data. */
30         class CoreExport Data
31         {
32         public:
33                 /** Maps keys to serialised data. */
34                 typedef insp::flat_map<std::string, Data> ChildMap;
35
36                 /** Maps keys to simple values. */
37                 typedef insp::flat_map<std::string, std::string> EntryMap;
38
39         private:
40                 /** A mapping of keys to serialised data. */
41                 ChildMap children;
42
43                 /** A mapping of keys to values. */
44                 EntryMap entries;
45
46          public:
47                 /** Retrieves the child elements. */
48                 const ChildMap& GetChildren() const { return children; }
49                 ChildMap& GetChildren() { return children; }
50
51                 /** Retrieves the key/value map. */
52                 const EntryMap& GetEntries() const { return entries; }
53                 EntryMap& GetEntries() { return entries; }
54
55                 /** Loads the serialised data with the specified key.
56                  * @param key The key by which this serialised data is identified.
57                  * @param out The location to store the serialised data for this key.
58                  */
59                 Data& Load(const std::string& key, Data& out);
60
61                 /** Loads the value with the specified key.
62                  * @param key The key by which this data is identified.
63                  * @param out The location to store the value for this keu
64                  */
65                 Data& Load(const std::string& key, std::string& out);
66
67                 /** Loads the value with the specified key. The value will be converted to the specified type.
68                  * @param key The key by which this data is identified.
69                  * @param out The location to store the value for this key.
70                  */
71                 template <typename T>
72                 Data& Load(const std::string& key, T& out)
73                 {
74                         // Attempt to load as a string.
75                         std::string str;
76                         Load(key, str);
77
78                         std::stringstream ss(str);
79                         ss >> out;
80                         return *this;
81                 }
82
83                 /** Stores the serialised data against the specified key.
84                  * @param key The key by which this serialised data should be stored against.
85                  * @param value The serialised data to store.
86                  */
87                 Data& Store(const std::string& key, const Data& value);
88
89                 /** Stores the value against the specified key.
90                  * @param key The key by which this value should be stored against.
91                  * @param value The value to store.
92                  */
93                 Data& Store(const std::string& key, const std::string& value);
94
95                 /** Stores the value against the specified key. The value will be converted to a string using ConvToStr.
96                  * @param key The key by which this value should be stored against.
97                  * @param value The value to store.
98                  */
99                 template <typename T>
100                 Data& Store(const std::string& key, const T& value)
101                 {
102                         return Store(key, ConvToStr(value));
103                 }
104         };
105
106         /** Deserializes the specified Data instance into this object.
107          * @param data The Data object to deserialize from.
108          * @return True if the deserialisation succeeded; otherwise, false.
109          */
110         virtual bool Deserialize(Data& data) = 0;
111
112         /** Serializes the this object into the specified Data obect.
113          * @param data The Data object to serialize to.
114          * @return True if the serialisation succeeded; otherwise, false.
115          */
116         virtual bool Serialize(Data& data) = 0;
117 };