neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_item_model.hpp
Go to the documentation of this file.
1// i_item_model.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2015, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (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, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
23#include <boost/format.hpp>
25#include <neolib/core/pair.hpp>
31
32namespace neogfx
33{
34 class i_item_model;
35
36 struct item_model_index : item_index<item_model_index> { using item_index::item_index; };
37 typedef std::optional<item_model_index> optional_item_model_index;
38
39 enum class item_data_type : uint32_t
40 {
41 Unknown,
42 Bool,
43 Int32,
44 UInt32,
45 Int64,
46 UInt64,
47 Float,
48 Double,
49 String,
50 Pointer,
62 };
63
64 enum class item_cell_data_category : uint32_t
65 {
66 Invalid,
67 Value,
68 Pointer,
73 };
74}
75
99
109
110namespace neogfx
111{
112 template <typename T>
113 struct item_cell_choice_type
114 {
115 typedef T value_type;
117 typedef neolib::vector<option> type;
118 };
119
120 typedef neolib::variant<
121 bool,
122 int32_t,
123 uint32_t,
124 int64_t,
125 uint64_t,
126 float,
127 double,
128 string,
129 void*,
131 item_cell_choice_type<bool>::type::value_type const*,
132 item_cell_choice_type<int32_t>::type::value_type const*,
133 item_cell_choice_type<uint32_t>::type::value_type const*,
134 item_cell_choice_type<int64_t>::type::value_type const*,
135 item_cell_choice_type<uint64_t>::type::value_type const*,
136 item_cell_choice_type<float>::type::value_type const*,
137 item_cell_choice_type<double>::type::value_type const*,
138 item_cell_choice_type<string>::type::value_type const*,
139 item_cell_choice_type<void*>::type::value_type const*,
140 item_cell_choice_type<custom_type>::type::value_type const*> item_cell_data_variant;
141
142 template <typename T> struct classify_item_call_data { static constexpr item_cell_data_category category = item_cell_data_category::Invalid; };
143 template <> struct classify_item_call_data<bool> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
144 template <> struct classify_item_call_data<int32_t> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
145 template <> struct classify_item_call_data<uint32_t> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
146 template <> struct classify_item_call_data<int64_t> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
147 template <> struct classify_item_call_data<uint64_t> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
148 template <> struct classify_item_call_data<float> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
149 template <> struct classify_item_call_data<double> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
150 template <> struct classify_item_call_data<string> { static constexpr item_cell_data_category category = item_cell_data_category::Value; };
151 template <> struct classify_item_call_data<void*> { static constexpr item_cell_data_category category = item_cell_data_category::Pointer; };
152 template <> struct classify_item_call_data<custom_type> { static constexpr item_cell_data_category category = item_cell_data_category::CustomType; };
153 template <> struct classify_item_call_data<item_cell_choice_type<bool>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
154 template <> struct classify_item_call_data<item_cell_choice_type<int32_t>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
155 template <> struct classify_item_call_data<item_cell_choice_type<uint32_t>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
156 template <> struct classify_item_call_data<item_cell_choice_type<int64_t>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
157 template <> struct classify_item_call_data<item_cell_choice_type<uint64_t>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
158 template <> struct classify_item_call_data<item_cell_choice_type<float>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
159 template <> struct classify_item_call_data<item_cell_choice_type<double>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
160 template <> struct classify_item_call_data<item_cell_choice_type<string>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseValue; };
161 template <> struct classify_item_call_data<item_cell_choice_type<void*>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChoosePointer; };
162 template <> struct classify_item_call_data<item_cell_choice_type<custom_type>::type::value_type const*> { static constexpr item_cell_data_category category = item_cell_data_category::ChooseCustomType; };
163
164 class item_cell_data : public item_cell_data_variant
165 {
166 public:
167 item_cell_data()
168 {
169 }
170 template <typename T, typename std::enable_if_t<is_alternative_v<T>, int> = 0>
171 item_cell_data(T const& aValue) :
172 item_cell_data_variant{ aValue }
173 {
174 }
175 template <typename T, typename std::enable_if_t<is_alternative_v<T>, int> = 0>
176 item_cell_data(T&& aValue) :
177 item_cell_data_variant{ std::forward<T>(aValue) }
178 {
179 }
180 item_cell_data(item_cell_data const& aOther) :
181 item_cell_data_variant{ static_cast<item_cell_data_variant const&>(aOther) }
182 {
183 }
184 item_cell_data(item_cell_data&& aOther) :
185 item_cell_data_variant{ static_cast<item_cell_data_variant&&>(std::move(aOther)) }
186 {
187 }
188 public:
189 item_cell_data(char const* aString) :
190 item_cell_data_variant{ string{ aString } }
191 {
192 }
193 item_cell_data(std::string const& aString) :
194 item_cell_data_variant{ string{ aString } }
195 {
196 }
197 item_cell_data(std::string&& aString) :
198 item_cell_data_variant{ string{ std::move(aString) } }
199 {
200 }
201 public:
202 item_cell_data& operator=(item_cell_data const& aOther)
203 {
204 item_cell_data_variant::operator=(static_cast<item_cell_data_variant const&>(aOther));
205 return *this;
206 }
207 item_cell_data& operator=(item_cell_data&& aOther)
208 {
209 item_cell_data_variant::operator=(static_cast<item_cell_data_variant&&>(std::move(aOther)));
210 return *this;
211 }
212 template <typename T, typename std::enable_if_t<is_alternative_v<T>, int> = 0>
213 item_cell_data& operator=(T const& aArgument)
214 {
215 item_cell_data_variant::operator=(aArgument);
216 return *this;
217 }
218 template <typename T, typename std::enable_if_t<is_alternative_v<T>, int> = 0>
219 item_cell_data& operator=(T&& aArgument)
220 {
221 item_cell_data_variant::operator=(std::forward<T>(aArgument));
222 return *this;
223 }
224 public:
225 item_cell_data& operator=(char const* aString)
226 {
227 item_cell_data_variant::operator=(string{ aString });
228 return *this;
229 }
230 item_cell_data& operator=(std::string const& aString)
231 {
232 item_cell_data_variant::operator=(string{ aString });
233 return *this;
234 }
235 item_cell_data& operator=(std::string&& aString)
236 {
237 item_cell_data_variant::operator=(string{ aString });
238 return *this;
239 }
240 public:
241 std::string to_string() const
242 {
243 return std::visit([](auto&& arg) -> std::string
244 {
245 typedef typename std::remove_cv<typename std::remove_reference<decltype(arg)>::type>::type type;
246 if constexpr(!std::is_same_v<type, std::monostate> && classify_item_call_data<type>::category == item_cell_data_category::Value)
247 return (boost::basic_format<char>{"%1%"} % arg).str();
248 else
249 return "";
250 }, *this);
251 }
252 };
253
254 struct item_cell_info
255 {
256 item_data_type dataType;
257 item_cell_data dataMin;
258 item_cell_data dataMax;
259 item_cell_data dataStep;
260 };
261
262 typedef std::optional<item_cell_info> optional_item_cell_info;
263
264 class i_item_model : public i_reference_counted, public i_property_owner
265 {
266 public:
267 declare_event(column_info_changed, item_model_index::column_type)
268 declare_event(item_added, item_model_index const&)
269 declare_event(item_changed, item_model_index const&)
270 declare_event(item_removing, item_model_index const&)
271 declare_event(item_removed, item_model_index const&)
272 declare_event(cleared)
273 public:
274 typedef i_item_model abstract_type;
275 typedef neolib::generic_iterator iterator;
276 typedef neolib::generic_iterator const_iterator;
277 public:
278 struct wrong_model_type : std::logic_error { wrong_model_type() : std::logic_error("neogfx::i_item_model::wrong_model_type") {} };
279 struct bad_column_index : std::logic_error { bad_column_index() : std::logic_error("neogfx::i_item_model::bad_column_index") {} };
280 struct item_not_found : std::logic_error { item_not_found() : std::logic_error("neogfx::i_item_model::item_not_found") {} };
281 public:
282 virtual ~i_item_model() = default;
283 public:
284 virtual bool is_tree() const = 0;
285 virtual uint32_t rows() const = 0;
286 virtual uint32_t columns() const = 0;
287 virtual uint32_t columns(item_model_index const& aIndex) const = 0;
288 virtual std::string const& column_name(item_model_index::column_type aColumnIndex) const = 0;
289 virtual void set_column_name(item_model_index::column_type aColumnIndex, std::string const& aName) = 0;
290 virtual item_data_type column_data_type(item_model_index::column_type aColumnIndex) const = 0;
291 virtual void set_column_data_type(item_model_index::column_type aColumnIndex, item_data_type aType) = 0;
292 virtual item_cell_data const& column_min_value(item_model_index::column_type aColumnIndex) const = 0;
293 virtual void set_column_min_value(item_model_index::column_type aColumnIndex, item_cell_data const& aValue) = 0;
294 virtual item_cell_data const& column_max_value(item_model_index::column_type aColumnIndex) const = 0;
295 virtual void set_column_max_value(item_model_index::column_type aColumnIndex, item_cell_data const& aValue) = 0;
296 virtual item_cell_data const& column_step_value(item_model_index::column_type aColumnIndex) const = 0;
297 virtual void set_column_step_value(item_model_index::column_type aColumnIndex, item_cell_data const& aValue) = 0;
298 public:
299 virtual iterator index_to_iterator(item_model_index const& aIndex) = 0;
300 virtual const_iterator index_to_iterator(item_model_index const& aIndex) const = 0;
301 virtual item_model_index iterator_to_index(const_iterator aPosition) const = 0;
302 virtual iterator begin() = 0;
303 virtual const_iterator begin() const = 0;
304 virtual iterator end() = 0;
305 virtual const_iterator end() const = 0;
306 virtual iterator sbegin() = 0;
307 virtual const_iterator sbegin() const = 0;
308 virtual iterator send() = 0;
309 virtual const_iterator send() const = 0;
310 virtual bool has_children(const_iterator aParent) const = 0;
311 virtual bool has_children(item_model_index const& aParentIndex) const = 0;
312 virtual bool has_parent(const_iterator aChild) const = 0;
313 virtual bool has_parent(item_model_index const& aChildIndex) const = 0;
314 virtual iterator parent(const_iterator aChild) = 0;
315 virtual const_iterator parent(const_iterator aChild) const = 0;
316 virtual item_model_index parent(item_model_index const& aChildIndex) const = 0;
317 virtual iterator sbegin(const_iterator aParent) = 0;
318 virtual const_iterator sbegin(const_iterator aParent) const = 0;
319 virtual iterator send(const_iterator aParent) = 0;
320 virtual const_iterator send(const_iterator aParent) const = 0;
321 public:
322 virtual bool empty() const = 0;
323 virtual void reserve(uint32_t aItemCount) = 0;
324 virtual uint32_t capacity() const = 0;
325 virtual iterator insert_item(const_iterator aPosition, item_cell_data const& aCellData) = 0;
326 virtual iterator insert_item(item_model_index const& aIndex, item_cell_data const& aCellData) = 0;
327 virtual iterator append_item(const_iterator aParent, item_cell_data const& aCellData) = 0;
328 virtual iterator append_item(item_model_index const& aIndex, item_cell_data const& aCellData) = 0;
329 virtual void clear() = 0;
330 virtual iterator erase(const_iterator aPosition) = 0;
331 virtual iterator erase(item_model_index const& aIndex) = 0;
332 virtual void insert_cell_data(const_iterator aPosition, item_model_index::column_type aColumnIndex, item_cell_data const& aCellData) = 0;
333 virtual void insert_cell_data(item_model_index const& aIndex, item_cell_data const& aCellData) = 0;
334 virtual void update_cell_data(const_iterator aPosition, item_model_index::column_type aColumnIndex, item_cell_data const& aCellData) = 0;
335 virtual void update_cell_data(item_model_index const& aIndex, item_cell_data const& aCellData) = 0;
336 public:
337 virtual const item_cell_info& cell_info(item_model_index const& aIndex) const = 0;
338 virtual item_cell_data const& cell_data(item_model_index const& aIndex) const = 0;
339 };
340}
#define end_declare_enum(enumName)
Definition i_enum.hpp:62
#define declare_enum_string(enumName, enumEnumerator)
Definition i_enum.hpp:59
#define begin_declare_enum(enumName)
Definition i_enum.hpp:52
neolib::any custom_type
constexpr object_type category(object_type aType)
std::string to_string(note const &aNote)
item_cell_data_category
std::optional< item_model_index > optional_item_model_index
Definition plf_hive.h:79
plf::hive< element_type, allocator_type >::size_type erase(plf::hive< element_type, allocator_type > &container, const element_type &value)
Definition plf_hive.h:4826
constexpr decltype(auto) visit(Visitor &&vis, neolib::variant< Types... > &&var)
Definition variant.hpp:60
#define declare_event(declName,...)
Definition i_event.hpp:305