GnuCash  4.8a-134-g214de30c7+
kvp-frame.hpp
1 /********************************************************************\
2  * kvp-frame.hpp -- Implements a key-value frame system *
3  * Copyright (C) 2014 Aaron Laws *
4  * Copyright 2015 John Ralls *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
84 #ifndef GNC_KVP_FRAME_TYPE
85 #define GNC_KVP_FRAME_TYPE
86 
87 #include "kvp-value.hpp"
88 #include <map>
89 #include <string>
90 #include <vector>
91 #include <cstring>
92 #include <algorithm>
93 #include <iostream>
94 using Path = std::vector<std::string>;
95 using KvpEntry = std::pair <std::vector <std::string>, KvpValue*>;
96 
110 {
112  {
113  public:
114  /* Returns true if one is less than two. */
115  bool operator()(const char * one, const char * two) const
116  {
117  auto ret = std::strcmp(one, two) < 0;
118  return ret;
119  }
120  };
121  using map_type = std::map<const char *, KvpValue*, cstring_comparer>;
122 
123  public:
124  KvpFrameImpl() noexcept {};
125 
129  KvpFrameImpl(const KvpFrameImpl &) noexcept;
130 
134  ~KvpFrameImpl() noexcept;
135 
145  //KvpValue* set(const char * key, KvpValue* newvalue) noexcept;
159  KvpValue* set(Path path, KvpValue* newvalue) noexcept;
171  KvpValue* set_path(Path path, KvpValue* newvalue) noexcept;
176  std::string to_string() const noexcept;
182  std::string to_string(std::string const &) const noexcept;
188  std::vector<std::string> get_keys() const noexcept;
189 
194  KvpValue* get_slot(Path keys) noexcept;
195 
200  template <typename func_type, typename data_type>
201  void for_each_slot_temp(func_type const &, data_type &) const noexcept;
202 
203  template <typename func_type>
204  void for_each_slot_temp(func_type const &) const noexcept;
205 
210  template <typename func_type, typename data_type>
211  void for_each_slot_prefix(std::string const & prefix, func_type const &, data_type &) const noexcept;
212 
213  template <typename func_type>
214  void for_each_slot_prefix(std::string const & prefix, func_type const &) const noexcept;
215 
220  std::vector <KvpEntry>
221  flatten_kvp(void) const noexcept;
222 
226  bool empty() const noexcept { return m_valuemap.empty(); }
227  friend int compare(const KvpFrameImpl&, const KvpFrameImpl&) noexcept;
228 
229  private:
230  map_type m_valuemap;
231 
232  KvpFrame * get_child_frame_or_nullptr (Path const &) noexcept;
233  KvpFrame * get_child_frame_or_create (Path const &) noexcept;
234  void flatten_kvp_impl(std::vector <std::string>, std::vector <KvpEntry> &) const noexcept;
235  KvpValue * set_impl (std::string const &, KvpValue *) noexcept;
236 };
237 
238 template<typename func_type, typename data_type>
239 void KvpFrame::for_each_slot_prefix(std::string const & prefix,
240  func_type const & func, data_type & data) const noexcept
241 {
242  std::for_each (m_valuemap.begin(), m_valuemap.end(),
243  [&prefix,&func,&data](const KvpFrameImpl::map_type::value_type & a)
244  {
245  /* Testing for prefix matching */
246  if (strncmp(a.first, prefix.c_str(), prefix.size()) == 0)
247  func (&a.first[prefix.size()], a.second, data);
248  }
249  );
250 }
251 
252 template <typename func_type>
253 void KvpFrame::for_each_slot_temp(func_type const & func) const noexcept
254 {
255  std::for_each (m_valuemap.begin(), m_valuemap.end(),
256  [&func](const KvpFrameImpl::map_type::value_type & a)
257  {
258  func (a.first, a.second);
259  }
260  );
261 }
262 
263 template <typename func_type, typename data_type>
264 void KvpFrame::for_each_slot_temp(func_type const & func, data_type & data) const noexcept
265 {
266  std::for_each (m_valuemap.begin(), m_valuemap.end(),
267  [&func,&data](const KvpFrameImpl::map_type::value_type & a)
268  {
269  func (a.first, a.second, data);
270  }
271  );
272 }
273 
274 int compare (const KvpFrameImpl &, const KvpFrameImpl &) noexcept;
275 int compare (const KvpFrameImpl *, const KvpFrameImpl *) noexcept;
278 #endif
void for_each_slot_temp(func_type const &, data_type &) const noexcept
The function should be of the form: <anything> func (char const *, KvpValue *, data_type &); Do not p...
~KvpFrameImpl() noexcept
Perform a deep delete.
Definition: kvp-frame.cpp:60
Implements KvpFrame.
Definition: kvp-frame.hpp:109
STL namespace.
std::string to_string() const noexcept
Make a string representation of the frame.
Definition: kvp-frame.cpp:160
std::vector< KvpEntry > flatten_kvp(void) const noexcept
Returns all keys and values of this frame recursively, flattening the frame-containing values...
KvpValue * set(Path path, KvpValue *newvalue) noexcept
Set the value with the key in the immediate frame, replacing and returning the old value if it exists...
Definition: kvp-frame.cpp:122
bool empty() const noexcept
Test for emptiness.
Definition: kvp-frame.hpp:226
void for_each_slot_prefix(std::string const &prefix, func_type const &, data_type &) const noexcept
Like for_each_slot, but doesn&#39;t traverse nested values.
friend int compare(const KvpFrameImpl &, const KvpFrameImpl &) noexcept
If the first KvpFrameImpl has an item that the second does not, 1 is returned.
Definition: kvp-frame.cpp:217
std::vector< std::string > get_keys() const noexcept
Report the keys in the immediate frame.
Definition: kvp-frame.cpp:190
KvpValue * get_slot(Path keys) noexcept
Get the value for the tail of the path or nullptr if it doesn&#39;t exist.
Definition: kvp-frame.cpp:146
KvpValue * set_path(Path path, KvpValue *newvalue) noexcept
Set the value with the key in a subframe following the keys in path, replacing and returning the old ...
Definition: kvp-frame.cpp:135