Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl: check if options are empty #9617

Merged
merged 2 commits into from
Aug 2, 2022
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
2 changes: 2 additions & 0 deletions google/cloud/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void CheckExpectedOptionsImpl(std::set<std::type_index> const& expected,
}
}

bool IsEmpty(Options const& options) { return options.m_.empty(); }

Options MergeOptions(Options preferred, Options alternatives) {
if (preferred.m_.empty()) return alternatives;
preferred.m_.insert(std::make_move_iterator(alternatives.m_.begin()),
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace internal {
Options MergeOptions(Options, Options);
void CheckExpectedOptionsImpl(std::set<std::type_index> const&, Options const&,
char const*);
// TODO(#8800) - Remove when bigtable::Table no longer uses bigtable::DataClient
bool IsEmpty(Options const&);
template <typename T>
inline T const& DefaultValue() {
static auto const* const kDefaultValue = new T{};
Expand Down Expand Up @@ -209,6 +211,7 @@ class Options {
friend Options internal::MergeOptions(Options, Options);
friend void internal::CheckExpectedOptionsImpl(
std::set<std::type_index> const&, Options const&, char const*);
friend bool internal::IsEmpty(Options const&);
devbww marked this conversation as resolved.
Show resolved Hide resolved

// The type-erased data holder of all the option values.
class DataHolder {
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ TEST(CheckUnexpectedOptions, OptionsListOneUnexpected) {
Contains(ContainsRegex("caller: Unexpected option.+FooOption")));
}

TEST(IsEmpty, Yes) {
Options opts;
EXPECT_TRUE(internal::IsEmpty(opts));
}

TEST(IsEmpty, No) {
auto opts = Options{}.set<IntOption>(0);
EXPECT_FALSE(internal::IsEmpty(opts));
}

TEST(MergeOptions, Basics) {
auto a = Options{}.set<StringOption>("from a").set<IntOption>(42);
auto b = Options{}.set<StringOption>("from b").set<BoolOption>(true);
Expand Down