InspIRCd  3.0
serialize.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Sadie Powell <[email protected]>
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 
23 class CoreExport Serializable
24 {
25  protected:
26  Serializable() { }
27 
28  public:
30  class CoreExport Data
31  {
32  public:
35 
38 
39  private:
41  ChildMap children;
42 
44  EntryMap entries;
45 
46  public:
48  const ChildMap& GetChildren() const { return children; }
49  ChildMap& GetChildren() { return children; }
50 
52  const EntryMap& GetEntries() const { return entries; }
53  EntryMap& GetEntries() { return entries; }
54 
59  Data& Load(const std::string& key, Data& out);
60 
65  Data& Load(const std::string& key, std::string& out);
66 
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 
87  Data& Store(const std::string& key, const Data& value);
88 
93  Data& Store(const std::string& key, const std::string& value);
94 
99  template <typename T>
100  Data& Store(const std::string& key, const T& value)
101  {
102  return Store(key, ConvToStr(value));
103  }
104  };
105 
110  virtual bool Deserialize(Data& data) = 0;
111 
116  virtual bool Serialize(Data& data) = 0;
117 };
Serializable::Data::Load
Data & Load(const std::string &key, T &out)
Definition: serialize.h:72
Serializable::Data::GetChildren
const ChildMap & GetChildren() const
Definition: serialize.h:48
Serializable
Definition: serialize.h:23
insp::flat_map< std::string, Data >
Serializable::Data
Definition: serialize.h:30
Serializable::Data::Store
Data & Store(const std::string &key, const T &value)
Definition: serialize.h:100
Serializable::Data::ChildMap
insp::flat_map< std::string, Data > ChildMap
Definition: serialize.h:34
Serializable::Data::GetEntries
const EntryMap & GetEntries() const
Definition: serialize.h:52
Serializable::Data::EntryMap
insp::flat_map< std::string, std::string > EntryMap
Definition: serialize.h:37