| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <proton/types.hpp> |
| 21 |
#include <proton/codec/encoder.hpp> |
| 22 |
#include <proton/codec/decoder.hpp> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <iostream> |
| 26 |
#include <iterator> |
| 27 |
#include <sstream> |
| 28 |
|
| 29 |
// Examples of how to use the encoder and decoder to create and examine AMQP values. |
| 30 |
// |
| 31 |
|
| 32 |
// Print is defined at the end as an example of how to query and extract complex |
| 33 |
// values from a decoder in terms of their simple components. |
| 34 |
void print(proton::value&); |
| 35 |
|
| 36 |
// Some helper templates to print map and std::vector results. |
| 37 |
namespace std { |
| 38 |
template<class T, class U> ostream& operator<<(ostream& o, const std::pair<T,U>& p) { |
| 39 |
return o << p.first << ":" << p.second; |
| 40 |
} |
| 41 |
template<class T> ostream& operator<<(ostream& o, const std::vector<T>& v) { |
| 42 |
o << "[ "; |
| 43 |
ostream_iterator<T> oi(o, " "); |
| 44 |
copy(v.begin(), v.end(), oi); |
| 45 |
return o << "]"; |
| 46 |
} |
| 47 |
template<class T> ostream& operator<<(ostream& o, const std::list<T>& v) { |
| 48 |
o << "[ "; |
| 49 |
ostream_iterator<T> oi(o, " "); |
| 50 |
copy(v.begin(), v.end(), oi); |
| 51 |
return o << "]"; |
| 52 |
} |
| 53 |
template<class K, class T> ostream& operator<<(ostream& o, const map<K, T>& m) { |
| 54 |
o << "{ "; |
| 55 |
ostream_iterator<std::pair<K,T> > oi(o, " "); |
| 56 |
copy(m.begin(), m.end(), oi); |
| 57 |
return o << "}"; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Insert/extract native C++ containers with uniform type values. |
| 62 |
static void uniform_containers() { |
| 63 |
std::cout << std::endl << "== Array, list and map of uniform type." << std::endl; |
| 64 |
proton::value v; |
| 65 |
|
| 66 |
std::vector<int> a; |
| 67 |
a.push_back(1); |
| 68 |
a.push_back(2); |
| 69 |
a.push_back(3); |
| 70 |
// By default a C++ container is encoded as an AMQP array. |
| 71 |
v = a; |
| 72 |
print(v); |
| 73 |
std::list<int> a1; |
| 74 |
proton::get(v, a1); |
| 75 |
std::cout << a1 << std::endl; |
| 76 |
|
| 77 |
// You can specify that a container should be encoded as an AMQP list instead. |
| 78 |
v = proton::codec::encoder::list(a1); |
| 79 |
print(v); |
| 80 |
std::cout << proton::get<std::vector<int> >(v) << std::endl; |
| 81 |
|
| 82 |
// C++ map types (types with key_type, mapped_type) convert to an AMQP map by default. |
| 83 |
std::map<std::string, int> m; |
| 84 |
m["one"] = 1; |
| 85 |
m["two"] = 2; |
| 86 |
v = m; |
| 87 |
print(v); |
| 88 |
std::cout << proton::get<std::map<std::string, int> >(v) << std::endl; |
| 89 |
|
| 90 |
// A sequence of pairs encodes as an AMQP MAP, which lets you control the encoded order. |
| 91 |
std::vector<std::pair<std::string, int> > pairs; |
| 92 |
pairs.push_back(std::make_pair("z", 3)); |
| 93 |
pairs.push_back(std::make_pair("a", 4)); |
| 94 |
v = pairs; |
| 95 |
print(v); |
| 96 |
|
| 97 |
// You can also decode an AMQP map as a sequence of pairs to preserve encode order. |
| 98 |
std::vector<std::pair<std::string, int> > pairs2; |
| 99 |
proton::codec::decoder d(v); |
| 100 |
d >> pairs2; |
| 101 |
std::cout << pairs2 << std::endl; |
| 102 |
|
| 103 |
// A vector of proton::value is normally encoded as a mixed-type AMQP LIST, |
| 104 |
// but you can encoded it as an array provided all the values match the array type. |
| 105 |
std::vector<proton::value> vv; |
| 106 |
vv.push_back(proton::value("a")); |
| 107 |
vv.push_back(proton::value("b")); |
| 108 |
vv.push_back(proton::value("c")); |
| 109 |
v = vv; |
| 110 |
print(v); |
| 111 |
} |
| 112 |
|
| 113 |
// Containers with mixed types use value to represent arbitrary AMQP types. |
| 114 |
static void mixed_containers() { |
| 115 |
std::cout << std::endl << "== List and map of mixed type values." << std::endl; |
| 116 |
proton::value v; |
| 117 |
|
| 118 |
std::vector<proton::value> l; |
| 119 |
l.push_back(proton::value(42)); |
| 120 |
l.push_back(proton::value(std::string("foo"))); |
| 121 |
// By default, a sequence of proton::value is treated as an AMQP list. |
| 122 |
v = l; |
| 123 |
print(v); |
| 124 |
std::vector<proton::value> l2 = proton::get<std::vector<proton::value> >(v); |
| 125 |
std::cout << l2 << std::endl; |
| 126 |
|
| 127 |
std::map<proton::value, proton::value> m; |
| 128 |
m[proton::value("five")] = proton::value(5); |
| 129 |
m[proton::value(4)] = proton::value("four"); v = m; |
| 130 |
print(v); |
| 131 |
typedef std::map<proton::value, proton::value> value_map; |
| 132 |
value_map m2(proton::get<value_map>(v)); |
| 133 |
std::cout << m2 << std::endl; |
| 134 |
} |
| 135 |
|
| 136 |
// Insert using stream operators (see print_next for example of extracting with stream ops.) |
| 137 |
static void insert_stream_operators() { |
| 138 |
std::cout << std::endl << "== Insert with stream operators." << std::endl; |
| 139 |
proton::value v; |
| 140 |
|
| 141 |
// Create an array of INT with values [1, 2, 3] |
| 142 |
proton::codec::encoder e(v); |
| 143 |
e << proton::codec::start::array(proton::INT) |
| 144 |
<< int32_t(1) << int32_t(2) << int32_t(3) |
| 145 |
<< proton::codec::finish(); |
| 146 |
print(v); |
| 147 |
|
| 148 |
// Create a mixed-type list of the values [42, 0, "x"]. |
| 149 |
proton::codec::encoder e2(v); |
| 150 |
e2 << proton::codec::start::list() |
| 151 |
<< int32_t(42) << false << proton::symbol("x") |
| 152 |
<< proton::codec::finish(); |
| 153 |
print(v); |
| 154 |
|
| 155 |
// Create a map { "k1":42, "k2": false } |
| 156 |
proton::codec::encoder e3(v); |
| 157 |
e3 << proton::codec::start::map() |
| 158 |
<< "k1" << int32_t(42) |
| 159 |
<< proton::symbol("k2") << false |
| 160 |
<< proton::codec::finish(); |
| 161 |
print(v); |
| 162 |
} |
| 163 |
|
| 164 |
int main(int, char**) { |
| 165 |
try { |
| 166 |
uniform_containers(); |
| 167 |
mixed_containers(); |
| 168 |
insert_stream_operators(); |
| 169 |
return 0; |
| 170 |
} catch (const std::exception& e) { |
| 171 |
std::cerr << std::endl << "error: " << e.what() << std::endl; |
| 172 |
} |
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
// print_next prints the next value from values by recursively descending into complex values. |
| 177 |
// |
| 178 |
// NOTE this is for example purposes only: There is a built in ostream operator<< for values. |
| 179 |
// |
| 180 |
// |
| 181 |
static void print_next(proton::codec::decoder& d) { |
| 182 |
proton::type_id type = d.next_type(); |
| 183 |
proton::codec::start s; |
| 184 |
switch (type) { |
| 185 |
case proton::ARRAY: { |
| 186 |
d >> s; |
| 187 |
std::cout << "array<" << s.element; |
| 188 |
if (s.is_described) { |
| 189 |
std::cout << ", descriptor="; |
| 190 |
print_next(d); |
| 191 |
} |
| 192 |
std::cout << ">["; |
| 193 |
for (size_t i = 0; i < s.size; ++i) { |
| 194 |
if (i) std::cout << ", "; |
| 195 |
print_next(d); |
| 196 |
} |
| 197 |
std::cout << "]"; |
| 198 |
d >> proton::codec::finish(); |
| 199 |
break; |
| 200 |
} |
| 201 |
case proton::LIST: { |
| 202 |
d >> s; |
| 203 |
std::cout << "list["; |
| 204 |
for (size_t i = 0; i < s.size; ++i) { |
| 205 |
if (i) std::cout << ", "; |
| 206 |
print_next(d); |
| 207 |
} |
| 208 |
std::cout << "]"; |
| 209 |
d >> proton::codec::finish(); |
| 210 |
break; |
| 211 |
} |
| 212 |
case proton::MAP: { |
| 213 |
d >> s; |
| 214 |
std::cout << "map{"; |
| 215 |
for (size_t i = 0; i < s.size/2; ++i) { |
| 216 |
if (i) std::cout << ", "; |
| 217 |
print_next(d); |
| 218 |
std::cout << ":"; // key:value |
| 219 |
print_next(d); |
| 220 |
} |
| 221 |
std::cout << "}"; |
| 222 |
d >> proton::codec::finish(); |
| 223 |
break; |
| 224 |
} |
| 225 |
case proton::DESCRIBED: { |
| 226 |
d >> s; |
| 227 |
std::cout << "described("; |
| 228 |
print_next(d); // Descriptor |
| 229 |
print_next(d); // value |
| 230 |
d >> proton::codec::finish(); |
| 231 |
break; |
| 232 |
} |
| 233 |
default: |
| 234 |
// A simple type. We could continue the switch for all AMQP types but |
| 235 |
// we will take a short cut and extract to another value and print that. |
| 236 |
proton::value v2; |
| 237 |
d >> v2; |
| 238 |
std::cout << type << "(" << v2 << ")"; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Print a value, for example purposes. Normal code can use operator<< |
| 243 |
void print(proton::value& v) { |
| 244 |
proton::codec::decoder d(v); |
| 245 |
d.rewind(); |
| 246 |
while (d.more()) { |
| 247 |
print_next(d); |
| 248 |
if (d.more()) std::cout << ", "; |
| 249 |
} |
| 250 |
std::cout << std::endl; |
| 251 |
} |