ngscopeclient 0.1-dev+51fbda87c
PreferenceTree.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* glscopeclient *
4* *
5* Copyright (c) 2012-2020 Andrew D. Zonenberg *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef PreferenceTree_h
37#define PreferenceTree_h
38
39#include <string>
40#include <vector>
41
42#include "Preference.h"
43#include "yaml-cpp/yaml.h"
44
45enum class PreferenceTreeNodeType
46{
48 Category
49};
50
52
53namespace internal
54{
56 {
57 public:
58 explicit PreferencePath(const std::string& path);
59
60 protected:
61 PreferencePath(std::vector<std::string> segments);
62
63 public:
64 PreferencePath NextLevel() const;
65 std::size_t GetLength() const;
66 const std::string& GetCurrentSegment() const;
67
68 protected:
69 std::vector<std::string> m_segments;
70 };
71
73 {
74 public:
75 PreferenceTreeNodeBase(PreferenceTreeNodeType type, std::string identifier)
76 : m_identifier{ std::move(identifier) }, m_type{ type }
77 {
78 }
79
81 {
82 }
83
84 public:
85 // Disallow copy
88
89 PreferenceTreeNodeBase& operator=(const PreferenceTreeNodeBase&) = delete;
90 PreferenceTreeNodeBase& operator=(PreferenceTreeNodeBase&&) = default;
91
92 public:
93 virtual void ToYAML(YAML::Node& node) const = 0;
94 virtual void FromYAML(const YAML::Node& node) = 0;
95 virtual Preference& GetLeaf(const PreferencePath& path) = 0;
96 virtual bool IsVisible() const = 0;
97
98 public:
99 const std::string& GetIdentifier() const;
100 PreferenceTreeNodeType GetType() const;
101 bool IsCategory() const;
102 bool IsPreference() const;
103 PreferenceCategory& AsCategory();
104 Preference& AsPreference();
105
106
107 protected:
108 std::string m_identifier; //< The identifier of this node
109 PreferenceTreeNodeType m_type; //< What particular type this node has
110 };
111
114 {
115 public:
117
118 public:
119 virtual void ToYAML(YAML::Node& node) const;
120 virtual void FromYAML(const YAML::Node& node);
121 virtual Preference& GetLeaf(const PreferencePath& path);
122 Preference& Get();
123 const Preference& Get() const;
124 virtual bool IsVisible() const;
125
126 protected:
127 Preference m_pref;
128 };
129}
130
133{
134protected:
135 using map_type = std::map<std::string, std::unique_ptr<internal::PreferenceTreeNodeBase>>;
136 using seq_type = std::vector<std::string>;
137
138public:
139 PreferenceCategory(std::string identifier);
140
141public:
142 Preference& GetLeaf(const std::string& path);
143 const Preference& GetLeaf(const std::string& path) const;
144 virtual void ToYAML(YAML::Node& node) const;
145 virtual void FromYAML(const YAML::Node& node);
146 virtual Preference& GetLeaf(const internal::PreferencePath& path);
147 void AddPreference(Preference pref);
148 void AddPreference(impl::PreferenceBuilder&& pref);
149 PreferenceCategory& AddCategory(std::string identifier);
150 map_type& GetChildren();
151 const seq_type& GetOrdering() const;
152 virtual bool IsVisible() const;
153
154protected:
155 map_type m_children;
156 seq_type m_ordering;
157};
158
159#endif // PreferenceTree_h
Basic preference class and auxilliary types.
Definition: PreferenceTree.h:133
Definition: Preference.h:120
Definition: Preference.h:265
Definition: PreferenceTree.h:114
Definition: PreferenceTree.h:56
Definition: PreferenceTree.h:73