summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2018-10-17 13:15:17 -0700
committerWilliam A. Kennington III <wak@google.com>2018-10-17 13:17:20 -0700
commit1a3b2c68ac0f409f501d4dedde7a0fc90e9baa90 (patch)
tree22efa35a9bbb1cf31778cf3c1777c60a6ec69338
parentad7e686b5563a146728aa9477f6e814dc0a3cbd4 (diff)
downloadsdbusplus-1a3b2c68ac0f409f501d4dedde7a0fc90e9baa90.tar.gz
sdbusplus-1a3b2c68ac0f409f501d4dedde7a0fc90e9baa90.zip
mapbox/variant: Implement std::get_if interface
Some of the code in the openbmc project uses their own ad-hoc get_if style interface. We need to provide one on the mapbox side to ease the transition. Tested: Built code against the get_if implementation to make sure it conforms to the same behavior as std::get_if. Change-Id: I8560a2d13110ab7fd82a21835a5e233682003fa8 Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r--mapbox/variant.hpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/mapbox/variant.hpp b/mapbox/variant.hpp
index 7c02032..ea980b3 100644
--- a/mapbox/variant.hpp
+++ b/mapbox/variant.hpp
@@ -1113,6 +1113,16 @@ ResultType& get_unchecked(T& var)
return var.template get_unchecked<ResultType>();
}
+template <typename ResultType, typename... Types>
+std::add_pointer_t<ResultType> get_if(variant<Types...>* v)
+{
+ if (v == nullptr || !v->template is<ResultType>())
+ {
+ return nullptr;
+ }
+ return &v->template get_unchecked<ResultType>();
+}
+
#ifdef HAS_EXCEPTIONS
template <typename ResultType, typename T>
auto get(T const& var) -> decltype(var.template get<ResultType>())
@@ -1126,6 +1136,17 @@ ResultType const& get_unchecked(T const& var)
{
return var.template get_unchecked<ResultType>();
}
+
+template <typename ResultType, typename... Types>
+std::add_pointer_t<const ResultType> get_if(const variant<Types...>* v)
+{
+ if (v == nullptr || !v->template is<ResultType>())
+ {
+ return nullptr;
+ }
+ return &v->template get_unchecked<ResultType>();
+}
+
} // namespace util
} // namespace mapbox
OpenPOWER on IntegriCloud