Skip to content

Commit

Permalink
Clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET committed Apr 9, 2024
1 parent 5006618 commit 1b46062
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 76 deletions.
173 changes: 98 additions & 75 deletions include/sparrow/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,84 +17,107 @@
#include <cassert>
#include <memory>

namespace sparrow {

/**
* @brief A value_ptr is a smart pointer that behaves like a value.
* It manages the lifetime of an object of type T which is not stored in the `value_ptr` but a pointer, similar to `unique_ptr`.
* When copied, it copies the managed object.
*
* @tparam T The type of the object managed by the `value_ptr`.
* @todo Make it constexpr.
*/
template <class T>
class value_ptr {
public:
value_ptr() = default;

explicit value_ptr(T value)
: value_(std::make_unique<T>(std::move(value))) {}

explicit value_ptr(T* value)
: value_(value != nullptr ? std::make_unique<T>(*value) : std::unique_ptr<T>()) {}

value_ptr(const value_ptr& other)
: value_(other.value_ ? std::make_unique<T>(*other.value_) : std::unique_ptr<T>()){}

value_ptr(value_ptr&& other) noexcept = default;

~value_ptr() = default;

value_ptr& operator=(const value_ptr& other) {
if (other.has_value()) {
if(value_) {
*value_ = *other.value_;
namespace sparrow
{

/**
* @brief A value_ptr is a smart pointer that behaves like a value.
* It manages the lifetime of an object of type T which is not stored in the `value_ptr` but a pointer,
* similar to `unique_ptr`. When copied, it copies the managed object.
*
* @tparam T The type of the object managed by the `value_ptr`.
* @todo Make it constexpr.
*/
template <class T>
class value_ptr
{
public:

value_ptr() = default;

explicit value_ptr(T value)
: value_(std::make_unique<T>(std::move(value)))
{
}

explicit value_ptr(T* value)
: value_(value != nullptr ? std::make_unique<T>(*value) : std::unique_ptr<T>())
{
}

value_ptr(const value_ptr& other)
: value_(other.value_ ? std::make_unique<T>(*other.value_) : std::unique_ptr<T>())
{
}

value_ptr(value_ptr&& other) noexcept = default;

~value_ptr() = default;

value_ptr& operator=(const value_ptr& other)
{
if (other.has_value())
{
if (value_)
{
*value_ = *other.value_;
}
else
{
value_ = std::make_unique<T>(*other.value_);
}
}
else {
value_ = std::make_unique<T>(*other.value_);
else
{
value_.reset();
}
}else {
return *this;
}

value_ptr& operator=(value_ptr&& other) noexcept = default;

T& operator*()
{
assert(value_);
return *value_;
}

const T& operator*() const
{
assert(value_);
return *value_;
}

T* operator->()
{
assert(value_);
return &*value_;
}

const T* operator->() const
{
assert(value_);
return &*value_;
}

explicit operator bool() const noexcept
{
return has_value();
}

bool has_value() const noexcept
{
return bool(value_);
}

void reset() noexcept
{
value_.reset();
}
return *this;
}

value_ptr& operator=(value_ptr&& other) noexcept = default;

T& operator*() {
assert(value_);
return *value_;
}

const T& operator*() const {
assert(value_);
return *value_;
}

T* operator->() {
assert(value_);
return &*value_;
}

const T* operator->() const {
assert(value_);
return &*value_;
}

explicit operator bool() const noexcept {
return has_value();
}

bool has_value() const noexcept {
return bool(value_);
}

void reset() noexcept {
value_.reset();
}

private:
std::unique_ptr<T> value_;
};

private:

std::unique_ptr<T> value_;
};

}
3 changes: 2 additions & 1 deletion test/test_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <vector>

#include "sparrow/memory.hpp"

#include "doctest/doctest.h"
#include <vector>

using namespace sparrow;

Expand Down

0 comments on commit 1b46062

Please sign in to comment.