diff --git a/include/cow.hpp b/include/cow.hpp index b11dcb1..80e3c0e 100644 --- a/include/cow.hpp +++ b/include/cow.hpp @@ -59,6 +59,8 @@ struct Cow : public _cow_util::Span { protected: Cow(const Cow& c); + explicit inline Cow(std::shared_ptr<_inner>&& super) : super(std::move(super)){} + const std::shared_ptr<_inner> super; virtual cow_t* get_raw() const; @@ -84,9 +86,3 @@ struct Cow::Fake : public Cow { cow_t* const fake; }; -//TODO -template -struct TypedCow : private Cow, public _cow_util::Span { - struct Fake : private Cow::Fake, public _cow_util::Span{}; -}; - diff --git a/include/cow/typed.hpp b/include/cow/typed.hpp new file mode 100644 index 0000000..34f9572 --- /dev/null +++ b/include/cow/typed.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +template +struct TypedCow : private Cow, public _cow_util::Span { + struct Fake; + + inline TypedCow(size_t sz) : TypedCow(sz, T()) {} + template + inline TypedCow(size_t sz, Args&&... args) : TypedCow(sz, T(std::forward(args)...)) {} + + inline TypedCow(TypedCow&& move) : Cow(std::move(move.super)) { + unsigned char* bytes = this->as_bytes(); + for(size_t i=0;i& copy) : Cow(copy.super) { + unsigned char* bytes = this->as_bytes(); + for(size_t i=0;ias_bytes(); + for(size_t i=0;i(bytes + (sizeof(T)*i))->~T(); + } + + void* area() { return (*this).Cow::area(); } + const void* area() const { return (*this).Cow::area(); } + inline size_t size() const override { return (*this).Cow::size() / sizeof(T); } + + inline T& operator[](size_t i) { if (i>=size()) throw "idx"; else return *reinterpret_cast(this->as_bytes() + (sizeof(T) * i)); } + inline const T& operator[](size_t i) const { if (i>=size()) throw "idx"; else return *reinterpret_cast(this->as_bytes() + (sizeof(T) * i)); } + + private: + inline explicit TypedCow(size_t sz, const T& copy_from) : Cow(sz * sizeof(T)) { + unsigned char* bytes = this->as_bytes(); + for(size_t i=0;i +struct TypedCow::Fake : private Cow::Fake, public _cow_util::Span { + +}; diff --git a/src/test/main.cpp b/src/test/main.cpp index ee1420f..0dc4850 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -8,6 +8,7 @@ #include #include +#include using namespace _cow_util;