Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(hello-c-client)
add_subdirectory(hello-cpp-client)
add_subdirectory(hello-cpp-server)
6 changes: 3 additions & 3 deletions examples/hello-c-client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ cmake_minimum_required(VERSION 3.19)
project(
LaunchDarklyHelloCClient
VERSION 0.1
DESCRIPTION "LaunchDarkly Hello C Client"
DESCRIPTION "LaunchDarkly Hello C Client-side SDK"
LANGUAGES C
)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(hello-c main.c)
target_link_libraries(hello-c PRIVATE launchdarkly::client launchdarkly::sse launchdarkly::common Threads::Threads)
add_executable(hello-c-client main.c)
target_link_libraries(hello-c-client PRIVATE launchdarkly::client launchdarkly::sse launchdarkly::common Threads::Threads)
6 changes: 3 additions & 3 deletions examples/hello-cpp-client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ cmake_minimum_required(VERSION 3.19)
project(
LaunchDarklyHelloCPPClient
VERSION 0.1
DESCRIPTION "LaunchDarkly Hello CPP Client"
DESCRIPTION "LaunchDarkly Hello CPP Client-side SDK"
LANGUAGES CXX
)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(hello-cpp main.cpp)
target_link_libraries(hello-cpp PRIVATE launchdarkly::client Threads::Threads)
add_executable(hello-cpp-client main.cpp)
target_link_libraries(hello-cpp-client PRIVATE launchdarkly::client Threads::Threads)
4 changes: 2 additions & 2 deletions examples/hello-cpp-client/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <launchdarkly/client_side/client.hpp>
#include <launchdarkly/context_builder.hpp>

#include <iostream>
#include <cstring>
#include <iostream>

// Set MOBILE_KEY to your LaunchDarkly mobile key.
#define MOBILE_KEY ""
Expand All @@ -18,7 +18,7 @@ using namespace launchdarkly;
int main() {
if (!strlen(MOBILE_KEY)) {
printf(
"*** Please edit main.c to set MOBILE_KEY to your LaunchDarkly "
"*** Please edit main.cpp to set MOBILE_KEY to your LaunchDarkly "
"mobile key first\n\n");
return 1;
}
Expand Down
15 changes: 15 additions & 0 deletions examples/hello-cpp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Required for Apple Silicon support.
cmake_minimum_required(VERSION 3.19)

project(
LaunchDarklyHelloCPPServer
VERSION 0.1
DESCRIPTION "LaunchDarkly Hello CPP Server-side SDK"
LANGUAGES CXX
)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(hello-cpp-server main.cpp)
target_link_libraries(hello-cpp-server PRIVATE launchdarkly::server Threads::Threads)
59 changes: 59 additions & 0 deletions examples/hello-cpp-server/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <launchdarkly/context_builder.hpp>
#include <launchdarkly/server_side/client.hpp>

#include <cstring>
#include <iostream>

// Set MOBILE_KEY to your LaunchDarkly mobile key.
#define MOBILE_KEY ""

// Set FEATURE_FLAG_KEY to the feature flag key you want to evaluate.
#define FEATURE_FLAG_KEY "my-boolean-flag"

// Set INIT_TIMEOUT_MILLISECONDS to the amount of time you will wait for
// the client to become initialized.
#define INIT_TIMEOUT_MILLISECONDS 3000

using namespace launchdarkly;
int main() {
if (!strlen(MOBILE_KEY)) {
printf(
"*** Please edit main.cpp to set MOBILE_KEY to your LaunchDarkly "
"mobile key first\n\n");
return 1;
}

auto config = server_side::ConfigBuilder(MOBILE_KEY).Build();
if (!config) {
std::cout << "error: config is invalid: " << config.error() << '\n';
return 1;
}

auto client = server_side::Client(std::move(*config));

auto start_result = client.StartAsync();
auto status = start_result.wait_for(
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
if (status == std::future_status::ready) {
if (start_result.get()) {
std::cout << "*** SDK successfully initialized!\n\n";
} else {
std::cout << "*** SDK failed to initialize\n";
return 1;
}
} else {
std::cout << "*** SDK initialization didn't complete in "
<< INIT_TIMEOUT_MILLISECONDS << "ms\n";
return 1;
}

auto context =
ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();

bool flag_value = client.BoolVariation(context, FEATURE_FLAG_KEY, false);

std::cout << "*** Feature flag '" << FEATURE_FLAG_KEY << "' is "
<< (flag_value ? "true" : "false") << " for this user\n\n";

return 0;
}