GnuCash  5.6-150-g038405b370+
gnc-optiondb-impl.hpp
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-optiondb.hpp -- Collection of GncOption objects *
3  * Copyright (C) 2019 John Ralls <jralls@ceridwen.us> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
32 #ifndef GNC_OPTIONDB_P_HPP_
33 #define GNC_OPTIONDB_P_HPP_
34 
35 #include "gnc-option.hpp"
36 #include "gnc-option-impl.hpp"
37 
38 #include <functional>
39 #include <exception>
40 #include <optional>
41 #include <iostream>
42 
43 #include <config.h>
44 #include "qof.h"
45 #include "gncInvoice.h"
46 #include "gncCustomer.h"
47 #include "gncEmployee.h"
48 #include "gncJob.h"
49 #include "gncVendor.h"
50 #include "gncTaxTable.h"
51 
52 using GncOptionVec = std::vector<GncOption>;
53 
59 {
60  std::string m_name;
61  GncOptionVec m_options;
62 public:
63  GncOptionSection(const char* name) : m_name{name}, m_options{} {}
64  ~GncOptionSection() = default;
65 
66  void foreach_option(std::function<void(GncOption&)> func);
67  void foreach_option(std::function<void(const GncOption&)> func) const;
68  const std::string& get_name() const noexcept { return m_name; }
69  size_t get_num_options() const noexcept { return m_options.size(); }
70  void add_option(GncOption&& option);
71  void remove_option(const char* name);
72  const GncOption* find_option(const char* name) const;
73 };
74 
75 using GncOptionSectionPtr = std::shared_ptr<GncOptionSection>;
76 
77 inline bool
78 operator<(const GncOptionSectionPtr& right, const GncOptionSectionPtr& left)
79 {
80  return right->get_name() < left->get_name();
81 }
82 
83 using GncOptionDBChangeCallback = void (*)(void* user_data);
84 
86 {
87  GncOptionDBCallback(size_t id, GncOptionDBChangeCallback func,
88  void* data) :
89  m_id{id}, m_func{func}, m_data{data} {}
90  ~GncOptionDBCallback() = default;
93  GncOptionDBCallback& operator=(const GncOptionDBCallback&) = default;
94  GncOptionDBCallback& operator=(GncOptionDBCallback&&) = default;
95 
96  size_t m_id;
97  GncOptionDBChangeCallback m_func;
98  void* m_data;
99 };
100 
101 using GncCallbackVec = std::vector<GncOptionDBCallback>;
102 
108 {
109 public:
110  GncOptionDB();
111  GncOptionDB(QofBook* book);
112  ~GncOptionDB() = default;
113 
114 /* The non-const version can't be redirected to the const one because the
115  * function parameters are incompatible.
116  */
117  void foreach_section(std::function<void(GncOptionSectionPtr&)> func)
118  {
119  for (auto& section : m_sections)
120  func(section);
121  }
122  void foreach_section(std::function<void(const GncOptionSectionPtr&)> func) const
123  {
124  for (auto& section : m_sections)
125  func(section);
126  }
127  size_t num_sections() const noexcept { return m_sections.size(); }
128  bool get_changed() const noexcept { return m_dirty; }
129  void register_option(const char* section, GncOption&& option);
130  void register_option(const char* section, GncOption* option);
131  void unregister_option(const char* section, const char* name);
132  void set_default_section(const char* section);
133  const GncOptionSection* const get_default_section() const noexcept;
134  std::string lookup_string_option(const char* section, const char* name);
135  bool set_string_option(const char* section, const char* name, const std::string& value)
136  {
137  return set_option<std::string>(section, name, value);
138  }
139  template <typename ValueType>
140  bool set_option(const char* section, const char* name, ValueType value)
141  {
142  try
143  {
144  auto option{find_option(section, name)};
145  if (!option)
146  return false;
147  option->set_value(value);
148  return true;
149  }
150  catch(const std::invalid_argument& err)
151  {
152  printf("Set Failed: %s\n", err.what());
153  return false;
154  }
155  }
156 // void set_selectable(const char* section, const char* name);
157  void make_internal(const char* section, const char* name);
158  void commit() {};
159  GncOptionSection* find_section(const std::string& sectname)
160  {
161  return const_cast<GncOptionSection*>(static_cast<const GncOptionDB&>(*this).find_section(sectname));
162  }
163  const GncOptionSection* find_section(const std::string& sectname) const;
164  GncOption* find_option(const std::string& section, const char* name)
165  {
166  return const_cast<GncOption*>(static_cast<const GncOptionDB&>(*this).find_option(section, name));
167  }
168  const GncOption* find_option(const std::string& section, const char* name) const;
169  std::ostream& save_to_key_value(std::ostream& oss) const noexcept;
170  std::istream& load_from_key_value(std::istream& iss);
171  void save_to_kvp(QofBook* book, bool clear_book) const noexcept;
172  void load_from_kvp(QofBook* book) noexcept;
173  std::ostream& save_option_key_value(std::ostream& oss,
174  const std::string& section,
175  const std::string& name) const noexcept;
176  std::istream& load_option_key_value(std::istream& iss);
177  size_t register_callback(GncOptionDBChangeCallback, void*);
178  void unregister_callback(size_t);
179  void run_callbacks();
180 private:
181  GncOptionSection* m_default_section;
182  std::vector<GncOptionSectionPtr> m_sections;
183  bool m_dirty = false;
184  GncCallbackVec m_callbacks{};
185 
186  std::function<GncOptionUIItem*()> m_get_ui_value;
187  std::function<void(GncOptionUIItem*)> m_set_ui_value;
188 };
189 
190 
191 #endif // GNC_OPTIONDB_P_HPP_
192 
Holds all of the options for a book, report, or stylesheet, organized by GncOptionSections.
Core Customer Interface.
Represents the public interface for an option.
Definition: gnc-option.hpp:135
C++ Public interface for individual options.
class GncOptionSection The upper-level classification implementation.
Tax Table programming interface.
Implementation templates and specializtions for GncOption values.
Business Invoice Interface.
Job Interface.
Employee Interface.
Vendor Interface.