6
6
#include < launchdarkly/events/asio_event_processor.hpp>
7
7
#include < launchdarkly/events/null_event_processor.hpp>
8
8
9
+ #include < launchdarkly/detail/c_binding_helpers.hpp>
9
10
#include < launchdarkly/encoding/sha_256.hpp>
10
11
#include < launchdarkly/logging/console_backend.hpp>
11
12
#include < launchdarkly/logging/null_logger.hpp>
@@ -30,14 +31,14 @@ using launchdarkly::client_side::data_sources::DataSourceStatus;
30
31
using launchdarkly::config::shared::built::DataSourceConfig;
31
32
using launchdarkly::config::shared::built::HttpProperties;
32
33
33
- static std::shared_ptr<::launchdarkly:: data_sources::IDataSource>
34
- MakeDataSource ( HttpProperties const & http_properties,
35
- Config const & config,
36
- Context const & context,
37
- boost::asio::any_io_executor const & executor,
38
- IDataSourceUpdateSink& flag_updater,
39
- data_sources::DataSourceStatusManager& status_manager,
40
- Logger& logger) {
34
+ static std::shared_ptr<data_sources::IDataSource> MakeDataSource (
35
+ HttpProperties const & http_properties,
36
+ Config const & config,
37
+ Context const & context,
38
+ boost::asio::any_io_executor const & executor,
39
+ IDataSourceUpdateSink& flag_updater,
40
+ data_sources::DataSourceStatusManager& status_manager,
41
+ Logger& logger) {
41
42
if (config.Offline ()) {
42
43
return std::make_shared<data_sources::NullDataSource>(executor,
43
44
status_manager);
@@ -80,39 +81,39 @@ static std::shared_ptr<IPersistence> MakePersistence(Config const& config) {
80
81
return persistence.implementation ;
81
82
}
82
83
83
- ClientImpl::ClientImpl (Config config ,
84
+ ClientImpl::ClientImpl (Config in_cfg ,
84
85
Context context,
85
86
std::string const & version)
86
- : config_(config),
87
+ : config_(std::move(in_cfg)), /* caution: do not use in_cfg (moved from!) */
87
88
http_properties_(
88
- HttpPropertiesBuilder (config .HttpProperties())
89
+ HttpPropertiesBuilder (config_ .HttpProperties())
89
90
.Header(" user-agent" , " CPPClient/" + version)
90
- .Header(" authorization" , config .SdkKey())
91
- .Header(" x-launchdarkly-tags" , config .ApplicationTag())
91
+ .Header(" authorization" , config_ .SdkKey())
92
+ .Header(" x-launchdarkly-tags" , config_ .ApplicationTag())
92
93
.Build()),
93
- logger_(MakeLogger(config .Logging())),
94
+ logger_(MakeLogger(config_ .Logging())),
94
95
ioc_(kAsioConcurrencyHint ),
95
96
work_(boost::asio::make_work_guard(ioc_)),
96
97
context_(std::move(context)),
97
- flag_manager_(config .SdkKey(),
98
+ flag_manager_(config_ .SdkKey(),
98
99
logger_,
99
- config .Persistence().max_contexts_,
100
- MakePersistence(config )),
100
+ config_ .Persistence().max_contexts_,
101
+ MakePersistence(config_ )),
101
102
data_source_factory_([this ]() {
102
103
return MakeDataSource (http_properties_, config_, context_,
103
104
ioc_.get_executor (), flag_manager_.Updater (),
104
105
status_manager_, logger_);
105
106
}),
106
107
data_source_(nullptr ),
107
108
event_processor_(nullptr ),
108
- eval_reasons_available_(config .DataSourceConfig().with_reasons) {
109
+ eval_reasons_available_(config_ .DataSourceConfig().with_reasons) {
109
110
flag_manager_.LoadCache (context_);
110
111
111
- if (config .Events ().Enabled () && !config .Offline ()) {
112
+ if (config_ .Events ().Enabled () && !config_ .Offline ()) {
112
113
event_processor_ =
113
114
std::make_unique<events::AsioEventProcessor<ClientSDK>>(
114
- ioc_.get_executor (), config .ServiceEndpoints (), config. Events (),
115
- http_properties_, logger_);
115
+ ioc_.get_executor (), config_ .ServiceEndpoints (),
116
+ config_. Events (), http_properties_, logger_);
116
117
} else {
117
118
event_processor_ = std::make_unique<events::NullEventProcessor>();
118
119
}
@@ -160,14 +161,14 @@ void ClientImpl::RestartDataSource() {
160
161
161
162
std::future<bool > ClientImpl::StartAsyncInternal (
162
163
std::function<bool (DataSourceStatus::DataSourceState)> result_predicate) {
163
- auto pr = std::make_shared<std::promise<bool >>();
164
- auto fut = pr ->get_future ();
164
+ auto init_promise = std::make_shared<std::promise<bool >>();
165
+ auto init_future = init_promise ->get_future ();
165
166
166
167
status_manager_.OnDataSourceStatusChangeEx (
167
- [result_predicate, pr](data_sources::DataSourceStatus status) {
168
- auto state = status. State ();
169
- if (IsInitialized (state)) {
170
- pr ->set_value (result_predicate (status.State ()));
168
+ [result = std::move (result_predicate),
169
+ init_promise](data_sources::DataSourceStatus const & status) {
170
+ if (auto const state = status. State (); IsInitialized (state)) {
171
+ init_promise ->set_value (result (status.State ()));
171
172
return true ; /* delete this change listener since the desired
172
173
state was reached */
173
174
}
@@ -176,7 +177,7 @@ std::future<bool> ClientImpl::StartAsyncInternal(
176
177
177
178
RestartDataSource ();
178
179
179
- return fut ;
180
+ return init_future ;
180
181
}
181
182
182
183
std::future<bool > ClientImpl::StartAsync () {
@@ -272,14 +273,15 @@ EvaluationDetail<T> ClientImpl::VariationInternal(FlagKey const& key,
272
273
event_processor_->SendAsync (std::move (event));
273
274
return EvaluationDetail<T>(default_value, std::nullopt,
274
275
std::move (error_reason));
276
+ }
275
277
276
- } else if (!Initialized ()) {
278
+ if (!Initialized ()) {
277
279
LD_LOG (logger_, LogLevel::kInfo )
278
280
<< " LaunchDarkly client has not yet been initialized. "
279
281
" Returning cached value" ;
280
282
}
281
283
282
- assert (desc->item );
284
+ LD_ASSERT (desc->item );
283
285
284
286
auto const & flag = *(desc->item );
285
287
auto const & detail = flag.Detail ();
0 commit comments