Class Options (2.15.1)

A class that holds option structs indexed by their type.

An "Option" is any struct that has a publicType member typedef. By convention they are named like "FooOption". Each library (e.g., spanner, storage) may define their own set of options. Additionally, there are common options defined that many libraries may use. All these options may be set in a singleOptions instance, and each library will look at the options that it needs.

Here's an overview of this class's interface, but see the method documentation below for details.

  • .set<T>(x)– Sets the optionT to valuex
  • .has<T>()– Returns true iff optionT is set
  • .unset<T>()– Removes the optionT
  • .get<T>()– Gets a const-ref to the value of optionT
  • .lookup<T>(x)– Gets a non-const-ref to optionT's value, initializing it tox if it was not set (x is optional).
Example:
struct FooOption {  using Type = int;};struct BarOption {  using Type = std::set<std::string>;};...Options opts;assert(opts.get<FooOption>() == 0);opts.set<FooOption>(42);assert(opts.get<FooOption>() == 42);// Inserts two elements directly into the BarOption's std::set.opts.lookup<BarOption>().insert("hello");opts.lookup<BarOption>().insert("world");std::set<std::string> const& bar = opts.get<BarOption>();assert(bar == std::set<std::string>{"hello", "world"});

Constructors

Options()

Constructs an empty instance.

Options(Options const &)

Parameter
NameDescription
rhsOptions const &

Options(Options &&)

Parameter
NameDescription
rhsOptions &&

Operators

operator=(Options const &)

Parameter
NameDescription
rhsOptions const &
Returns
TypeDescription
Options &

operator=(Options &&)

Parameter
NameDescription
rhsOptions &&
Returns
TypeDescription
Options &

Functions

set(ValueTypeT< T >) &

Sets optionT to the valuev and returns a reference to*this.

struct FooOption {  using Type = int;};auto opts = Options{};opts.set<FooOption>(123);
Parameters
NameDescription
vValueTypeT< T >

the value to set the option T

typename T

the option type

Returns
TypeDescription
Options &

set(ValueTypeT< T >) &&

Sets optionT to the valuev and returns a reference to*this.

struct FooOption {  using Type = int;};auto opts = Options{}.set<FooOption>(123);
Parameters
NameDescription
vValueTypeT< T >

the value to set the option T

typename T

the option type

Returns
TypeDescription
Options &&

has() const

Returns true IFF an option with typeT exists.

Parameter
NameDescription
typename T

the option type

Returns
TypeDescription
bool

unset()

Erases the option specified by the typeT.

Parameter
NameDescription
typename T

the option type

Returns
TypeDescription
void

get() const

Returns a reference to the value forT, or a value-initialized default ifT was not set.

This method will always return a reference to a valid value of the correct type for optionT, whether or notT has actually been set. Usehas<T>() to check whether or not the option has been set.

struct FooOption {  using Type = std::set<std::string>;};Options opts;std::set<std::string> const& x = opts.get<FooOption>();assert(x.empty());assert(!x.has<FooOption>());opts.set<FooOption>({"foo"});assert(opts.get<FooOption>().size() == 1);
Parameter
NameDescription
typename T

the option type

Returns
TypeDescription
ValueTypeT< T > const &

lookup(ValueTypeT< T >)

Returns a reference to the value for optionT, setting the value toinit_value if necessary.

struct BigOption {  using Type = std::set<std::string>;};Options opts;std::set<std::string>& x = opts.lookup<BigOption>();assert(x.empty());x.insert("foo");opts.lookup<BigOption>().insert("bar");assert(x.size() == 2);
Parameters
NameDescription
valueValueTypeT< T >

the initial value to use ifT is not set (optional)

typename T

the option type

Returns
TypeDescription
ValueTypeT< T > &

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.