GnuCash  4.11-517-g41de4cefce
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 extern "C"
43 {
44 #include <config.h>
45 #include <qof.h>
46 #include <gncInvoice.h>
47 #include <gncCustomer.h>
48 #include <gncEmployee.h>
49 #include <gncJob.h>
50 #include <gncVendor.h>
51 #include <gncTaxTable.h>
52 }
53 
54 using GncOptionVec = std::vector<GncOption>;
55 
61 {
62  std::string m_name;
63  GncOptionVec m_options;
64 public:
65  GncOptionSection(const char* name) : m_name{name}, m_options{} {}
66  ~GncOptionSection() = default;
67 
68  void foreach_option(std::function<void(GncOption&)> func);
69  void foreach_option(std::function<void(const GncOption&)> func) const;
70  const std::string& get_name() const noexcept { return m_name; }
71  size_t get_num_options() const noexcept { return m_options.size(); }
72  void add_option(GncOption&& option);
73  void remove_option(const char* name);
74  const GncOption* find_option(const char* name) const;
75 };
76 
77 using GncOptionSectionPtr = std::shared_ptr<GncOptionSection>;
78 
79 inline bool
80 operator<(const GncOptionSectionPtr& right, const GncOptionSectionPtr& left)
81 {
82  return right->get_name() < left->get_name();
83 }
84 
85 using GncOptionDBChangeCallback = void (*)(void* user_data);
86 
88 {
89  GncOptionDBCallback(size_t id, GncOptionDBChangeCallback func,
90  void* data) :
91  m_id{id}, m_func{func}, m_data{data} {}
92  ~GncOptionDBCallback() = default;
95  GncOptionDBCallback& operator=(const GncOptionDBCallback&) = default;
96  GncOptionDBCallback& operator=(GncOptionDBCallback&&) = default;
97 
98  size_t m_id;
99  GncOptionDBChangeCallback m_func;
100  void* m_data;
101 };
102 
103 using GncCallbackVec = std::vector<GncOptionDBCallback>;
104 
110 {
111 public:
112  GncOptionDB();
113  GncOptionDB(QofBook* book);
114  ~GncOptionDB() = default;
115 
116 /* The non-const version can't be redirected to the const one because the
117  * function parameters are incompatible.
118  */
119  void foreach_section(std::function<void(GncOptionSectionPtr&)> func)
120  {
121  for (auto& section : m_sections)
122  func(section);
123  }
124  void foreach_section(std::function<void(const GncOptionSectionPtr&)> func) const
125  {
126  for (auto& section : m_sections)
127  func(section);
128  }
129  size_t num_sections() const noexcept { return m_sections.size(); }
130  bool get_changed() const noexcept { return m_dirty; }
131  void register_option(const char* section, GncOption&& option);
132  void register_option(const char* section, GncOption* option);
133  void unregister_option(const char* section, const char* name);
134  void set_default_section(const char* section);
135  const GncOptionSection* const get_default_section() const noexcept;
136  std::string lookup_string_option(const char* section, const char* name);
137  template <typename ValueType>
138  bool set_option(const char* section, const char* name, ValueType value)
139  {
140  try
141  {
142  auto option{find_option(section, name)};
143  if (!option)
144  return false;
145  option->set_value(value);
146  return true;
147  }
148  catch(const std::invalid_argument& err)
149  {
150  printf("Set Failed: %s\n", err.what());
151  return false;
152  }
153  }
154 // void set_selectable(const char* section, const char* name);
155  void make_internal(const char* section, const char* name);
156  void commit() {};
157  GncOptionSection* find_section(const std::string& sectname)
158  {
159  return const_cast<GncOptionSection*>(static_cast<const GncOptionDB&>(*this).find_section(sectname));
160  }
161  const GncOptionSection* find_section(const std::string& sectname) const;
162  GncOption* find_option(const std::string& section, const char* name)
163  {
164  return const_cast<GncOption*>(static_cast<const GncOptionDB&>(*this).find_option(section, name));
165  }
166  const GncOption* find_option(const std::string& section, const char* name) const;
167  std::ostream& save_to_key_value(std::ostream& oss) const noexcept;
168  std::istream& load_from_key_value(std::istream& iss);
169  void save_to_kvp(QofBook* book, bool clear_book) const noexcept;
170  void load_from_kvp(QofBook* book) noexcept;
171  std::ostream& save_option_key_value(std::ostream& oss,
172  const std::string& section,
173  const std::string& name) const noexcept;
174  std::istream& load_option_key_value(std::istream& iss);
175  size_t register_callback(GncOptionDBChangeCallback, void*);
176  void unregister_callback(size_t);
177  void run_callbacks();
178 private:
179  GncOptionSection* m_default_section;
180  std::vector<GncOptionSectionPtr> m_sections;
181  bool m_dirty = false;
182  GncCallbackVec m_callbacks{};
183 
184  std::function<GncOptionUIItem*()> m_get_ui_value;
185  std::function<void(GncOptionUIItem*)> m_set_ui_value;
186 };
187 
188 
189 #endif // GNC_OPTIONDB_P_HPP_
190 
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:130
C++ Public interface for individual options.
class GncOptionSection The upper-level classification implmentation.
Tax Table programming interface.
Implementation templates and specializtions for GncOption values.
Business Invoice Interface.
Job Interface.
Employee Interface.
Vendor Interface.