C++ Server-Side SDK
LaunchDarkly SDK
Public Member Functions | List of all members
launchdarkly::ContextBuilder Class Referencefinal

#include <context_builder.hpp>

Public Member Functions

 ContextBuilder (Context const &context)
 
AttributesBuilder< ContextBuilder, Context > & Kind (std::string const &kind, std::string key)
 
AttributesBuilder< ContextBuilder, Context > * Kind (std::string const &kind)
 
Context Build () const
 

Detailed Description

Class for building LaunchDarkly contexts.

You cannot build a context until you have added at least one kind.

Building a context with a single kind.

auto context = ContextBuilder()
.Kind("user", "bobby-bobberson")
.Name("Bob")
.Anonymous(false)
// Set a custom attribute.
.Set("likesCats", true)
// Set a private custom attribute.
.SetPrivate("email", "email@email.email")
.Build();

Building a context with multiple Kinds.

auto context = ContextBuilder()
.Kind("user", "bobby-bobberson")
.Name("Bob")
.Anonymous(false)
// Set a custom attribute.
.Set("likesCats", true)
// Set a private custom attribute.
.SetPrivate("email", "email@email.email")
// Add another kind to the context.
.Kind("org", "org-key")
.Anonymous(true)
.Set("goal", "money")
.Build();

Using the builder with loops.

auto builder = ContextBuilder();
// The data in this sample is not realistic, but it is intended to show
// how to use the builder with loops.
for (auto const& kind : Kinds) { // Some collection we are using to make
Kinds.
// The `kind` method returns a reference, always store it in a reference.
auto& kind_builder = builder.Kind(kind, kind + "-key");
for (auto const& prop : props) { // A collection of props we want to add.
kind_builder.Set(prop.first, prop.second);
}
}
auto context = builder.Build();

Constructor & Destructor Documentation

◆ ContextBuilder()

launchdarkly::ContextBuilder::ContextBuilder ( Context const &  context)

Create a new context builder from the given context. The created builder will have all the kinds and attributes of the original context.

If the original context is not valid, then this builder will be created in a default state.

Parameters
contextThe context to base the builder on.

Member Function Documentation

◆ Build()

Context launchdarkly::ContextBuilder::Build ( ) const

Build a context. The same builder instance may be used to build multiple contexts.

You MUST add at least one kind before building a context. Not doing so will result in an invalid context.

Returns
The built context.

◆ Kind() [1/2]

AttributesBuilder< ContextBuilder, Context > * launchdarkly::ContextBuilder::Kind ( std::string const &  kind)

Start updating an existing kind.

Parameters
kindThe kind to start updating.
Returns
A builder which allows adding attributes for the kind, or nullptr if the kind doesn't already exist.

◆ Kind() [2/2]

AttributesBuilder< ContextBuilder, Context > & launchdarkly::ContextBuilder::Kind ( std::string const &  kind,
std::string  key 
)

Start adding a kind to the context.

If you call this function multiple times with the same kind, then the same builder will be returned each time. If you previously called the function with the same kind, but different key, then the key will be updated.

Parameters
kindThe kind being added.
keyThe key for the kind.
Returns
A builder which allows adding attributes for the kind.

The documentation for this class was generated from the following files: