OnixS C++ CME Market Data Handler  5.5.1
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 
30 (
31  SettingGroup
32 );
33 
34 /// Represents a service controlling change/update
35 /// operations for the collections of settings.
38 {
39  /// Begins the setting change operation.
40  ///
41  /// May throw an exception if change is not possible
42  /// for the moment or is prohibited for a setting or
43  /// a group to which the setting being updated belongs.
44  virtual
45  void
46  beginChange(
47  const SettingGroup&,
48  const Char*) = 0;
49 
50  /// Accomplishes the setting change operation.
51  ///
52  /// The given member must be invoked after
53  /// successful invocation of the 'beginChange'
54  /// member and before any other setting change.
55  virtual
56  void
57  endChange() = 0;
58 };
59 
60 // Default attributes and functionality.
64 {
65  /// Implements base class interface.
66  void
68  const SettingGroup&,
69  const Char*)
70  {
71  }
72 
73  /// Implements base class interface.
74  void
76  {
77  }
78 
79  /// Returns instance of the
80  /// trivial assignment controller.
81  static
83  self()
84  {
85  static
87  trivial;
88 
89  return trivial;
90  }
91 };
92 
93 /// Scope guard to update setting in the safe way.
95 {
96  SettingChangeController& controller_;
97 
99  const SettingChangeGuard&);
100 
102  operator =(
103  const SettingChangeGuard&);
104 
105 public:
106  /// Initializes the guard for the given setting update.
108  SettingChangeController& controller,
109  const SettingGroup& group,
110  const Char* details)
111  : controller_(controller)
112  {
113  controller_.beginChange(group, details);
114  }
115 
116  /// Accomplishes setting update.
118  {
119  controller_.endChange();
120  }
121 };
122 
123 /// Base services for settings grouped by a certain criteria.
125 {
126  // Controller for the given group of settings.
127  SettingChangeController& controller_;
128 
129  SettingGroup(
130  const SettingGroup&);
131 
132  SettingGroup&
133  operator =(
134  const SettingGroup&);
135 
136 public:
137  /// Initializes the group of settings
138  /// with the given validation services.
141  controller = NULL)
142  : controller_
143  (
144  controller
145  ? *controller
147  self()
148  )
149  {
150  }
151 
152  /// Does actually nothing.
154  {
155  }
156 
157  /// Guarded assignment of the given
158  /// value to the given variable.
159  template
160  <
161  class Assignee,
162  class Value
163  >
164  void
166  const Char* description,
167  Assignee& assignee,
168  Value value) const
169  {
170  const
172  guard(
173  controller_,
174  *this,
175  description);
176  {
177  assignee = value;
178  }
179  }
180 
181  /// Guarded invoke of the given routine which assumes
182  /// complex change or update for the given object.
183  template
184  <
185  class Changeable
186  >
187  void
189  const Char* description,
190  void (Changeable::*change)(),
191  Changeable& changeable) const
192  {
193  const
195  guard(
196  controller_,
197  *this,
198  description);
199  {
200  (changeable.*change)();
201  }
202  }
203 
204  /// Guarded invoke of the given routine which assumes
205  /// complex change or update for the given object.
206  template
207  <
208  class Change,
209  class Changeable,
210  class Arg
211  >
212  void
214  const Char* description,
215  Change change,
216  Changeable& changeable,
217  const Arg& arg) const
218  {
219  const
221  guard(
222  controller_,
223  *this,
224  description);
225  {
226  (changeable.*change)(arg);
227  }
228  }
229 
230  /// Guarded invoke of the given routine which assumes
231  /// complex change or update for the given object.
232  template
233  <
234  class Change,
235  class Changeable,
236  class Arg1,
237  class Arg2
238  >
239  void
241  const Char* description,
242  Change change,
243  Changeable& changeable,
244  const Arg1& arg1,
245  const Arg2& arg2) const
246  {
247  const
249  guard(
250  controller_,
251  *this,
252  description);
253  {
254  (changeable.*change)
255  (
256  arg1,
257  arg2
258  );
259  }
260  }
261 
262  /// Returns an instance
263  /// representing a null group.
265  static
266  const SettingGroup&
268  {
269  static
270  const
271  SettingGroup
272  nullInstance;
273 
274  return nullInstance;
275  }
276 };
277 
278 /// Checks the two groups for equality.
279 inline
280 bool
282  const SettingGroup& left,
283  const SettingGroup& right)
284 {
285  return (&left == &right);
286 }
287 
288 /// Checks the two groups for inequality.
289 inline
290 bool
292  const SettingGroup& left,
293  const SettingGroup& right)
294 {
295  return (&left != &right);
296 }
297 
298 // Pre-defined collections.
299 
300 /// Represents a setting which is a list of values.
301 template
302 <
303  class Type,
304  class Container = std::vector<Type>
305 >
307 {
308  const SettingGroup& group_;
309 
310  Container items_;
311 
312  template
313  <
314  class Initializer
315  >
316  void
317  initNoControl(
318  Initializer initializer)
319  {
320  items_.clear();
321 
322  initializer(items_);
323  }
324 
325 public:
326  /// Alias for the type representing the list item.
327  typedef Type Item;
328 
329  /// Internal container used to store items.
330  typedef Container Items;
331 
332  /// Type in which items are counted.
333  typedef typename Items::size_type Size;
334 
335  /// Constant iterator over the items.
336  typedef
337  typename
338  Items::const_iterator
340 
341  /// Initializes the instance belonging
342  /// to the given group of settings.
343  explicit
345  const SettingGroup* group = NULL)
346  : group_(
347  group
348  ? *group
349  : SettingGroup::null())
350  {
351  }
352 
353  /// Initializes as a copy the other instance.
355  const ListSetting& other)
356  : group_(SettingGroup::null())
357  , items_(other.items_)
358  {
359  }
360 
361  /// Initializes as copy of the given range of items.
362  template
363  <
364  class InputIterator
365  >
367  InputIterator first,
368  InputIterator last)
369  : group_(SettingGroup::null())
370  , items_(first, last)
371  {
372  }
373 
374  /// Implicit conversion into the container keeping items.
375  operator const Items&() const
376  {
377  return items_;
378  }
379 
380  /// Indicates whether the list is an empty one.
381  bool empty() const
382  {
383  return items_.empty();
384  }
385 
386  /// Returns number of items in the given list.
387  Size size() const
388  {
389  return items_.size();
390  }
391 
392  /// Returns iterator pointing to the beginnig of the list.
393  Iterator begin() const
394  {
395  return items_.begin();
396  }
397 
398  /// Returns iterator pointing to the item beyond the last one.
399  Iterator end() const
400  {
401  return items_.end();
402  }
403 
404  /// Provides access to the items by its index.
405  const Item&
406  operator [](
407  Size index) const
408  {
409  return items_[index];
410  }
411 
412  /// Performs managed initialization of the
413  /// list using the given functional object.
414  template
415  <
416  class Initializer
417  >
418  void
420  Initializer initializer)
421  {
422  group_.
423  controlChange
424  (
425  "Initialize List",
426  &ListSetting::initNoControl<Initializer>,
427  *this,
428  initializer
429  );
430  }
431 
432  /// Appends the given item to the tail of the list.
433  void
435  const Item& item)
436  {
437  group_.
438  controlChange
439  (
440  "Append an Item",
441  static_cast
442  <void (Items::*)(const Item&)>
443  (&Items::push_back),
444  items_,
445  item
446  );
447  }
448 
449  /// Removes all items from the list.
450  ///
451  /// Iterators and references related
452  /// to the given list are invalidated.
453  void
455  {
456  group_.
457  controlChange
458  (
459  "Clear the List",
460  &Items::clear,
461  items_
462  );
463  }
464 
465  /// Re-initialize the list as a copy of the other one.
466  ListSetting&
467  operator =(
468  const ListSetting& other)
469  {
470  group_.
471  controlChange
472  (
473  "Assign List",
474  &ListSetting::assignNoControl,
475  *this,
476  other
477  );
478 
479  return *this;
480  }
481 
482  /// Re-initialize the list as a copy of the given range.
483  template
484  <
485  class InputIterator
486  >
487  ListSetting&
489  InputIterator first,
490  InputIterator last)
491  {
492  group_.
493  controlChange
494  (
495  "Assign List",
496  &Items::assign,
497  items_,
498  first,
499  last
500  );
501 
502  return *this;
503  }
504 
505  /// Unmanaged assingment of the list.
506  ///
507  /// The given member must be used when the given
508  /// instance is part of the other managed collection.
509  /// It's not supposed to be invoked when the instance
510  /// is manipulated in ordinary way.
511  ListSetting&
513  const ListSetting& other)
514  {
515  items_ = other.items_;
516 
517  return *this;
518  }
519 };
520 
521 /// Serializes the given list setting
522 /// into a string/text presentation.
523 template
524 <
525  class Type
526 >
527 void
529  std::string& str,
530  const ListSetting<Type>& list,
531  Char delimiter = ',')
532 {
533  const
534  typename
536  endOfList =
537  list.end();
538 
539  typename
541  item =
542  list.begin();
543 
544  while (true)
545  {
546  toStr(str, *item);
547 
548  if (++item == endOfList)
549  {
550  break;
551  }
552 
553  str += delimiter;
554  }
555 }
556 
#define ONIXS_CMEMDH_LTWT_CLASS
Definition: Bootstrap.h:94
Represents a setting which is a list of values.
Definition: SettingGroup.h:306
ListSetting & assign(InputIterator first, InputIterator last)
Re-initialize the list as a copy of the given range.
Definition: SettingGroup.h:488
ListSetting(const SettingGroup *group=NULL)
Definition: SettingGroup.h:344
void initialize(Initializer initializer)
Definition: SettingGroup.h:419
bool operator!=(const SettingGroup &left, const SettingGroup &right)
Checks the two groups for inequality.
Definition: SettingGroup.h:291
~SettingChangeGuard()
Accomplishes setting update.
Definition: SettingGroup.h:117
void beginChange(const SettingGroup &, const Char *)
Implements base class interface.
Definition: SettingGroup.h:67
Container Items
Internal container used to store items.
Definition: SettingGroup.h:330
void controlChange(const Char *description, Change change, Changeable &changeable, const Arg1 &arg1, const Arg2 &arg2) const
Definition: SettingGroup.h:240
virtual void beginChange(const SettingGroup &, const Char *)=0
Scope guard to update setting in the safe way.
Definition: SettingGroup.h:94
void controlAssignment(const Char *description, Assignee &assignee, Value value) const
Definition: SettingGroup.h:165
ListSetting(const ListSetting &other)
Initializes as a copy the other instance.
Definition: SettingGroup.h:354
void toStr(std::string &str, const ListSetting< Type > &list, Char delimiter= ',')
Definition: SettingGroup.h:528
char Char
Character type alias.
Definition: String.h:36
bool value(Number &number, const MultiContainer &container, Tag tag)
SettingChangeGuard(SettingChangeController &controller, const SettingGroup &group, const Char *details)
Initializes the guard for the given setting update.
Definition: SettingGroup.h:107
Iterator begin() const
Returns iterator pointing to the beginnig of the list.
Definition: SettingGroup.h:393
void endChange()
Implements base class interface.
Definition: SettingGroup.h:75
void push_back(const Item &item)
Appends the given item to the tail of the list.
Definition: SettingGroup.h:434
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
ListSetting(InputIterator first, InputIterator last)
Initializes as copy of the given range of items.
Definition: SettingGroup.h:366
Base services for settings grouped by a certain criteria.
Definition: SettingGroup.h:124
Items::size_type Size
Type in which items are counted.
Definition: SettingGroup.h:333
bool operator==(const SettingGroup &left, const SettingGroup &right)
Checks the two groups for equality.
Definition: SettingGroup.h:281
static ONIXS_CMEMDH_EXPORTED const SettingGroup & null()
Definition: SettingGroup.h:267
Items::const_iterator Iterator
Constant iterator over the items.
Definition: SettingGroup.h:339
#define ONIXS_CMEMDH_LTWT_CLASS_DECL(name)
Definition: Bootstrap.h:106
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition: Bootstrap.h:102
ListSetting & assignNoControl(const ListSetting &other)
Definition: SettingGroup.h:512
Size size() const
Returns number of items in the given list.
Definition: SettingGroup.h:387
Iterator end() const
Returns iterator pointing to the item beyond the last one.
Definition: SettingGroup.h:399
bool empty() const
Indicates whether the list is an empty one.
Definition: SettingGroup.h:381
SettingGroup(SettingChangeController *controller=NULL)
Definition: SettingGroup.h:139
Type Item
Alias for the type representing the list item.
Definition: SettingGroup.h:327
void controlChange(const Char *description, Change change, Changeable &changeable, const Arg &arg) const
Definition: SettingGroup.h:213
#define ONIXS_CMEMDH_EXPORTED_STRUCT
Definition: Bootstrap.h:58
~SettingGroup()
Does actually nothing.
Definition: SettingGroup.h:153
void controlChange(const Char *description, void(Changeable::*change)(), Changeable &changeable) const
Definition: SettingGroup.h:188
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156