wamp_arguments.hpp
1 //
3 // Copyright (c) Crossbar.io Technologies GmbH and contributors
4 //
5 // Boost Software License - Version 1.0 - August 17th, 2003
6 //
7 // Permission is hereby granted, free of charge, to any person or organization
8 // obtaining a copy of the software and accompanying documentation covered by
9 // this license (the "Software") to use, reproduce, display, distribute,
10 // execute, and transmit the Software, and to prepare derivative works of the
11 // Software, and to permit third-parties to whom the Software is furnished to
12 // do so, all subject to the following:
13 //
14 // The copyright notices in the Software and this entire statement, including
15 // the above license grant, this restriction and the following disclaimer,
16 // must be included in all copies of the Software, in whole or in part, and
17 // all derivative works of the Software, unless such copies or derivative
18 // works are solely in the form of machine-executable object code generated by
19 // a source language processor.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 //
30 
31 #ifndef AUTOBAHN_WAMP_ARGUMENTS_HPP
32 #define AUTOBAHN_WAMP_ARGUMENTS_HPP
33 
34 #include <array>
35 #include <msgpack.hpp>
36 #include <string>
37 #include <unordered_map>
38 #include <vector>
39 
40 namespace autobahn {
41 
42 using wamp_arguments = std::vector<msgpack::object>;
43 using wamp_kw_arguments = std::unordered_map<std::string, msgpack::object>;
44 
45 static msgpack::zone EMPTY_ARGUMENTS_ZONE;
46 static msgpack::zone EMPTY_KW_ARGUMENTS_ZONE;
47 static const msgpack::object EMPTY_ARGUMENTS(std::array<msgpack::object, 0>(), &EMPTY_ARGUMENTS_ZONE);
48 static const msgpack::object EMPTY_KW_ARGUMENTS(wamp_kw_arguments(), &EMPTY_KW_ARGUMENTS_ZONE);
49 
50 
51 //msgpack map utilities.
52 //TODO: refactor event & invocation to used these
53 template <typename T>
54 inline T value_for_key(const msgpack::object& object, const std::string& key)
55 {
56  if (object.type != msgpack::type::MAP) {
57  throw msgpack::type_error();
58  }
59  for (std::size_t i = 0; i < object.via.map.size; ++i) {
60  const msgpack::object_kv& kv = object.via.map.ptr[i];
61  if (kv.key.type == msgpack::type::STR && key.size() == kv.key.via.str.size
62  && key.compare(0, key.size(), kv.key.via.str.ptr, kv.key.via.str.size) == 0)
63  {
64  return kv.val.as<T>();
65  }
66  }
67  throw std::out_of_range(key + " keyword argument doesn't exist");
68 }
69 
70 template <typename T>
71 inline T value_for_key(const msgpack::object& object, const char* key)
72 {
73  if (object.type != msgpack::type::MAP) {
74  throw msgpack::type_error();
75  }
76  std::size_t key_size = strlen(key);
77  for (std::size_t i = 0; i < object.via.map.size; ++i) {
78  const msgpack::object_kv& kv = object.via.map.ptr[i];
79  if (kv.key.type == msgpack::type::STR && key_size == kv.key.via.str.size
80  && memcmp(key, kv.key.via.str.ptr, key_size) == 0)
81  {
82  return kv.val.as<T>();
83  }
84  }
85  throw std::out_of_range(std::string(key) + " keyword argument doesn't exist");
86 }
87 
88 template <typename T>
89 inline T value_for_key_or(const msgpack::object& object, const std::string& key, const T& fallback)
90 {
91  if (object.type != msgpack::type::MAP) {
92  throw msgpack::type_error();
93  }
94  for (std::size_t i = 0; i < object.via.map.size; ++i) {
95  const msgpack::object_kv& kv = object.via.map.ptr[i];
96  if (kv.key.type == msgpack::type::STR && key.size() == kv.key.via.str.size
97  && key.compare(0, key.size(), kv.key.via.str.ptr, kv.key.via.str.size) == 0)
98  {
99  return kv.val.as<T>();
100  }
101  }
102  return fallback;
103 }
104 
105 template <typename T>
106 inline T value_for_key_or(const msgpack::object& object, const char* key, const T& fallback)
107 {
108  if (object.type != msgpack::type::MAP) {
109  throw msgpack::type_error();
110  }
111  std::size_t key_size = strlen(key);
112  for (std::size_t i = 0; i < object.via.map.size; ++i) {
113  const msgpack::object_kv& kv = object.via.map.ptr[i];
114  if (kv.key.type == msgpack::type::STR && key_size == kv.key.via.str.size
115  && memcmp(key, kv.key.via.str.ptr, key_size) == 0)
116  {
117  return kv.val.as<T>();
118  }
119  }
120  return fallback;
121 }
122 } // namespace autobahn
123 
124 #endif // AUTOBAHN_WAMP_ARGUMENTS_HPP