Added `Area`

cpp
Avril 4 years ago
parent f31805d9b2
commit d1c3d67326
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -69,6 +69,11 @@ all: | clean
.PHONY: install .PHONY: install
.PHONY: uninstall .PHONY: uninstall
.PHONY: test
test:
@rm -f $(PROJECT)-cpp-test
@$(MAKE) $(PROJECT)-cpp-test
# Targets # Targets
dirs: dirs:
@ -130,3 +135,7 @@ uninstall:
-rm $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).{a,so} -rm $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).{a,so}
cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/{}" cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/{}"
-rmdir $(DESTDIR)$(PREFIX)/include/$(PROJECT) -rmdir $(DESTDIR)$(PREFIX)/include/$(PROJECT)
$(PROJECT)-cpp-test: lib$(PROJECT).a
g++ -O3 --std=gnu++20 -Iinclude/ -g -Wall -Wextra src/test/*.cpp -o $@ -l:$<
valgrind ./$@

@ -0,0 +1,28 @@
// `Area` is a copyable type that opaquely represents either `Cow` or `Cow::Fake`.
#pragma once
#include <memory>
#include <utility>
#include <cow.hpp>
struct Area {
Area(size_t sz);
Area(const Area& area);
Area(Area&& area);
inline const Cow* operator->() const { return _area.get(); }
inline Cow* operator->() { return _area.get(); }
inline const Cow* operator*() const { return _area.get(); }
inline Cow* operator*() { return _area.get(); }
inline operator const Cow&() const { return *_area.get(); }
inline operator Cow&() { return *_area.get(); }
inline bool is_clone() const { return dynamic_cast<Cow::Fake*>(_area.get()) != nullptr; }
~Area();
private:
const std::unique_ptr<Cow> _area;
};

@ -0,0 +1,8 @@
#include <cow/area.hpp>
Area::Area(size_t sz) : _area(std::make_unique<Cow>(sz)){}
Area::Area(const Area& copy) :
_area(std::make_unique<Cow::Fake>(*copy._area.get())){}
Area::Area(Area&& move) :
_area(std::move(*const_cast<std::unique_ptr<Cow>*>(&move._area))){}
Area::~Area(){}

@ -7,6 +7,8 @@
#include <vector> #include <vector>
#include <cow/area.hpp>
using namespace _cow_util; using namespace _cow_util;
/// UTF8 multibyte 4. /// UTF8 multibyte 4.
@ -35,7 +37,7 @@ struct utf8_t {
} }
constexpr inline utf8_t(const char* c) { constexpr inline utf8_t(const char* c) {
for(int i=0;i<MULTIBYTE && (data[i] = c[i]); i++) (void)0; for(size_t i=0;i<MULTIBYTE && (data[i] = c[i]); i++) (void)0;
} }
constexpr inline utf8_t(const Unicode& data) : data(data) {} constexpr inline utf8_t(const Unicode& data) : data(data) {}
@ -53,6 +55,7 @@ struct utf8_t {
Unicode data; Unicode data;
}; };
namespace Tiling { namespace Tiling {
struct Map; struct Map;
@ -138,6 +141,8 @@ namespace Tiling {
}; };
} }
template<typename T = unsigned char> template<typename T = unsigned char>
void print_slice(Slice<T> memory) void print_slice(Slice<T> memory)
{ {
@ -165,11 +170,23 @@ void moving_cow(Cow moved)
int main() int main()
{ {
Area area(4000);
write_fake(area, "Hello???");
Area area2 = area;
write_fake(area2, "Hi");
Area area3 = std::move(area2);
Area area4 = std::move(area);
read_fake(area3);
read_fake(area4);
printf("Is clone: a1: %d, a2: %d\n", area4.is_clone(), area3.is_clone());
utf8_t ch = ""; utf8_t ch = "";
utf8_t ch2('a'); utf8_t ch2('a');
utf8_t ch3 = ch.c_str(); utf8_t ch3 = ch.c_str();
utf8_t ch4 = ch3.data; utf8_t ch4 = ch3.data;
utf8_t ch5 = ch4; utf8_t ch5 = ch4;
(void)ch5;
printf("Test: %s, %s, %s\n", (const char*)ch, ch2.c_str(), ch3.c_str()); printf("Test: %s, %s, %s\n", (const char*)ch, ch2.c_str(), ch3.c_str());
Cow real(4096); Cow real(4096);

Loading…
Cancel
Save