Debian Codegraph @

cpp/examples/message_properties.cpp

raw
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/message.hpp>
21 #include <proton/types.hpp>
22 #include <iostream>
23 #include <map>
24
25 int main(int argc, char **argv) {
26 try {
27 proton::message m;
28
29 // Setting properties: legal types are converted automatically to their
30 // AMQP counterpart.
31 m.properties().put("short", int16_t(123));
32 m.properties().put("string", "foo");
33 m.properties().put("symbol", proton::symbol("sym"));
34
35 // Examining properties using proton::get()
36
37 // 1 argument get<>() template specifies expected type of property.
38 std::string s = proton::get<std::string>(m.properties().get("string"));
39
40 // 2 argument get, property must have matching type to output argument.
41 int16_t i;
42 proton::get(m.properties().get("short"), i);
43
44 // Checking property types
45 proton::type_id type = m.properties().get("symbol").type();
46 if (type != proton::SYMBOL) {
47 throw std::logic_error("wrong type!");
48 }
49
50 // proton::scalar has its own ostream <<
51 std::cout << "using put/get:"
52 << " short=" << i
53 << " string=" << s
54 << " symbol=" << m.properties().get("symbol")
55 << std::endl;
56
57 // Converting properties to a convertible type
58 std::cout << "using coerce:"
59 << " short(as long)="
60 << proton::coerce<long>(m.properties().get("short"))
61 << std::endl;
62
63 // Extract the properties as a std::map for more complex map operations.
64 // You can use other map and sequence types to hold a map, see @ref types_page
65 typedef std::map<std::string, proton::scalar> property_map;
66 property_map props;
67 proton::get(m.properties(), props);
68 for (property_map::iterator i = props.begin(); i != props.end(); ++i) {
69 std::cout << "props[" << i->first << "]=" << i->second << std::endl;
70 }
71 props["string"] = "bar";
72 props["short"] = 42;
73 // Update the properties in the message from the modified props map
74 m.properties() = props;
75
76 std::cout << "short=" << m.properties().get("short")
77 << " string=" << m.properties().get("string")
78 << std::endl;
79
80 // proton::get throws an exception if types do not match exactly.
81 try {
82 proton::get<uint32_t>(m.properties().get("short")); // bad: uint32_t != int16_t
83 throw std::logic_error("expected exception");
84 } catch (const proton::conversion_error& e) {
85 std::cout << "expected conversion_error: \"" << e.what() << '"' << std::endl;
86 }
87
88 // proton::coerce throws an exception if types are not convertible.
89 try {
90 proton::get<uint32_t>(m.properties().get("string")); // bad: string to uint32_t
91 throw std::logic_error("expected exception");
92 } catch (const proton::conversion_error& e) {
93 std::cout << "expected conversion_error: \"" << e.what() << '"' << std::endl;
94 }
95
96 return 0;
97 } catch (const std::exception& e) {
98 std::cerr << "unexpected exception: " << e.what() << std::endl;
99 return 1;
100 }
101 }