MCPcopy Create free account
hub / github.com/apache/qpid-proton / uniform_containers

Function uniform_containers

cpp/examples/encode_decode.cpp:62–111  ·  view source on GitHub ↗

Insert/extract native C++ containers with uniform type values.

Source from the content-addressed store, hash-verified

60
61// Insert/extract native C++ containers with uniform type values.
62static 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.
114static void mixed_containers() {

Callers 1

mainFunction · 0.85

Calls 4

printFunction · 0.85
listFunction · 0.85
getFunction · 0.50
valueFunction · 0.50

Tested by

no test coverage detected