Response<T> stores Nullable<T> (#654)

* Response<T> stores Nullable<T>

Drive-by: Replace `Nullable(const T&)` with `Nullable(T)` to avoid extra copy when initialized with an rvalue.

* Update sdk/core/azure-core/inc/azure/core/response.hpp

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

Co-authored-by: Rick Winter <rick.winter@microsoft.com>
This commit is contained in:
Casey Carter 2020-09-29 10:01:00 -07:00 committed by GitHub
parent fbe95f36c5
commit 21d4e4c5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 9 deletions

View File

@ -47,7 +47,8 @@ namespace Azure { namespace Core {
*
* @param initialValue A non-absent value to initialize with.
*/
constexpr Nullable(const T& initialValue) : m_value(initialValue), m_hasValue(true) {}
constexpr Nullable(T initialValue) noexcept(std::is_nothrow_move_constructible<T>::value)
: m_value(std::move(initialValue)), m_hasValue(true) {}
/// Copy constructor.
Nullable(const Nullable& other) noexcept(std::is_nothrow_copy_constructible<T>::value)
@ -171,7 +172,7 @@ namespace Azure { namespace Core {
std::is_scalar<U>::value
&& std::is_same<T, typename std::decay<U>::type>::value) // Avoid repeated
// assignment of
// equivallent scaler
// equivalent scalar
// types
&& std::is_constructible<T, U>::value // Ensure the type is constructible
&& std::is_assignable<T&, U>::value, // Ensure the type is assignable

View File

@ -9,6 +9,9 @@
#pragma once
#include "azure/core/http/http.hpp"
#include "azure/core/nullable.hpp"
#include <memory> // for unique_ptr
#include <utility> // for move
namespace Azure { namespace Core {
/**
@ -17,7 +20,7 @@ namespace Azure { namespace Core {
* @tparam T A specific type of value to get from the raw HTTP response type.
*/
template <class T> class Response {
T m_value;
Nullable<T> m_value;
std::unique_ptr<Http::RawResponse> m_rawResponse;
public:
@ -33,36 +36,65 @@ namespace Azure { namespace Core {
{
}
/**
* @brief Initialize a #Response<T> with an absent value.
*
* @param rawResponse Raw HTTP response.
*/
explicit Response(std::unique_ptr<Http::RawResponse>&& rawResponse)
: m_rawResponse(std::move(rawResponse))
{
}
/**
* @brief Get raw HTTP response.
*/
// Do not give up raw response ownership.
Http::RawResponse& GetRawResponse() { return *this->m_rawResponse; }
/**
* @brief Check whether a value is contained.
*
* @return `true` If a value is contained, `false` if value is absent.
*/
bool HasValue() const noexcept {
return this->m_value.HasValue();
}
/**
* @brief Get a pointer to a value of a specific type.
*/
const T* operator->() const { return &this->m_value; };
const T* operator->() const {
return &this->m_value.GetValue(); // GetValue ensures there is a contained value
}
/**
* @brief Get a tpointer to a value of a specific type.
* @brief Get a pointer to a value of a specific type.
*/
T* operator->() { return &this->m_value; };
T* operator->() {
return &this->m_value.GetValue();
}
/**
* @brief Get value of a specific type.
*/
T& operator*() { return this->m_value; };
T& operator*() {
return this->m_value.GetValue();
}
/**
* @brief Get value of a specific type.
*/
const T& operator*() const { return this->m_value; };
const T& operator*() const {
return this->m_value.GetValue();
}
/**
* @brief Get an rvalue reference to the value of a specific type.
*/
T&& ExtractValue() { return std::move(this->m_value); }
T&& ExtractValue() {
return std::move(this->m_value).GetValue();
}
/**
* @brief Get a smaprt pointer rvalue reference to the value of a specific type.