OnixS C++ CME MDP Premium Market Data Handler  5.8.3
API Documentation
SettingGroup.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
23 #include <vector>
24 
25 #include <OnixS/CME/MDH/String.h>
26 
28 
29 ONIXS_CMEMDH_LTWT_CLASS_DECL(SettingGroup);
30 
31 /// Represents a service controlling change/update
32 /// operations for the collections of settings.
34 {
35  /// Begins the setting change operation.
36  ///
37  /// May throw an exception if change is not possible
38  /// for the moment or is prohibited for a setting or
39  /// a group to which the setting being updated belongs.
40  virtual void beginChange(const SettingGroup&, const Char*) = 0;
41 
42  /// Accomplishes the setting change operation.
43  ///
44  /// The given member must be invoked after
45  /// successful invocation of the 'beginChange'
46  /// member and before any other setting change.
47  virtual void endChange() = 0;
48 };
49 
50 // Default attributes and functionality.
52 {
53  /// Implements base class interface.
55 
56  /// Implements base class interface.
58 
59  /// Returns instance of the
60  /// trivial assignment controller.
62  static SettingChangeController& self();
63 };
64 
65 /// Scope guard to update setting in the safe way.
67 {
68 public:
69  /// Initializes the guard for the given setting update.
70  SettingChangeGuard(SettingChangeController& controller, const SettingGroup& group, const Char* details)
71  : controller_(controller)
72  {
73  controller_.beginChange(group, details);
74  }
75 
76  /// Accomplishes setting update.
78  {
79  controller_.endChange();
80  }
81 
82 private:
83  SettingChangeController& controller_;
84 
86 
87  SettingChangeGuard& operator=(const SettingChangeGuard&);
88 };
89 
90 /// Base services for settings grouped by a certain criteria.
92 {
93 public:
94  /// Initializes the group of settings
95  /// with the given validation services.
97  : controller_(controller ? *controller : TrivialChangeController::self())
98  {
99  }
100 
101  /// Does actually nothing.
103 
104  /// Guarded assignment of the given
105  /// value to the given variable.
106  template <class Assignee, class Value>
107  void controlAssignment(const Char* description, Assignee& assignee, Value value) const
108  {
109  const SettingChangeGuard guard(controller_, *this, description);
110  {
111  assignee = value;
112  }
113  }
114 
115  /// Guarded invoke of the given routine which assumes
116  /// complex change or update for the given object.
117  template <class Changeable>
118  void controlChange(const Char* description, void (Changeable::*change)(), Changeable& changeable) const
119  {
120  const SettingChangeGuard guard(controller_, *this, description);
121  {
122  (changeable.*change)();
123  }
124  }
125 
126  /// Guarded invoke of the given routine which assumes
127  /// complex change or update for the given object.
128  template <class Change, class Changeable, class Arg>
129  void controlChange(const Char* description, Change change, Changeable& changeable, const Arg& arg) const
130  {
131  const SettingChangeGuard guard(controller_, *this, description);
132  {
133  (changeable.*change)(arg);
134  }
135  }
136 
137  /// Guarded invoke of the given routine which assumes
138  /// complex change or update for the given object.
139  template <class Change, class Changeable, class Arg1, class Arg2>
140  void
141  controlChange(const Char* description, Change change, Changeable& changeable, const Arg1& arg1, const Arg2& arg2)
142  const
143  {
144  const SettingChangeGuard guard(controller_, *this, description);
145  {
146  (changeable.*change)(arg1, arg2);
147  }
148  }
149 
150  /// Returns an instance
151  /// representing a null group.
153  static const SettingGroup& null();
154 
155 private:
156  // Controller for the given group of settings.
157  SettingChangeController& controller_;
158 
159  SettingGroup(const SettingGroup&);
160 
161  SettingGroup& operator=(const SettingGroup&);
162 };
163 
164 /// Checks the two groups for equality.
165 inline bool operator==(const SettingGroup& left, const SettingGroup& right)
166 {
167  return (&left == &right);
168 }
169 
170 /// Checks the two groups for inequality.
171 inline bool operator!=(const SettingGroup& left, const SettingGroup& right)
172 {
173  return (&left != &right);
174 }
175 
176 // Pre-defined collections.
177 
178 /// Represents a setting which is a list of values.
179 template <class Type, class Container = std::vector<Type> >
181 {
182 public:
183  /// Alias for the type representing the list item.
184  typedef Type Item;
185 
186  /// Internal container used to store items.
187  typedef Container Items;
188 
189  /// Type in which items are counted.
190  typedef typename Items::size_type Size;
191 
192  /// Constant iterator over the items.
193  typedef typename Items::const_iterator Iterator;
194 
195  /// Initializes the instance belonging
196  /// to the given group of settings.
198  : group_(group ? *group : SettingGroup::null())
199  {
200  }
201 
202  /// Initializes as a copy the other instance.
203  ListSetting(const ListSetting& other)
204  : group_(SettingGroup::null())
205  , items_(other.items_)
206  {
207  }
208 
209  /// Initializes as copy of the given range of items.
210  template <class InputIterator>
211  ListSetting(InputIterator first, InputIterator last)
212  : group_(SettingGroup::null())
213  , items_(first, last)
214  {
215  }
216 
217  /// Implicit conversion into the container keeping items.
218  operator const Items&() const
219  {
220  return items_;
221  }
222 
223  /// Indicates whether the list is an empty one.
224  bool empty() const
225  {
226  return items_.empty();
227  }
228 
229  /// Returns number of items in the given list.
230  Size size() const
231  {
232  return items_.size();
233  }
234 
235  /// Returns iterator pointing to the beginning of the list.
236  Iterator begin() const
237  {
238  return items_.begin();
239  }
240 
241  /// Returns iterator pointing to the item beyond the last one.
242  Iterator end() const
243  {
244  return items_.end();
245  }
246 
247  /// Provides access to the items by its index.
248  const Item& operator[](Size index) const
249  {
250  return items_[index];
251  }
252 
253  /// Performs managed initialization of the
254  /// list using the given functional object.
255  template <class Initializer>
256  void initialize(Initializer initializer)
257  {
258  group_.controlChange("Initialize List", &ListSetting::initNoControl<Initializer>, *this, initializer);
259  }
260 
261  /// Appends the given item to the tail of the list.
262  void push_back(const Item& item)
263  {
264  group_.controlChange(
265  "Append an Item", static_cast<void (Items::*)(const Item&)>(&Items::push_back), items_, item
266  );
267  }
268 
269  /// Removes all items from the list.
270  ///
271  /// Iterators and references related
272  /// to the given list are invalidated.
273  void clear()
274  {
275  group_.controlChange("Clear the List", &Items::clear, items_);
276  }
277 
278  /// Re-initialize the list as a copy of the other one.
280  {
281  group_.controlChange("Assign List", &ListSetting::assignNoControl, *this, other);
282 
283  return *this;
284  }
285 
286  /// Re-initialize the list as a copy of the given range.
287  template <class InputIterator>
288  ListSetting& assign(InputIterator first, InputIterator last)
289  {
290  group_.controlChange("Assign List", &Items::assign, items_, first, last);
291 
292  return *this;
293  }
294 
295  /// Unmanaged assignment of the list.
296  ///
297  /// The given member must be used when the given
298  /// instance is part of the other managed collection.
299  /// It's not supposed to be invoked when the instance
300  /// is manipulated in ordinary way.
302  {
303  items_ = other.items_;
304 
305  return *this;
306  }
307 
308 private:
309  const SettingGroup& group_;
310 
311  Container items_;
312 
313  template <class Initializer>
314  void initNoControl(Initializer initializer)
315  {
316  items_.clear();
317 
318  initializer(items_);
319  }
320 };
321 
322 /// Serializes the given list setting
323 /// into a string/text presentation.
324 template <class Type>
325 void toStr(std::string& str, const ListSetting<Type>& list, Char delimiter = ',')
326 {
327  const typename ListSetting<Type>::Iterator endOfList = list.end();
328 
329  typename ListSetting<Type>::Iterator item = list.begin();
330 
331  while (true)
332  {
333  toStr(str, *item);
334 
335  if (++item == endOfList)
336  {
337  break;
338  }
339 
340  str += delimiter;
341  }
342 }
343 
Represents a setting which is a list of values.
Definition: SettingGroup.h:180
#define ONIXS_CMEMDH_OVERRIDE
Definition: Compiler.h:143
ListSetting & assign(InputIterator first, InputIterator last)
Re-initialize the list as a copy of the given range.
Definition: SettingGroup.h:288
ListSetting & operator=(const ListSetting &other)
Re-initialize the list as a copy of the other one.
Definition: SettingGroup.h:279
void clear()
Removes all items from the list.
Definition: SettingGroup.h:273
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:145
void initialize(Initializer initializer)
Performs managed initialization of the list using the given functional object.
Definition: SettingGroup.h:256
bool operator!=(const SettingGroup &left, const SettingGroup &right)
Checks the two groups for inequality.
Definition: SettingGroup.h:171
~SettingChangeGuard()
Accomplishes setting update.
Definition: SettingGroup.h:77
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
Container Items
Internal container used to store items.
Definition: SettingGroup.h:187
void controlChange(const Char *description, Change change, Changeable &changeable, const Arg1 &arg1, const Arg2 &arg2) const
Guarded invoke of the given routine which assumes complex change or update for the given object...
Definition: SettingGroup.h:141
Scope guard to update setting in the safe way.
Definition: SettingGroup.h:66
void controlAssignment(const Char *description, Assignee &assignee, Value value) const
Guarded assignment of the given value to the given variable.
Definition: SettingGroup.h:107
ListSetting(const ListSetting &other)
Initializes as a copy the other instance.
Definition: SettingGroup.h:203
void toStr(std::string &str, const ListSetting< Type > &list, Char delimiter= ',')
Serializes the given list setting into a string/text presentation.
Definition: SettingGroup.h:325
char Char
Character type alias.
Definition: String.h:36
bool value(Number &number, const MultiContainer &container, Tag tag)
Finds a tag-value entry in the given collection by the given tag and returns its value component tran...
SettingChangeGuard(SettingChangeController &controller, const SettingGroup &group, const Char *details)
Initializes the guard for the given setting update.
Definition: SettingGroup.h:70
Iterator begin() const
Returns iterator pointing to the beginning of the list.
Definition: SettingGroup.h:236
const Item & operator[](Size index) const
Provides access to the items by its index.
Definition: SettingGroup.h:248
void push_back(const Item &item)
Appends the given item to the tail of the list.
Definition: SettingGroup.h:262
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
ListSetting(InputIterator first, InputIterator last)
Initializes as copy of the given range of items.
Definition: SettingGroup.h:211
void endChange() override
Implements base class interface.
Definition: SettingGroup.h:57
Base services for settings grouped by a certain criteria.
Definition: SettingGroup.h:91
Items::size_type Size
Type in which items are counted.
Definition: SettingGroup.h:190
bool operator==(const SettingGroup &left, const SettingGroup &right)
Checks the two groups for equality.
Definition: SettingGroup.h:165
ListSetting(const SettingGroup *group=nullptr)
Initializes the instance belonging to the given group of settings.
Definition: SettingGroup.h:197
Items::const_iterator Iterator
Constant iterator over the items.
Definition: SettingGroup.h:193
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition: Bootstrap.h:48
SettingGroup(SettingChangeController *controller=nullptr)
Initializes the group of settings with the given validation services.
Definition: SettingGroup.h:96
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition: Bootstrap.h:47
ListSetting & assignNoControl(const ListSetting &other)
Unmanaged assignment of the list.
Definition: SettingGroup.h:301
Size size() const
Returns number of items in the given list.
Definition: SettingGroup.h:230
Represents a service controlling change/update operations for the collections of settings.
Definition: SettingGroup.h:33
void beginChange(const SettingGroup &, const Char *) override
Implements base class interface.
Definition: SettingGroup.h:54
Iterator end() const
Returns iterator pointing to the item beyond the last one.
Definition: SettingGroup.h:242
bool empty() const
Indicates whether the list is an empty one.
Definition: SettingGroup.h:224
Type Item
Alias for the type representing the list item.
Definition: SettingGroup.h:184
void controlChange(const Char *description, Change change, Changeable &changeable, const Arg &arg) const
Guarded invoke of the given routine which assumes complex change or update for the given object...
Definition: SettingGroup.h:129
~SettingGroup()
Does actually nothing.
Definition: SettingGroup.h:102
void controlChange(const Char *description, void(Changeable::*change)(), Changeable &changeable) const
Guarded invoke of the given routine which assumes complex change or update for the given object...
Definition: SettingGroup.h:118
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68