started TypedCow <cow/typed.hpp>

absolute-shit
Avril 3 years ago
parent ffe01128c4
commit 382efd5d43
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -59,6 +59,8 @@ struct Cow : public _cow_util::Span<unsigned char> {
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<typename T>
struct TypedCow : private Cow, public _cow_util::Span<T> {
struct Fake : private Cow::Fake, public _cow_util::Span<T>{};
};

@ -0,0 +1,48 @@
#pragma once
#include <cow.hpp>
#include <utility>
template<typename T>
struct TypedCow : private Cow, public _cow_util::Span<T> {
struct Fake;
inline TypedCow(size_t sz) : TypedCow(sz, T()) {}
template<typename... Args>
inline TypedCow(size_t sz, Args&&... args) : TypedCow(sz, T(std::forward<Args>(args)...)) {}
inline TypedCow(TypedCow<T>&& move) : Cow(std::move(move.super)) {
unsigned char* bytes = this->as_bytes();
for(size_t i=0;i<size();i++)
new ((void*)(bytes + (sizeof(T)*i))) T(std::move(move[i]));
}
inline TypedCow(const TypedCow<T>& copy) : Cow(copy.super) {
unsigned char* bytes = this->as_bytes();
for(size_t i=0;i<size();i++)
new ((void*)(bytes + (sizeof(T)*i))) T(copy[i]);
}
inline ~TypedCow() {
unsigned char* bytes = this->as_bytes();
for(size_t i=0;i<size();i++)
reinterpret_cast<T*>(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<T*>(this->as_bytes() + (sizeof(T) * i)); }
inline const T& operator[](size_t i) const { if (i>=size()) throw "idx"; else return *reinterpret_cast<const T*>(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<sz;i++)
new ((void*)(bytes + (sizeof(T)*i))) T(copy_from);
}
};
template<typename T>
struct TypedCow<T>::Fake : private Cow::Fake, public _cow_util::Span<T> {
};

@ -8,6 +8,7 @@
#include <vector>
#include <cow/area.hpp>
#include <cow/typed.hpp>
using namespace _cow_util;

Loading…
Cancel
Save