better Area controls

made integer constructors explicit
cpp
Avril 4 years ago
parent d1c3d67326
commit fe7740100a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -10,7 +10,7 @@ struct Cow : public _cow_util::Span<unsigned char> {
struct Fake; struct Fake;
Cow() = delete; Cow() = delete;
Cow(size_t size); explicit Cow(size_t size);
Cow(Cow&& m); Cow(Cow&& m);
virtual ~Cow(); virtual ~Cow();
@ -34,6 +34,8 @@ struct Cow : public _cow_util::Span<unsigned char> {
static Cow from_raw(cow_t* owned); static Cow from_raw(cow_t* owned);
virtual cow_t* raw() const;
private: private:
struct _inner; struct _inner;
Cow(cow_t* raw); Cow(cow_t* raw);
@ -56,6 +58,8 @@ struct Cow::Fake : public Cow {
static Fake from_real(const Cow& real); static Fake from_real(const Cow& real);
inline cow_t* raw() const override { return fake; }
protected: protected:
cow_t* get_raw() const override; cow_t* get_raw() const override;
private: private:

@ -7,10 +7,15 @@
#include <cow.hpp> #include <cow.hpp>
struct Area { struct Area {
Area(size_t sz); Area() = delete;
explicit Area(size_t sz);
Area(const Area& area); Area(const Area& area);
Area(Area&& area); Area(Area&& area);
Area(Cow&& move);
explicit Area(const Cow& copy);
inline const Cow* operator->() const { return _area.get(); } inline const Cow* operator->() const { return _area.get(); }
inline Cow* operator->() { return _area.get(); } inline Cow* operator->() { return _area.get(); }
@ -22,6 +27,8 @@ struct Area {
inline bool is_clone() const { return dynamic_cast<Cow::Fake*>(_area.get()) != nullptr; } inline bool is_clone() const { return dynamic_cast<Cow::Fake*>(_area.get()) != nullptr; }
inline cow_t* raw() const { return _area->raw(); }
~Area(); ~Area();
private: private:
const std::unique_ptr<Cow> _area; const std::unique_ptr<Cow> _area;

@ -6,3 +6,8 @@ Area::Area(const Area& copy) :
Area::Area(Area&& move) : Area::Area(Area&& move) :
_area(std::move(*const_cast<std::unique_ptr<Cow>*>(&move._area))){} _area(std::move(*const_cast<std::unique_ptr<Cow>*>(&move._area))){}
Area::~Area(){} Area::~Area(){}
Area::Area(Cow&& r) :
_area(std::make_unique<Cow>(std::move(r))){}
Area::Area(const Cow& r) :
_area(std::make_unique<Cow::Fake>(r.clone())){}

@ -41,6 +41,8 @@ cow_t* Cow::get_raw() const { return super->ptr(); }
size_t Cow::size() const { return super->cow.size; } size_t Cow::size() const { return super->cow.size; }
cow_t* Cow::raw() const { return &super->cow; }
Cow::Fake::Fake(const Cow& copy) : Cow(copy), fake(cow_clone(copy.super->ptr())){} Cow::Fake::Fake(const Cow& copy) : Cow(copy), fake(cow_clone(copy.super->ptr())){}
Cow::Fake::Fake(const Fake& copy) : Cow(copy), fake(cow_clone(copy.fake)){}//Fake(*static_cast<const Cow*>(&copy)){} Cow::Fake::Fake(const Fake& copy) : Cow(copy), fake(cow_clone(copy.fake)){}//Fake(*static_cast<const Cow*>(&copy)){}
Cow::Fake::Fake(Fake&& move) : Cow(std::move(move)), fake(move.fake) Cow::Fake::Fake(Fake&& move) : Cow(std::move(move)), fake(move.fake)

@ -170,7 +170,9 @@ void moving_cow(Cow moved)
int main() int main()
{ {
Area area(4000); Cow _area(4000);
Area area = std::move(_area);
write_fake(area, "Hello???"); write_fake(area, "Hello???");
Area area2 = area; Area area2 = area;
write_fake(area2, "Hi"); write_fake(area2, "Hi");

Loading…
Cancel
Save