Skip to content

Commit

Permalink
Drop std::unique_ptr
Browse files Browse the repository at this point in the history
Trim down the `result` class even more by operating directly on pointers
and dropping use of `std::unique_ptr`.
  • Loading branch information
RauliL committed Feb 9, 2024
1 parent 8e9afbb commit c8896d7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.6)

PROJECT(
PeeloResult
VERSION 0.3.0
VERSION 0.4.0
DESCRIPTION "Header only C++ result type."
HOMEPAGE_URL "https://github.com/peelonet/peelo-result"
LANGUAGES CXX
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ method of `result` class. If this method returns `true`, then it's value can be
accessed with the `value` method, otherwise it's error can be accessed with
the `error` method.

**Note**: The underlying containers are implemented with [std::unique_ptr], so
accessing `value` of erroneous result leads to undefined behavior and vice
Note: Accessing `value` or erronous result leads to undefined behavior and vice
versa.

[std::unique_ptr]: https://en.cppreference.com/w/cpp/memory/unique_ptr

### Usage example

```C++
Expand Down
99 changes: 84 additions & 15 deletions include/peelo/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
*/
#pragma once

#include <memory>
#include <utility>

namespace peelo
{
template<class T, class E>
class result
class result final
{
public:
using value_type = T;
Expand All @@ -51,17 +51,53 @@ namespace peelo
: m_value(that.m_value ? new value_type(*that.m_value) : nullptr)
, m_error(that.m_error ? new error_type(*that.m_error) : nullptr) {}

result(result&& that)
: m_value(std::move(that.m_value))
, m_error(std::move(that.m_error))
{
that.m_value = nullptr;
that.m_error = nullptr;
}

template<class U, class G>
result(const result<U, G>& that)
: m_value(that ? new value_type(that.value()) : nullptr)
, m_error(that ? nullptr : new error_type(that.error())) {}

~result()
{
if (m_value)
{
delete m_value;
}
if (m_error)
{
delete m_error;
}
}

result& operator=(const result& that)
{
if (this != &that)
{
m_value.reset(that.m_value ? new value_type(*that.m_value) : nullptr);
m_error.reset(that.m_error ? new error_type(*that.m_error) : nullptr);
if (m_value)
{
delete m_value;
m_value = nullptr;
}
if (m_error)
{
delete m_error;
m_error = nullptr;
}
if (that.m_value)
{
m_value = new value_type(*that.m_value);
}
if (that.m_error)
{
m_error = new error_type(*that.m_error);
}
}

return *this;
Expand All @@ -70,10 +106,43 @@ namespace peelo
template<class U, class G>
result& operator=(const result<U, G>& that)
{
const auto has_value = that.has_value();
if (m_value)
{
delete m_value;
m_value = nullptr;
}
if (m_error)
{
delete m_error;
m_error = nullptr;
}
if (that)
{
m_value = new value_type(that.value());
} else {
m_error = new error_type(that.error());
}

return *this;
}

m_value.reset(has_value ? new value_type(that.value()) : nullptr);
m_error.reset(has_value ? nullptr : new error_type(that.error()));
result& operator=(result&& that)
{
if (this != &that)
{
if (m_value)
{
delete m_value;
}
if (m_error)
{
delete m_error;
}
m_value = std::move(that.m_value);
m_error = std::move(that.m_error);
that.m_value = nullptr;
that.m_error = nullptr;
}

return *this;
}
Expand Down Expand Up @@ -105,16 +174,16 @@ namespace peelo

inline bool equals(const result& that) const
{
if (has_value())
if (m_value)
{
if (!that.has_value())
if (!that)
{
return false;
}

return *m_value == *that.m_value;
}
else if (that.has_value())
else if (that)
{
return false;
}
Expand All @@ -135,16 +204,16 @@ namespace peelo
template<class U, class G>
inline bool equals(const result<U, G>& that) const
{
if (has_value())
if (m_value)
{
if (!that.has_value())
if (!that)
{
return false;
}

return *m_value == that.value();
}
else if (that.has_value())
else if (that)
{
return false;
}
Expand All @@ -170,7 +239,7 @@ namespace peelo
, m_error(error) {}

private:
std::unique_ptr<value_type> m_value;
std::unique_ptr<error_type> m_error;
value_type* m_value;
error_type* m_error;
};
}
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FOREACH(TEST_FILENAME ${TEST_SOURCES})
TARGET_COMPILE_FEATURES(
${TEST_NAME}
PUBLIC
cxx_std_17
cxx_std_11
)

IF(MSVC)
Expand Down

0 comments on commit c8896d7

Please sign in to comment.