CppWAMP
C++11 client library for the WAMP protocol
socketoptions.hpp
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_SOCKETOPTIONS_HPP
8 #define CPPWAMP_SOCKETOPTIONS_HPP
9 
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 namespace wamp
15 {
16 
17 namespace internal
18 {
19 
20 //------------------------------------------------------------------------------
21 // Base class for polymorphic socket options.
22 //------------------------------------------------------------------------------
23 template <typename TProtocol>
24 class SocketOptionBase
25 {
26 public:
27  using Protocol = TProtocol;
28  using Ptr = std::shared_ptr<SocketOptionBase>;
29 
30  virtual ~SocketOptionBase() {}
31 
32  virtual int level(const Protocol& p) const = 0;
33 
34  virtual int name(const Protocol& p) const = 0;
35 
36  virtual const void* data(const Protocol& p) const = 0;
37 
38  virtual size_t size(const Protocol& p) const = 0;
39 };
40 
41 //------------------------------------------------------------------------------
42 // Polymorphic wrapper around a Boost Asio socket option.
43 //------------------------------------------------------------------------------
44 template <typename TProtocol, typename TOption>
45 class SocketOptionWrapper : public SocketOptionBase<TProtocol>
46 {
47 public:
48  using Protocol = TProtocol;
49  using Option = TOption;
50 
51  SocketOptionWrapper(TOption&& option) : option_(std::move(option)) {}
52 
53  virtual int level(const Protocol& p) const override
54  {return option_.level(p);}
55 
56  virtual int name(const Protocol& p) const override
57  {return option_.name(p);}
58 
59  virtual const void* data(const Protocol& p) const override
60  {return option_.data(p);}
61 
62  virtual size_t size(const Protocol& p) const override
63  {return option_.size(p);}
64 
65 private:
66  TOption option_;
67 };
68 
69 //------------------------------------------------------------------------------
70 // Polymorphic holder of a Boost Asio socket option.
71 // Meets the SettableSocketOption requirement of Boost.Asio.
72 // https://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/SettableSocketOption.html
73 //------------------------------------------------------------------------------
74 template <typename TProtocol>
75 class SocketOption
76 {
77 public:
78  using Protocol = TProtocol;
79 
80  template <typename TOption>
81  SocketOption(TOption&& option)
82  {
83  using DecayedOption = typename std::decay<TOption>::type;
84  using ConcreteOption = SocketOptionWrapper<TProtocol, DecayedOption>;
85  option_.reset(new ConcreteOption(std::move(option)));
86  }
87 
88  int level(const Protocol& p) const {return option_->level(p);}
89 
90  int name(const Protocol& p) const {return option_->name(p);}
91 
92  const void* data(const Protocol& p) const {return option_->data(p);}
93 
94  size_t size(const Protocol& p) const {return option_->size(p);}
95 
96 private:
97  typename SocketOptionBase<TProtocol>::Ptr option_;
98 };
99 
100 //------------------------------------------------------------------------------
101 // Generic socket option container
102 //------------------------------------------------------------------------------
103 template <typename TProtocol>
104 class SocketOptionList
105 {
106 public:
107  using Protocol = TProtocol;
108  using Option = SocketOption<Protocol>;
109 
110  template <typename TOption>
111  void add(TOption&& option)
112  {
113  options_.emplace_back(std::forward<TOption>(option));
114  }
115 
116  template <typename TSocket>
117  void applyTo(TSocket& socket) const
118  {
119  for (const auto& opt: options_)
120  socket.set_option(opt);
121  }
122 
123 private:
124  std::vector<Option> options_;
125 };
126 
127 } // namespace internal
128 
129 } // namespace wamp
130 
131 #endif // CPPWAMP_SOCKETOPTIONS_HPP
wamp
Definition: anyhandler.hpp:36