CppWAMP
C++11 client library for the WAMP protocol
options.hpp
Go to the documentation of this file.
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_OPTIONS_HPP
8 #define CPPWAMP_OPTIONS_HPP
9 
10 #include <utility>
11 #include "api.hpp"
12 #include "traits.hpp"
13 #include "variant.hpp"
14 #include "./internal/passkey.hpp"
15 #include "./internal/wampmessage.hpp"
16 
17 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
21 
22 namespace wamp
23 {
24 
25 //------------------------------------------------------------------------------
27 //------------------------------------------------------------------------------
28 template <typename TDerived, typename TMessage>
29 class CPPWAMP_API Options
30 {
31 public:
33  TDerived& withOption(String key, Variant value)
34  {
35  message_.options().emplace(std::move(key), std::move(value));
36  return static_cast<TDerived&>(*this);
37  }
38 
40  TDerived& withOptions(Object opts)
41  {
42  message_.options() = std::move(opts);
43  return static_cast<TDerived&>(*this);
44  }
45 
47  const Object& options() const {return message_.options();}
48 
50  Variant optionByKey(const String& key) const
51  {
52  Variant result;
53  auto iter = options().find(key);
54  if (iter != options().end())
55  result = iter->second;
56  return result;
57  }
58 
60  template <typename T>
62  const String& key,
63  T&& fallback
65  ) const
66  {
67  auto iter = options().find(key);
68  if (iter != options().end())
69  return iter->second.template to<ValueTypeOf<T>>();
70  else
71  return std::forward<T>(fallback);
72  }
73 
74 protected:
75  using MessageType = TMessage;
76 
78  template <typename... TArgs>
79  explicit Options(TArgs&&... args)
80  : message_(std::forward<TArgs>(args)...)
81  {}
82 
83  MessageType& message() {return message_;}
84 
85  const MessageType& message() const {return message_;}
86 
87 private:
88  MessageType message_;
89 
90 public:
91  // Internal use only
92  MessageType& message(internal::PassKey) {return message_;}
93 };
94 
95 } // namespace wamp
96 
97 #endif // CPPWAMP_OPTIONS_HPP
wamp::Options::optionByKey
Variant optionByKey(const String &key) const
Obtains an option by key.
Definition: options.hpp:50
wamp::Variant
Discriminated union container that represents a JSON value.
Definition: variant.hpp:134
wamp::Options::withOptions
TDerived & withOptions(Object opts)
Sets all options at once.
Definition: options.hpp:40
wamp::Options::withOption
TDerived & withOption(String key, Variant value)
Adds an option.
Definition: options.hpp:33
api.hpp
Defines macros related to exporting/importing APIs.
wamp::Object
std::map< String, Variant > Object
Variant bound type for maps of variants.
Definition: variantdefs.hpp:52
wamp::Options::Options
Options(TArgs &&... args)
Constructor taking message construction aruments.
Definition: options.hpp:79
wamp
Definition: anyhandler.hpp:36
wamp::Options
Wrapper around a WAMP message containing an options dictionary.
Definition: options.hpp:29
wamp::ValueTypeOf
typename std::remove_cv< typename std::remove_reference< T >::type >::type ValueTypeOf
Metafunction used to obtain the plain value type of a parameter passed by universal reference.
Definition: traits.hpp:51
variant.hpp
Contains the declaration of Variant and other closely related types/functions.
traits.hpp
Contains general-purpose type traits.
wamp::Options::options
const Object & options() const
Accesses the entire dictionary of options.
Definition: options.hpp:47
wamp::Options::optionOr
ValueTypeOf< T > optionOr(const String &key, T &&fallback) const
Obtains an option by key or a fallback value.
Definition: options.hpp:61
wamp::String
std::string String
Variant bound type for text strings.
Definition: variantdefs.hpp:50