Apama  10.1.0.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
map_extractor.hpp
Go to the documentation of this file.
1 /*
2  * Title: bits/map_extractor.hpp
3  * Description: Helper class for extracting values from the map_t type
4  * $Copyright (c) 2017 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  */
7 
14 #ifndef _DATAT_BITS_INCLUDE_TAG
15 #error Must only be included from sag_connectivity_cpp.hpp, use that instead
16 #endif
17 
25 {
26 public:
31  MapExtractor(const map_t &map, const std::string &displayName)
32  : map(map), keysAlreadyAccessed(), displayName(displayName)
33  {
34  if (displayName.empty())
35  {
36  throw std::runtime_error("displayName cannot be empty");
37  }
38  }
39 
40  MapExtractor(const MapExtractor &) = delete;
41  MapExtractor &operator=(const MapExtractor &) = delete;
42  MapExtractor &operator=(MapExtractor &&) = delete;
43  ~MapExtractor() = default;
44 
47  map(other.map),
48  keysAlreadyAccessed(std::move(other.keysAlreadyAccessed)),
49  displayName(std::move(other.displayName))
50  {}
51 
53  bool empty()
54  {
55  return map.empty();
56  }
57 
60  {
61  return map;
62  }
63 
65  std::string getDisplayName()
66  {
67  return displayName;
68  }
69 
74  std::string toString();
75 
80  {
81  std::vector<std::string> remaining;
82  for (auto it = map.begin(); it != map.end(); ++it)
83  {
84  if (keysAlreadyAccessed.find(it->first) == keysAlreadyAccessed.end())
85  {
86  remaining.push_back(stringifyKey(it->first));
87  }
88  }
89  if (remaining.empty()) return;
90 
91  std::stringstream ss;
92  for (auto it = remaining.begin(); it != remaining.end(); ++it)
93  {
94  ss << "'" << *it << "'" << " ";
95  }
96  throw std::runtime_error("Found unexpected items in the map '" + displayName + "': " + ss.str());
97  }
98 
109  template<typename T>
110  const is_not_convertable_t<T> &get(const data_t& key)
111  {
112  return com::softwareag::connectivity::get<T>(doGet(key));
113  }
114 
127  template<typename T>
128  const is_convertable_t<T> get(const data_t& key)
129  {
130  return com::softwareag::connectivity::convert_to<T>(doGet(key));
131  }
132 
140  template<typename T>
141  const T get(const data_t& key, const T defaultValue)
142  {
143  try
144  {
145  return get<T>(key);
146  }
147  catch (const std::exception &)
148  {
149  return defaultValue;
150  }
151  }
152 
153 
158  MapExtractor getMap(const data_t& key, bool emptyIfMissing)
159  {
160  try
161  {
162  return MapExtractor(get<map_t>(key), displayName + "." + stringifyKey(key));
163  }
164  catch (const std::exception &)
165  {
166  if (emptyIfMissing)
167  {
168  return MapExtractor(emptyMap, displayName + "." + stringifyKey(key));
169  }
170  throw;
171  }
172  }
173 
177  std::string getStringDisallowEmpty(const data_t& key)
178  {
179  std::string ret = get<const char*>(key);
180  if (ret.empty())
181  {
182  throw std::runtime_error("The string value associated with key '" + stringifyKey(key) + "' in " + displayName + " is empty");
183  }
184  return ret;
185  }
186 
190  std::string getStringDisallowEmpty(const data_t& key, std::string defaultValue)
191  {
192  try {
193  return getStringDisallowEmpty(key);
194  } catch (const std::exception &) {
195  return defaultValue;
196  }
197  }
198 
202  std::string getStringAllowEmpty(const data_t& key)
203  {
204  return get<const char*>(key);
205  }
206 
210  std::string getStringAllowEmpty(const data_t& key, std::string defaultValue)
211  {
212  try {
213  return getStringAllowEmpty(key);
214  } catch (const std::exception &) {
215  return defaultValue;
216  }
217  }
218 
219 private:
223  const data_t& doGet(const data_t& key)
224  {
225  keysAlreadyAccessed[key.copy()] = data_t();
226 
227  auto it = map.find(key);
228  if (it == map.end())
229  {
230  throw std::runtime_error("Key '" + stringifyKey(key) + "' not found in " + displayName);
231  }
232  if (it->second.type_tag() == SAG_DATA_EMPTY)
233  {
234  throw std::runtime_error("Key '" + stringifyKey(key) + "' has an empty value in " + displayName);
235  }
236  return it->second;
237  }
238 
240  std::string stringifyKey(const data_t& data);
241 
243  const map_t& map;
245  map_t keysAlreadyAccessed;
247  const std::string displayName;
249  const map_t emptyMap;
250 };
std::string getDisplayName()
Returns the display name associated with this map.
Definition: map_extractor.hpp:65
iterator find(const data_t &k)
Searches the map for an item with the given key and returns an iterator to that position in the map...
std::string toString()
Returns a string representation of this object, including both the display name and map contents...
std::string getStringAllowEmpty(const data_t &key, std::string defaultValue)
Returns a string value, returning the defaultValue if the key is missing or the associated value is t...
Definition: map_extractor.hpp:210
const map_t & getUnderlyingMap()
Returns a reference to the wrapped map.
Definition: map_extractor.hpp:59
A map class which implements many of the functions on std::map.
Definition: map_t.hpp:36
iterator end()
Forward iterator end.
Definition: map_t.hpp:274
STL namespace.
Provides a compile-time type-safe way to extract values from a map_t, with user-friendly error messag...
Definition: map_extractor.hpp:24
bool empty()
Returns true if the map is empty.
Definition: map_extractor.hpp:53
MapExtractor getMap(const data_t &key, bool emptyIfMissing)
Returns a MapExtractor wrapping a map_t from the map.
Definition: map_extractor.hpp:158
data_t copy() const
Return a deep copy of this data_t.
iterator begin()
Forward iterator begin.
Definition: map_t.hpp:272
std::string getStringAllowEmpty(const data_t &key)
Returns a string value, throwing an exception if the key is missing or the associated value is the em...
Definition: map_extractor.hpp:202
bool empty() const
Returns true if the map is empty (size() == 0)
Definition: map_t.hpp:288
MapExtractor(MapExtractor &&other)
Construct a MapExtractor, moving the contents from another MapExtractor.
Definition: map_extractor.hpp:46
void checkNoItemsRemaining()
Checks that all entries in this map have been read (using one of the get functions in this class)...
Definition: map_extractor.hpp:79
A variant type which can be one of the following:
Definition: data_t.hpp:42
std::string getStringDisallowEmpty(const data_t &key)
Returns a string value, throwing an exception if the key is missing, the associated value is the empt...
Definition: map_extractor.hpp:177
MapExtractor(const map_t &map, const std::string &displayName)
Construct a MapExtractor wrapping the specified map.
Definition: map_extractor.hpp:31
Empty.
Definition: sag_connectivity_c.h:36
std::string getStringDisallowEmpty(const data_t &key, std::string defaultValue)
Returns a string value, returning the defaultValue if the key is missing, the associated value is the...
Definition: map_extractor.hpp:190