mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Allow creation of box and ref through std::unique_ptr and std::shared_ptr respectively
This commit is contained in:
+12
-1
@@ -4,6 +4,8 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// The Box class behaves very similarly to the unique_ptr, but unlike the
|
||||
@@ -20,6 +22,15 @@ class Box {
|
||||
return Box<T>(std::make_unique<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
/// You can generate them from unique_ptrs as well, in which case it will
|
||||
/// return an Error, if the unique_ptr is not set.
|
||||
static Result<Box<T>> make(std::unique_ptr<T>&& _ptr) {
|
||||
if (!_ptr) {
|
||||
return Error("std::unique_ptr was a nullptr.");
|
||||
}
|
||||
return Box<T>(std::move(_ptr));
|
||||
}
|
||||
|
||||
Box() : ptr_(std::make_unique<T>()) {}
|
||||
|
||||
Box(const Box<T>& _other) = delete;
|
||||
@@ -77,7 +88,7 @@ class Box {
|
||||
|
||||
/// Generates a new Ref<T>.
|
||||
template <class T, class... Args>
|
||||
Box<T> make_box(Args&&... _args) {
|
||||
auto make_box(Args&&... _args) {
|
||||
return Box<T>::make(std::forward<Args>(_args)...);
|
||||
}
|
||||
|
||||
|
||||
+21
-1
@@ -4,6 +4,8 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// The Ref class behaves very similarly to the shared_ptr, but unlike the
|
||||
@@ -20,6 +22,24 @@ class Ref {
|
||||
return Ref<T>(std::make_shared<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
/// You can generate them from shared_ptrs as well, in which case it will
|
||||
/// return an Error, if the shared_ptr is not set.
|
||||
static Result<Ref<T>> make(std::shared_ptr<T>&& _ptr) {
|
||||
if (!_ptr) {
|
||||
return Error("std::shared_ptr was a nullptr.");
|
||||
}
|
||||
return Ref<T>(std::move(_ptr));
|
||||
}
|
||||
|
||||
/// You can generate them from shared_ptrs as well, in which case it will
|
||||
/// return an Error, if the shared_ptr is not set.
|
||||
static Result<Ref<T>> make(const std::shared_ptr<T>& _ptr) {
|
||||
if (!_ptr) {
|
||||
return Error("std::shared_ptr was a nullptr.");
|
||||
}
|
||||
return Ref<T>(_ptr);
|
||||
}
|
||||
|
||||
Ref() : ptr_(std::make_shared<T>()) {}
|
||||
|
||||
Ref(const Ref<T>& _other) = default;
|
||||
@@ -87,7 +107,7 @@ class Ref {
|
||||
|
||||
/// Generates a new Ref<T>.
|
||||
template <class T, class... Args>
|
||||
Ref<T> make_ref(Args&&... _args) {
|
||||
auto make_ref(Args&&... _args) {
|
||||
return Ref<T>::make(std::forward<Args>(_args)...);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user