platform_build/tools/aconfig/templates/cpp_test_flag_provider.template
Dennis Shen 8d544f7439 cpp codegen redesign, unit test support
cpp codegen iteration 2, based on discussions with three internal teams
that use c++. Refer to the design doc "aconfig c++ codegen" for detailed
design. At a high level, we generate two sets of code artifacts with the
same signatured api: one for production that without any local flag
override capability, one for unit test that allows local flag overrides.
It supports static methods style interface as well as injection pattern.

Refer to the test points in the codegen_cpp.rs for examples of generated
code.

for production target codegen: aconfig create-cpp-lib --cache <cache> --out <out dir>
for test target codegen: aconfig create-cpp-lib --cache <cache> --out
<out dir> --mode test

Bug: b/279483801
Test: atest aconfig.test
Change-Id: I92fefb9623d5435525339a74f57bbd36d0afef08
2023-07-05 14:05:44 +00:00

49 lines
1.4 KiB
Text

#ifndef {header}_flag_provider_HEADER_H
#define {header}_flag_provider_HEADER_H
#include "{header}.h"
#include <unordered_map>
#include <unordered_set>
#include <cassert>
namespace {cpp_namespace} \{
class flag_provider : public flag_provider_interface \{
private:
std::unordered_map<std::string, bool> overrides_;
std::unordered_set<std::string> flag_names_;
public:
flag_provider()
: overrides_(),
flag_names_() \{
{{ for item in class_elements}}
flag_names_.insert({item.uppercase_flag_name});{{ endfor }}
}
{{ for item in class_elements}}
virtual bool {item.flag_name}() override \{
auto it = overrides_.find({item.uppercase_flag_name});
if (it != overrides_.end()) \{
return it->second;
} else \{
{{ if item.readwrite- }}
return GetServerConfigurableFlag(
"{item.device_config_namespace}",
"{item.device_config_flag}",
"{item.default_value}") == "true";
{{ -else- }}
return {item.default_value};
{{ -endif }}
}
}
{{ endfor }}
virtual void override_flag(std::string const& flag, bool val) override \{
assert(flag_names_.count(flag));
overrides_[flag] = val;
}
virtual void reset_overrides() override \{
overrides_.clear();
}
};
}
#endif