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

feat(common): add Options::set<>() && overload #12424

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix build problems
coryan committed Aug 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e1ec3d90cbad15c20cdf9767457eecd962235cc2
34 changes: 29 additions & 5 deletions google/cloud/options.h
Original file line number Diff line number Diff line change
@@ -108,8 +108,18 @@ class Options {
std::swap(m_, tmp.m_);
return *this;
}
Options(Options&&) = default;
Options& operator=(Options&&) = default;
Options(Options&& rhs) noexcept = default;

// Older versions of GCC (or maybe libstdc++) crash with the default move
// assignment:
// https://godbolt.org/z/j4EKjdrv4
// I suspect this is the problem addressed by:
// https://github.com/gcc-mirror/gcc/commit/c2fb0a1a2e7a0fb15cf3cf876f621902ccd273f0
Options& operator=(Options&& rhs) noexcept {
Options tmp(std::move(rhs));
std::swap(m_, tmp.m_);
return *this;
}

/**
* Sets option `T` to the value @p v and returns a reference to `*this`.
@@ -118,7 +128,8 @@ class Options {
* struct FooOption {
* using Type = int;
* };
* auto opts = Options{}.set<FooOption>(123);
* auto opts = Options{};
* opts.set<FooOption>(123);
* @endcode
*
* @tparam T the option type
@@ -130,10 +141,23 @@ class Options {
return *this;
}

/// @copydoc set(ValueTypeT<T>) &
/**
* Sets option `T` to the value @p v and returns a reference to `*this`.
*
* @code
* struct FooOption {
* using Type = int;
* };
* auto opts = Options{}.set<FooOption>(123);
* @endcode
*
* @tparam T the option type
* @param v the value to set the option T
*/
template <typename T>
Options&& set(ValueTypeT<T> v) && {
return std::move(set<T>(std::move(v)));
set<T>(std::move(v));
return std::move(*this);
}

/**