Debian Codegraph @

cpp/src/sender_options.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "proton/codec/map.hpp"
23 #include "proton/sender_options.hpp"
24 #include "proton/messaging_handler.hpp"
25 #include "proton/source_options.hpp"
26 #include "proton/target_options.hpp"
27
28 #include "proactor_container_impl.hpp"
29 #include "contexts.hpp"
30 #include "messaging_adapter.hpp"
31 #include "proton_bits.hpp"
32
33 namespace proton {
34
35 template <class T> struct option {
36 T value;
37 bool set;
38
39 option() : value(), set(false) {}
40 option& operator=(const T& x) { value = x; set = true; return *this; }
41 void update(const option<T>& x) { if (x.set) *this = x.value; }
42 };
43
44 class sender_options::impl {
45 static link_context& get_context(sender l) {
46 return link_context::get(unwrap(l));
47 }
48
49 static void set_delivery_mode(sender l, proton::delivery_mode mode) {
50 switch (mode) {
51 case delivery_mode::AT_MOST_ONCE:
52 pn_link_set_snd_settle_mode(unwrap(l), PN_SND_SETTLED);
53 break;
54 case delivery_mode::AT_LEAST_ONCE:
55 pn_link_set_snd_settle_mode(unwrap(l), PN_SND_UNSETTLED);
56 pn_link_set_rcv_settle_mode(unwrap(l), PN_RCV_FIRST);
57 break;
58 default:
59 break;
60 }
61 }
62
63 public:
64 option<messaging_handler*> handler;
65 option<proton::delivery_mode> delivery_mode;
66 option<bool> auto_settle;
67 option<source_options> source;
68 option<target_options> target;
69 option<std::string> name;
70 option<std::map<symbol, value> > properties;
71
72 void apply(sender& s) {
73 if (s.uninitialized()) {
74 if (delivery_mode.set) set_delivery_mode(s, delivery_mode.value);
75 if (handler.set && handler.value) container::impl::set_handler(s, handler.value);
76 if (auto_settle.set) get_context(s).auto_settle = auto_settle.value;
77 if (source.set) {
78 proton::source local_s(make_wrapper<proton::source>(pn_link_source(unwrap(s))));
79 source.value.apply(local_s);
80 }
81 if (target.set) {
82 proton::target local_t(make_wrapper<proton::target>(pn_link_target(unwrap(s))));
83 target.value.apply(local_t);
84 }
85 if (properties.set) {
86 value(pn_link_properties(unwrap(s))) = properties.value;
87 }
88 }
89 }
90
91 void update(const impl& x) {
92 handler.update(x.handler);
93 delivery_mode.update(x.delivery_mode);
94 auto_settle.update(x.auto_settle);
95 source.update(x.source);
96 target.update(x.target);
97 name.update(x.name);
98 properties.update(x.properties);
99 }
100
101 };
102
103 sender_options::sender_options() : impl_(new impl()) {}
104 sender_options::sender_options(const sender_options& x) : impl_(new impl()) {
105 *this = x;
106 }
107 sender_options::~sender_options() = default;
108
109 sender_options& sender_options::operator=(const sender_options& x) {
110 *impl_ = *x.impl_;
111 return *this;
112 }
113
114 void sender_options::update(const sender_options& x) { impl_->update(*x.impl_); }
115
116 sender_options& sender_options::handler(class messaging_handler &h) { impl_->handler = &h; return *this; }
117 sender_options& sender_options::delivery_mode(proton::delivery_mode m) {impl_->delivery_mode = m; return *this; }
118 sender_options& sender_options::auto_settle(bool b) {impl_->auto_settle = b; return *this; }
119 sender_options& sender_options::source(const source_options &s) {impl_->source = s; return *this; }
120 sender_options& sender_options::target(const target_options &s) {impl_->target = s; return *this; }
121 sender_options& sender_options::name(const std::string &s) {impl_->name = s; return *this; }
122 sender_options& sender_options::properties(const std::map<symbol, value> &props) { impl_->properties = props; return *this; }
123
124 void sender_options::apply(sender& s) const { impl_->apply(s); }
125
126 const std::string* sender_options::get_name() const {
127 return impl_->name.set ? &impl_->name.value : 0;
128 }
129 } // namespace proton