YAMI4 C++
parameters.h
1 // Copyright Maciej Sobczak 2008-2019.
2 // This file is part of YAMI4.
3 //
4 // YAMI4 is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // YAMI4 is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with YAMI4. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef YAMICPP_PARAMETERS_H_INCLUDED
18 #define YAMICPP_PARAMETERS_H_INCLUDED
19 
20 #include "parameter_entry.h"
21 #include "parameter_type.h"
22 #include "serializable.h"
23 #include <yami4-core/dll.h>
24 #include <yami4-core/parameter_iterator.h>
25 #include <memory>
26 #include <ostream>
27 #include <string>
28 
29 namespace yami
30 {
31 
32 namespace core
33 {
34 class parameters;
35 } // namespace core
36 
71 class DLL parameters : public serializable
72 {
73 public:
77  parameters();
78 
83  explicit parameters(core::parameters * external);
84 
86  parameters(const parameters & other);
87 
89  void operator=(const parameters & other);
90 
94  void swap(parameters & other);
95 
100 
101  // single-value operations
102 
110  void set_boolean(const std::string & name, bool value);
111  void set_boolean(const char * name, bool value);
112 
118  bool get_boolean(const std::string & name) const;
119  bool get_boolean(const char * name) const;
120 
128  void set_integer(const std::string & name, int value);
129  void set_integer(const char * name, int value);
130 
136  int get_integer(const std::string & name) const;
137  int get_integer(const char * name) const;
138 
146  void set_long_long(const std::string & name, long long value);
147  void set_long_long(const char * name, long long value);
148 
154  long long get_long_long(const std::string & name) const;
155  long long get_long_long(const char * name) const;
156 
164  void set_double_float(const std::string & name, double value);
165  void set_double_float(const char * name, double value);
166 
172  double get_double_float(const std::string & name) const;
173  double get_double_float(const char * name) const;
174 
182  void set_string(const std::string & name, const std::string & value);
183  void set_string(const char * name, const char * value);
184 
198  void set_string_shallow(const std::string & name,
199  const char * value, std::size_t value_length);
200  void set_string_shallow(const char * name, std::size_t name_length,
201  const char * value, std::size_t value_length);
202 
208  std::string get_string(const std::string & name) const;
209  std::string get_string(const char * name) const;
210 
218  const char * get_string(const std::string & name,
219  std::size_t & length) const;
220  const char * get_string(const char * name,
221  std::size_t & length) const;
222 
231  void set_binary(const std::string & name,
232  const void * value, std::size_t value_length);
233  void set_binary(const char * name,
234  const void * value, std::size_t value_length);
235 
249  void set_binary_shallow(const std::string & name,
250  const void * value, std::size_t value_length);
251  void set_binary_shallow(const char * name, std::size_t name_length,
252  const void * value, std::size_t value_length);
253 
261  const void * get_binary(const std::string & name,
262  std::size_t & length) const;
263  const void * get_binary(const char * name,
264  std::size_t & length) const;
265 
266  // array operations
267 
279  void set_boolean_array(const std::string & name,
280  const bool * values, std::size_t array_length);
281  void set_boolean_array(const char * name,
282  const bool * values, std::size_t array_length);
283 
296  void set_boolean_array_shallow(const std::string & name,
297  const bool * values, std::size_t array_length);
298  void set_boolean_array_shallow(const char * name,
299  const bool * values, std::size_t array_length);
300 
310  bool * get_boolean_array(const std::string & name,
311  std::size_t & array_length) const;
312  bool * get_boolean_array(const char * name,
313  std::size_t & array_length) const;
314 
326  void set_integer_array(const std::string & name,
327  const int * values, std::size_t array_length);
328  void set_integer_array(const char * name,
329  const int * values, std::size_t array_length);
330 
343  void set_integer_array_shallow(const std::string & name,
344  const int * values, std::size_t array_length);
345  void set_integer_array_shallow(const char * name,
346  const int * values, std::size_t array_length);
347 
357  int * get_integer_array(const std::string & name,
358  std::size_t & array_length) const;
359  int * get_integer_array(const char * name,
360  std::size_t & array_length) const;
361 
374  void set_long_long_array(const std::string & name,
375  const long long * values, std::size_t array_length);
376  void set_long_long_array(const char * name,
377  const long long * values, std::size_t array_length);
378 
392  void set_long_long_array_shallow(const std::string & name,
393  const long long * values, std::size_t array_length);
394  void set_long_long_array_shallow(const char * name,
395  const long long * values, std::size_t array_length);
396 
407  long long * get_long_long_array(const std::string & name,
408  std::size_t & array_length) const;
409  long long * get_long_long_array(const char * name,
410  std::size_t & array_length) const;
411 
423  void set_double_float_array(const std::string & name,
424  const double * values, std::size_t array_length);
425  void set_double_float_array(const char * name,
426  const double * values, std::size_t array_length);
427 
440  void set_double_float_array_shallow(const std::string & name,
441  const double * values, std::size_t array_length);
442  void set_double_float_array_shallow(const char * name,
443  const double * values, std::size_t array_length);
444 
455  double * get_double_float_array(const std::string & name,
456  std::size_t & array_length) const;
457  double * get_double_float_array(const char * name,
458  std::size_t & array_length) const;
459 
470  void create_string_array(const std::string & name,
471  std::size_t array_length);
472  void create_string_array(const char * name, std::size_t array_length);
473 
483  void set_string_in_array(const std::string & name, std::size_t index,
484  const std::string & value);
485  void set_string_in_array(const char * name, std::size_t index,
486  const char * value);
487 
495  std::size_t get_string_array_length(const std::string & name) const;
496  std::size_t get_string_array_length(const char * name) const;
497 
504  std::string get_string_in_array(const std::string & name,
505  std::size_t index) const;
506  std::string get_string_in_array(const char * name,
507  std::size_t index) const;
508 
517  const char * get_string_in_array(const std::string & name,
518  std::size_t index, std::size_t & length) const;
519  const char * get_string_in_array(const char * name,
520  std::size_t index, std::size_t & length) const;
521 
532  void create_binary_array(const std::string & name,
533  std::size_t array_length);
534  void create_binary_array(const char * name, std::size_t array_length);
535 
546  void set_binary_in_array(const std::string & name, std::size_t index,
547  const void * value, std::size_t value_length);
548  void set_binary_in_array(const char * name, std::size_t index,
549  const void * value, std::size_t value_length);
550 
558  std::size_t get_binary_array_length(const std::string & name) const;
559  std::size_t get_binary_array_length(const char * name) const;
560 
569  const void * get_binary_in_array(const std::string & name,
570  std::size_t index, std::size_t & length) const;
571  const void * get_binary_in_array(const char * name,
572  std::size_t index, std::size_t & length) const;
573 
574  // support for data nesting
575 
585  core::parameters * create_nested_parameters(const std::string & name);
586  core::parameters * create_nested_parameters(const char * name);
587 
595  core::parameters * get_nested_parameters(const std::string & name) const;
596  core::parameters * get_nested_parameters(const char * name) const;
597 
611  void create_nested_array(const std::string & name,
612  std::size_t array_length);
613  void create_nested_array(const char * name, std::size_t array_length);
614 
622  std::size_t get_nested_array_length(const std::string & name) const;
623  std::size_t get_nested_array_length(const char * name) const;
624 
634  core::parameters * get_nested_in_array(const std::string & name,
635  std::size_t index) const;
636  core::parameters * get_nested_in_array(const char * name,
637  std::size_t index) const;
638 
639  // inspection
640 
646  std::size_t size() const;
647 
653  parameter_type type(const std::string & name) const;
654  parameter_type type(const char * name) const;
655 
665  class iterator
666  {
667  public:
668  iterator() : empty_(true), it_() {}
669  iterator(core::parameter_iterator it) : empty_(false), it_(it) {}
670 
671  bool operator==(const iterator & rhs) const
672  {
673  // this is enough for the input iterator,
674  // because the user cannot legally have two iterators
675  // in the middle of the collection and therefore the comparison
676  // can be used only to check for the end-of-sequence condition
677  return empty_ && rhs.empty_;
678  }
679 
680  bool operator!=(const iterator & rhs) const
681  {
682  return operator==(rhs) == false;
683  }
684 
685  iterator & operator++()
686  {
687  if (it_.has_next())
688  {
689  it_.move_next();
690  }
691  else
692  {
693  empty_ = true;
694  }
695 
696  return *this;
697  }
698 
699  iterator operator++(int)
700  {
701  iterator old(*this);
702  operator++();
703  return old;
704  }
705 
706  parameter_entry operator*() const
707  {
708  parameter_entry entry;
709  entry.entry_ = it_.current();
710  return entry;
711  }
712 
713  private:
714  friend class parameters;
715  bool empty_;
716  core::parameter_iterator it_;
717  };
718 
724  iterator begin() const;
725 
730  iterator end() const;
731 
740  bool find(const std::string & name, parameter_entry & entry) const;
741  bool find(const char * name, parameter_entry & entry) const;
742 
743  // cleanup
744 
752  void remove(const std::string & name);
753  void remove(const char * name);
754 
762  void remove(iterator it);
763 
773  void merge_from(const parameters & other);
774 
780  void clear();
781 
782  // serialization
783 
788  virtual std::size_t serialize_buffer_size() const;
789 
804  virtual void serialize(char * * buffers, std::size_t * buffer_sizes,
805  std::size_t num_of_buffers) const;
806 
829  void deserialize(const char * * buffers, std::size_t * buffer_sizes,
830  std::size_t num_of_buffers);
831 
833  virtual const core::parameters & get_core_object() const;
834 
835  // used from unit tests
836  void dump(std::ostream & os) const;
837 
838 private:
839 
840  // values that are owned and controlled by this object
841  std::unique_ptr<core::parameters> own_params_;
842 
843  // delegated object (not necessarily own_params_, can be external)
844  // note: it is not owned
845  core::parameters * params_;
846 };
847 
848 } // namespace yami
849 
850 #endif // YAMICPP_PARAMETERS_H_INCLUDED
~parameters()
Destructor.
Definition: parameters.h:99
Collection of message parameters.
Definition: parameters.h:71
Namespace devoted to everything related to YAMI4.
Definition: activity_statistics_monitor.cpp:27
parameter_type
Type of parameter entry.
Definition: parameter_type.h:24
Read-only view on the parameters entry.
Definition: parameter_entry.h:37
Common interface for serializable data source.
Definition: serializable.h:32
Iterator class for inspecting entries in the collection.
Definition: parameters.h:665