Google Cloud C++ Client  1.42.0
C++ Client Library for Google Cloud Platform
Classes | Public Member Functions | List of all members
google::cloud::Options Class Reference

A class that holds option structs indexed by their type. More...

#include <google/cloud/options.h>

Public Member Functions

 Options ()=default
 Constructs an empty instance. More...
 
 Options (Options const &rhs)
 
Optionsoperator= (Options const &rhs)
 
 Options (Options &&)=default
 
Optionsoperator= (Options &&)=default
 
template<typename T >
Optionsset (ValueTypeT< T > v)
 Sets option T to the value v and returns a reference to *this. More...
 
template<typename T >
bool has () const
 Returns true IFF an option with type T exists. More...
 
template<typename T >
void unset ()
 Erases the option specified by the type T. More...
 
template<typename T >
ValueTypeT< T > const & get () const
 Returns a reference to the value for T, or a value-initialized default if T was not set. More...
 
template<typename T >
ValueTypeT< T > & lookup (ValueTypeT< T > value={})
 Returns a reference to the value for option T, setting the value to init_value if necessary. More...
 

Detailed Description

A class that holds option structs indexed by their type.

An "Option" is any struct that has a public Type 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 single Options 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.

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"});

Definition at line 87 of file options.h.

Constructor & Destructor Documentation

◆ Options() [1/3]

google::cloud::Options::Options ( )
default

Constructs an empty instance.

◆ Options() [2/3]

google::cloud::Options::Options ( Options const &  rhs)
inline

Definition at line 96 of file options.h.

◆ Options() [3/3]

google::cloud::Options::Options ( Options &&  )
default

Member Function Documentation

◆ get()

template<typename T >
ValueTypeT<T> const& google::cloud::Options::get ( ) const
inline

Returns a reference to the value for T, or a value-initialized default if T was not set.

This method will always return a reference to a valid value of the correct type for option T, whether or not T has actually been set. Use has<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);
Options()=default
Constructs an empty instance.
Template Parameters
Tthe option type

Definition at line 170 of file options.h.

◆ has()

template<typename T >
bool google::cloud::Options::has ( ) const
inline

Returns true IFF an option with type T exists.

Template Parameters
Tthe option type

Definition at line 132 of file options.h.

◆ lookup()

template<typename T >
ValueTypeT<T>& google::cloud::Options::lookup ( ValueTypeT< T >  value = {})
inline

Returns a reference to the value for option T, setting the value to init_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);
Template Parameters
Tthe option type
Parameters
valuethe initial value to use if T is not set (optional)

Definition at line 198 of file options.h.

◆ operator=() [1/2]

Options& google::cloud::Options::operator= ( Options &&  )
default

◆ operator=() [2/2]

Options& google::cloud::Options::operator= ( Options const &  rhs)
inline

Definition at line 99 of file options.h.

◆ set()

template<typename T >
Options& google::cloud::Options::set ( ValueTypeT< T >  v)
inline

Sets option T to the value v and returns a reference to *this.

struct FooOption {
using Type = int;
};
auto opts = Options{}.set<FooOption>(123);
Template Parameters
Tthe option type
Parameters
vthe value to set the option T

Definition at line 121 of file options.h.

◆ unset()

template<typename T >
void google::cloud::Options::unset ( )
inline

Erases the option specified by the type T.

Template Parameters
Tthe option type

Definition at line 142 of file options.h.