Apama  10.1.0.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
visitors.hpp
Go to the documentation of this file.
1 /*
2  * Title: bits/visitors.hpp
3  * Description: C++ header-only wrapper for C-ABI data_t type
4  * $Copyright (c) 2015-2016 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$
5  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
6  * @Version: $Id: visitors.hpp 294926 2016-11-09 18:33:24Z matj $
7  */
8 
15 #ifndef _DATAT_BITS_INCLUDE_TAG
16 #error Must only be included from sag_connectivity_cpp.hpp, use that instead
17 #endif
18 
19 
43 template<typename DERIVED, typename RV>
44 class visitor
45 {
46 public:
48  typedef RV result_type;
50  typedef DERIVED derived_t;
51 
53  result_type operator()() const { return derived().visitEmpty(); }
55  result_type visitEmpty() const { return derived().error("empty"); }
56 
58  result_type operator()(int64_t &i) const { return derived().visitInteger(i); }
60  result_type visitInteger(int64_t &i) const { return derived().error("integer"); }
61 
63  result_type operator()(double &i) const { return derived().visitDouble(i); }
65  result_type visitDouble(double &i) const { return derived().error("double"); }
66 
68  result_type operator()(bool &i) const { return derived().visitBoolean(i); }
70  result_type visitBoolean(bool &i) const { return derived().error("boolean"); }
71 
73  result_type operator()(const char *&i) const { return derived().visitString(i); }
75  result_type visitString(const char *&i) const { return derived().error("string"); }
76 
78  result_type operator()(decimal_t &i) const { return derived().visitDecimal(i); }
80  result_type visitDecimal(decimal_t &i) const { return derived().error("decimal"); }
81 
83  result_type operator()(buffer_t &i) const { return derived().visitBuffer(i); }
85  result_type visitBuffer(buffer_t &i) const { return derived().error("buffer"); }
86 
88  result_type operator()(list_t &i) const { return derived().visitList(i); }
90  result_type visitList(list_t &i) const { return derived().error("list"); }
91 
93  result_type operator()(map_t &i) const { return derived().visitMap(i); }
95  result_type visitMap(map_t &i) const { return derived().error("map"); }
96 
98  result_type operator()(sag_underlying_custom_t &i) const { return derived().visitCustom(i); }
100  result_type visitCustom(sag_underlying_custom_t &i) const { return derived().error("custom"); }
101 
102 protected:
105  result_type error(const std::string &reason) const { throw std::runtime_error("Visit type error: "+reason); }
106 private:
108  const derived_t &derived() const { return static_cast<const derived_t&>(*this); }
109 };
110 
135 template<typename DERIVED, typename RV>
137 {
138 public:
140  typedef RV result_type;
142  typedef DERIVED derived_t;
143 
145  result_type operator()() const { return derived().visitEmpty(); }
147  result_type visitEmpty() const { return derived().error("empty"); }
148 
150  result_type operator()(int64_t i) const { return derived().visitInteger(i); }
152  result_type visitInteger(int64_t i) const { return derived().error("integer"); }
153 
155  result_type operator()(double i) const { return derived().visitDouble(i); }
157  result_type visitDouble(double i) const { return derived().error("double"); }
158 
160  result_type operator()(bool i) const { return derived().visitBoolean(i); }
162  result_type visitBoolean(bool i) const { return derived().error("boolean"); }
163 
165  result_type operator()(const char *i) const { return derived().visitString(i); }
167  result_type visitString(const char *i) const { return derived().error("string"); }
168 
170  result_type operator()(const decimal_t &i) const { return derived().visitDecimal(i); }
172  result_type visitDecimal(const decimal_t &i) const { return derived().error("decimal"); }
173 
175  result_type operator()(const buffer_t &i) const { return derived().visitBuffer(i); }
177  result_type visitBuffer(const buffer_t &i) const { return derived().error("buffer"); }
178 
180  result_type operator()(const list_t &i) const { return derived().visitList(i); }
182  result_type visitList(const list_t &i) const { return derived().error("list"); }
183 
185  result_type operator()(const map_t &i) const { return derived().visitMap(i); }
187  result_type visitMap(const map_t &i) const { return derived().error("map"); }
188 
190  result_type operator()(const sag_underlying_custom_t &i) const { return derived().visitCustom(i); }
192  result_type visitCustom(const sag_underlying_custom_t &i) const { return derived().error("custom"); }
193 
194 protected:
197  result_type error(const std::string &reason) const { throw std::runtime_error("Visit type error: "+reason); }
198 private:
200  const derived_t &derived() const { return static_cast<const derived_t&>(*this); }
201 };
202 
result_type visitInteger(int64_t &i) const
Visit an integer.
Definition: visitors.hpp:60
result_type visitBoolean(bool i) const
Visit a boolean.
Definition: visitors.hpp:162
result_type operator()() const
Visit an empty variant (don't override this, instead override visitEmpty)
Definition: visitors.hpp:53
result_type operator()(const list_t &i) const
Visit a list (don't override this, instead override visitList)
Definition: visitors.hpp:180
A list class which implements many of the functions on std::vector.
Definition: list_t.hpp:34
A map class which implements many of the functions on std::map.
Definition: map_t.hpp:36
result_type visitCustom(sag_underlying_custom_t &i) const
Visit a custom object.
Definition: visitors.hpp:100
DERIVED derived_t
The type of the derived class.
Definition: visitors.hpp:142
result_type visitMap(const map_t &i) const
Visit a map.
Definition: visitors.hpp:187
result_type operator()(bool &i) const
Visit a boolean (don't override this, instead override visitBoolean)
Definition: visitors.hpp:68
result_type visitBuffer(const buffer_t &i) const
Visit a byte buffer.
Definition: visitors.hpp:177
result_type operator()(list_t &i) const
Visit a list (don't override this, instead override visitList)
Definition: visitors.hpp:88
sag_underlying_decimal_t decimal_t
Decimals are implemented with an underlying 64bit int conforming to IEEE 754 decimal 64...
Definition: sag_connectivity_cpp.hpp:49
Helper class for writing visitors to apply to data_t.
Definition: visitors.hpp:44
result_type error(const std::string &reason) const
Handles visiting unhandled types.
Definition: visitors.hpp:105
result_type visitDouble(double &i) const
Visit a double.
Definition: visitors.hpp:65
result_type visitCustom(const sag_underlying_custom_t &i) const
Visit a custom object.
Definition: visitors.hpp:192
result_type visitInteger(int64_t i) const
Visit an integer.
Definition: visitors.hpp:152
result_type operator()(const buffer_t &i) const
Visit a byte buffer (don't override this, instead override visitBuffer)
Definition: visitors.hpp:175
result_type visitDecimal(const decimal_t &i) const
Visit a decimal.
Definition: visitors.hpp:172
result_type visitEmpty() const
Visit an empty variant.
Definition: visitors.hpp:147
result_type operator()(buffer_t &i) const
Visit a byte buffer (don't override this, instead override visitBuffer)
Definition: visitors.hpp:83
result_type visitDouble(double i) const
Visit a double.
Definition: visitors.hpp:157
result_type visitEmpty() const
Visit an empty variant.
Definition: visitors.hpp:55
result_type operator()(const char *i) const
Visit a string (don't override this, instead override visitString)
Definition: visitors.hpp:165
result_type operator()(const char *&i) const
Visit a string (don't override this, instead override visitString)
Definition: visitors.hpp:73
result_type visitString(const char *&i) const
Visit a string.
Definition: visitors.hpp:75
A class that holds an untyped byte buffer.
Definition: buffer_t.hpp:27
result_type operator()(double &i) const
Visit a double (don't override this, instead override visitDouble)
Definition: visitors.hpp:63
result_type operator()(bool i) const
Visit a boolean (don't override this, instead override visitBoolean)
Definition: visitors.hpp:160
result_type operator()(int64_t &i) const
Visit an integer (don't override this, instead override visitInteger)
Definition: visitors.hpp:58
result_type visitMap(map_t &i) const
Visit a map.
Definition: visitors.hpp:95
DERIVED derived_t
The type of the derived class.
Definition: visitors.hpp:50
result_type operator()(double i) const
Visit a double (don't override this, instead override visitDouble)
Definition: visitors.hpp:155
result_type operator()(const map_t &i) const
Visit a map (don't override this, instead override visitMap)
Definition: visitors.hpp:185
result_type visitBoolean(bool &i) const
Visit a boolean.
Definition: visitors.hpp:70
result_type operator()(map_t &i) const
Visit a map (don't override this, instead override visitMap)
Definition: visitors.hpp:93
RV result_type
The return type from applying this visitor.
Definition: visitors.hpp:140
result_type operator()(decimal_t &i) const
Visit a decimal (don't override this, instead override visitDecimal)
Definition: visitors.hpp:78
result_type visitList(const list_t &i) const
Visit a list.
Definition: visitors.hpp:182
result_type operator()(sag_underlying_custom_t &i) const
Visit a custom object (don't override this, instead override visitCustom)
Definition: visitors.hpp:98
result_type error(const std::string &reason) const
Handles visiting unhandled types.
Definition: visitors.hpp:197
result_type operator()(const sag_underlying_custom_t &i) const
Visit a custom object (don't override this, instead override visitCustom)
Definition: visitors.hpp:190
result_type operator()() const
Visit an empty variant (don't override this, instead override visitEmpty)
Definition: visitors.hpp:145
result_type visitString(const char *i) const
Visit a string.
Definition: visitors.hpp:167
Helper class for writing visitors to apply to data_t.
Definition: visitors.hpp:136
RV result_type
The return type from applying this visitor.
Definition: visitors.hpp:48
result_type operator()(const decimal_t &i) const
Visit a decimal (don't override this, instead override visitDecimal)
Definition: visitors.hpp:170
result_type operator()(int64_t i) const
Visit an integer (don't override this, instead override visitInteger)
Definition: visitors.hpp:150
result_type visitList(list_t &i) const
Visit a list.
Definition: visitors.hpp:90
result_type visitBuffer(buffer_t &i) const
Visit a byte buffer.
Definition: visitors.hpp:85
result_type visitDecimal(decimal_t &i) const
Visit a decimal.
Definition: visitors.hpp:80