Skip to content

Commit

Permalink
impl: check if options are empty (#9617)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolduc authored Aug 2, 2022
1 parent 5d18a03 commit 919c183
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
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&);

// 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

0 comments on commit 919c183

Please sign in to comment.