C++ Client-Side SDK
LaunchDarkly SDK
attributes_builder.hpp
1 #pragma once
2 
3 #include <string>
4 
5 #include <launchdarkly/attribute_reference.hpp>
6 #include <launchdarkly/attributes.hpp>
7 #include <launchdarkly/value.hpp>
8 
9 namespace launchdarkly {
10 
11 class ContextBuilder;
12 
21 template <class BuilderReturn, class BuildType>
22 class AttributesBuilder final {
23  friend class ContextBuilder;
24 
25  public:
33  AttributesBuilder(BuilderReturn& builder, std::string kind, std::string key)
34  : key_(std::move(key)), kind_(std::move(kind)), builder_(builder) {}
35 
44  AttributesBuilder(BuilderReturn& builder,
45  std::string kind,
46  Attributes const& attributes)
47  : key_(attributes.Key()),
48  kind_(std::move(kind)),
49  builder_(builder),
50  name_(attributes.Name()),
51  anonymous_(attributes.Anonymous()),
52  private_attributes_(attributes.PrivateAttributes()) {
53  for (auto& pair : attributes.CustomAttributes().AsObject()) {
54  values_[pair.first] = pair.second;
55  }
56  }
57 
62  AttributesBuilder(AttributesBuilder const& builder) = delete;
63  AttributesBuilder& operator=(AttributesBuilder const&) = delete;
64  AttributesBuilder& operator=(AttributesBuilder&&) = delete;
65 
66  // This cannot be noexcept because of:
67  // https://developercommunity.visualstudio.com/t/bug-in-stdmapstdpair-implementation-with-move-only/840554
68  AttributesBuilder(AttributesBuilder&& builder) = default;
69  ~AttributesBuilder() = default;
70 
79  AttributesBuilder& Name(std::string name);
80 
88  AttributesBuilder& Anonymous(bool anonymous);
89 
103  AttributesBuilder& Set(std::string name, launchdarkly::Value value);
104 
123 
169 
177  template <typename IterType>
178  AttributesBuilder& AddPrivateAttributes(IterType attributes) {
179  for (auto iter : attributes) {
180  private_attributes_.insert(iter);
181  }
182  return *this;
183  }
184 
197  AttributesBuilder& Kind(std::string kind, std::string key) {
198  return builder_.Kind(kind, key);
199  }
200 
208  AttributesBuilder* Kind(std::string const& kind) {
209  return builder_.Kind(kind);
210  }
211 
217  [[nodiscard]] BuildType Build() const { return builder_.Build(); }
218 
219  private:
220  BuilderReturn& builder_;
221 
227  void Key(std::string key) { key_ = std::move(key); }
228 
229  Attributes BuildAttributes() const;
230 
231  AttributesBuilder& Set(std::string name,
232  launchdarkly::Value value,
233  bool private_attribute);
234 
235  std::string kind_;
236  std::string key_;
237  std::string name_;
238  bool anonymous_ = false;
239 
240  std::map<std::string, launchdarkly::Value> values_;
241  AttributeReference::SetType private_attributes_;
242 };
243 } // namespace launchdarkly
Definition: attribute_reference.hpp:34
Definition: attributes_builder.hpp:22
AttributesBuilder(BuilderReturn &builder, std::string kind, Attributes const &attributes)
Definition: attributes_builder.hpp:44
AttributesBuilder & Set(std::string name, launchdarkly::Value value)
AttributesBuilder(BuilderReturn &builder, std::string kind, std::string key)
Definition: attributes_builder.hpp:33
AttributesBuilder & Kind(std::string kind, std::string key)
Definition: attributes_builder.hpp:197
BuildType Build() const
Definition: attributes_builder.hpp:217
AttributesBuilder & SetPrivate(std::string name, launchdarkly::Value value)
AttributesBuilder & Name(std::string name)
AttributesBuilder & AddPrivateAttributes(IterType attributes)
Definition: attributes_builder.hpp:178
AttributesBuilder * Kind(std::string const &kind)
Definition: attributes_builder.hpp:208
AttributesBuilder(AttributesBuilder const &builder)=delete
AttributesBuilder & Anonymous(bool anonymous)
AttributesBuilder & AddPrivateAttribute(AttributeReference ref)
Definition: attributes.hpp:15
Value const & CustomAttributes() const
Definition: attributes.cpp:17
Definition: context_builder.hpp:62
Definition: value.hpp:42
Object const & AsObject() const
Definition: value.cpp:121