2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019-2020 Sadie Powell <sadie@witchery.services>
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.
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
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/>.
22 /** Base class for serializable elements. */
23 class CoreExport Serializable
29 /** Encapsulates a chunk of serialised data. */
33 /** Maps keys to serialised data. */
34 typedef insp::flat_map<std::string, Data> ChildMap;
36 /** Maps keys to simple values. */
37 typedef insp::flat_map<std::string, std::string> EntryMap;
40 /** A mapping of keys to serialised data. */
43 /** A mapping of keys to values. */
47 /** Retrieves the child elements. */
48 const ChildMap& GetChildren() const { return children; }
49 ChildMap& GetChildren() { return children; }
51 /** Retrieves the key/value map. */
52 const EntryMap& GetEntries() const { return entries; }
53 EntryMap& GetEntries() { return entries; }
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.
59 Data& Load(const std::string& key, Data& out);
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
65 Data& Load(const std::string& key, std::string& out);
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.
72 Data& Load(const std::string& key, T& out)
74 // Attempt to load as a string.
78 std::stringstream ss(str);
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.
87 Data& Store(const std::string& key, const Data& value);
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.
93 Data& Store(const std::string& key, const std::string& value);
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.
100 Data& Store(const std::string& key, const T& value)
102 return Store(key, ConvToStr(value));
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.
110 virtual bool Deserialize(Data& data) = 0;
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.
116 virtual bool Serialize(Data& data) = 0;