From cc3bb171e1a9e683823e2613467b9e92ba7e3757 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 10 Sep 2024 21:17:15 -0400 Subject: [PATCH] Restructing the source layout to make it nicer. --- Makefile | 4 +- meson.build | 26 +--- audiotest.cpp => scratchpad/audiotest.cpp | 0 badref.cpp => scratchpad/badref.cpp | 0 corotest.cpp => scratchpad/corotest.cpp | 2 +- fsmtest.cpp => scratchpad/fsmtest.cpp | 2 +- .../ftx_thread_test.cpp | 6 +- ftxtest.cpp => scratchpad/ftxtest.cpp | 6 +- jsontest.cpp => scratchpad/jsontest.cpp | 0 scratchpad/meson.build | 23 ++++ regtest.cpp => scratchpad/regtest.cpp | 0 threadtest.cpp => scratchpad/threadtest.cpp | 0 scratchpad/uniqptrtest.cpp | 130 ++++++++++++++++++ .tarpit_sample.json => tarpit_sample.json | 0 14 files changed, 166 insertions(+), 33 deletions(-) rename audiotest.cpp => scratchpad/audiotest.cpp (100%) rename badref.cpp => scratchpad/badref.cpp (100%) rename corotest.cpp => scratchpad/corotest.cpp (97%) rename fsmtest.cpp => scratchpad/fsmtest.cpp (96%) rename ftx_thread_test.cpp => scratchpad/ftx_thread_test.cpp (93%) rename ftxtest.cpp => scratchpad/ftxtest.cpp (92%) rename jsontest.cpp => scratchpad/jsontest.cpp (100%) create mode 100644 scratchpad/meson.build rename regtest.cpp => scratchpad/regtest.cpp (100%) rename threadtest.cpp => scratchpad/threadtest.cpp (100%) create mode 100644 scratchpad/uniqptrtest.cpp rename .tarpit_sample.json => tarpit_sample.json (100%) diff --git a/Makefile b/Makefile index a85a0df..069c269 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: build test patch: - cp .\patches\process.h .\subprojects\libgit2-1.8.1\src\util\process.h + powershell "cp ./patches/process.h ./subprojects/libgit2-1.8.1/src/util/process.h" build: meson compile -j 4 -C builddir @@ -11,6 +11,8 @@ test: # make an install for real maybe copy dll and .exe to dir and zip? install: build test + powershell "cp ./builddir/subprojects/libgit2-1.8.1/liblibgit2package.dll ." + powershell "cp ./builddir/subprojects/efsw/libefsw.dll ." powershell "cp builddir/escape_turings_tarpit.exe ." run: install diff --git a/meson.build b/meson.build index 8faa8d5..ae9148a 100644 --- a/meson.build +++ b/meson.build @@ -25,6 +25,8 @@ dependencies = [ sfml, imgui, json ] +subdir('scratchpad') + executable('escape_turings_tarpit', ['game_engine.cpp', 'gui.cpp', @@ -34,30 +36,6 @@ executable('escape_turings_tarpit', 'escape_turings_tarpit.cpp'], dependencies: dependencies) -executable('regtest', 'regtest.cpp', - dependencies: [fmt]) - -executable('audiotest', 'audiotest.cpp', - dependencies: dependencies) - -executable('jsontest', 'jsontest.cpp', - dependencies: dependencies) - -executable('threadtest', 'threadtest.cpp', - dependencies: dependencies) - -executable('fsmtest', 'fsmtest.cpp', - dependencies: dependencies) - -executable('badref', 'badref.cpp', - dependencies: dependencies) - -executable('corotest', [ - 'corotest.cpp' - ], - dependencies: dependencies, - cpp_args: '-fcoroutines') - runtests = executable('runtests', [ 'game_engine.cpp', 'tests/game_engine.cpp', diff --git a/audiotest.cpp b/scratchpad/audiotest.cpp similarity index 100% rename from audiotest.cpp rename to scratchpad/audiotest.cpp diff --git a/badref.cpp b/scratchpad/badref.cpp similarity index 100% rename from badref.cpp rename to scratchpad/badref.cpp diff --git a/corotest.cpp b/scratchpad/corotest.cpp similarity index 97% rename from corotest.cpp rename to scratchpad/corotest.cpp index c6d7a8a..30d8412 100644 --- a/corotest.cpp +++ b/scratchpad/corotest.cpp @@ -1,4 +1,4 @@ -#include "coro.hpp" +#include "../coro.hpp" #include #include #include diff --git a/fsmtest.cpp b/scratchpad/fsmtest.cpp similarity index 96% rename from fsmtest.cpp rename to scratchpad/fsmtest.cpp index d1a67d0..f7bedeb 100644 --- a/fsmtest.cpp +++ b/scratchpad/fsmtest.cpp @@ -1,5 +1,5 @@ #include -#include "fsm.hpp" +#include "../fsm.hpp" using namespace fmt; diff --git a/ftx_thread_test.cpp b/scratchpad/ftx_thread_test.cpp similarity index 93% rename from ftx_thread_test.cpp rename to scratchpad/ftx_thread_test.cpp index 833712f..28d9916 100644 --- a/ftx_thread_test.cpp +++ b/scratchpad/ftx_thread_test.cpp @@ -9,9 +9,9 @@ #include // for operator+, to_string #include // for sleep_for -#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|= -#include "ftxui/component/loop.hpp" // for Loop -#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive +#include // for CatchEvent, Renderer, operator|= +#include // for Loop +#include // for ScreenInteractive using namespace ftxui; diff --git a/ftxtest.cpp b/scratchpad/ftxtest.cpp similarity index 92% rename from ftxtest.cpp rename to scratchpad/ftxtest.cpp index 389508c..54aa75c 100644 --- a/ftxtest.cpp +++ b/scratchpad/ftxtest.cpp @@ -8,9 +8,9 @@ #include // for operator+, to_string #include // for sleep_for -#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|= -#include "ftxui/component/loop.hpp" // for Loop -#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive +#include // for CatchEvent, Renderer, operator|= +#include // for Loop +#include // for ScreenInteractive #include using namespace ftxui; diff --git a/jsontest.cpp b/scratchpad/jsontest.cpp similarity index 100% rename from jsontest.cpp rename to scratchpad/jsontest.cpp diff --git a/scratchpad/meson.build b/scratchpad/meson.build new file mode 100644 index 0000000..0597ebf --- /dev/null +++ b/scratchpad/meson.build @@ -0,0 +1,23 @@ +executable('regtest', 'regtest.cpp', + dependencies: [fmt]) + +executable('audiotest', 'audiotest.cpp', + dependencies: dependencies) + +executable('jsontest', 'jsontest.cpp', + dependencies: dependencies) + +executable('threadtest', 'threadtest.cpp', + dependencies: dependencies) + +executable('fsmtest', 'fsmtest.cpp', + dependencies: dependencies) + +executable('badref', 'badref.cpp', + dependencies: dependencies) + +executable('corotest', [ + 'corotest.cpp' + ], + dependencies: dependencies, + cpp_args: '-fcoroutines') diff --git a/regtest.cpp b/scratchpad/regtest.cpp similarity index 100% rename from regtest.cpp rename to scratchpad/regtest.cpp diff --git a/threadtest.cpp b/scratchpad/threadtest.cpp similarity index 100% rename from threadtest.cpp rename to scratchpad/threadtest.cpp diff --git a/scratchpad/uniqptrtest.cpp b/scratchpad/uniqptrtest.cpp new file mode 100644 index 0000000..5bdd157 --- /dev/null +++ b/scratchpad/uniqptrtest.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include + +// helper class for runtime polymorphism demo below +struct B +{ + virtual ~B() = default; + + virtual void bar() { std::cout << "B::bar\n"; } +}; + +struct D : B +{ + D() { std::cout << "D::D\n"; } + ~D() { std::cout << "D::~D\n"; } + + void bar() override { std::cout << "D::bar\n"; } +}; + +// a function consuming a unique_ptr can take it by value or by rvalue reference +std::unique_ptr pass_through(std::unique_ptr p) +{ + p->bar(); + return p; +} + +// helper function for the custom deleter demo below +void close_file(std::FILE* fp) +{ + std::fclose(fp); +} + +// unique_ptr-based linked list demo +struct List +{ + struct Node + { + int data; + std::unique_ptr next; + }; + + std::unique_ptr head; + + ~List() + { + // destroy list nodes sequentially in a loop, the default destructor + // would have invoked its `next`'s destructor recursively, which would + // cause stack overflow for sufficiently large lists. + while (head) + { + auto next = std::move(head->next); + head = std::move(next); + } + } + + void push(int data) + { + head = std::unique_ptr(new Node{data, std::move(head)}); + } +}; + +int main() +{ + std::cout << "1) Unique ownership semantics demo\n"; + { + // Create a (uniquely owned) resource + std::unique_ptr p = std::make_unique(); + + // Transfer ownership to `pass_through`, + // which in turn transfers ownership back through the return value + std::unique_ptr q = pass_through(std::move(p)); + + // p is now in a moved-from 'empty' state, equal to nullptr + assert(!p); + } + + std::cout << "\n" "2) Runtime polymorphism demo\n"; + { + // Create a derived resource and point to it via base type + std::unique_ptr p = std::make_unique(); + + // Dynamic dispatch works as expected + p->bar(); + } + + std::cout << "\n" "3) Custom deleter demo\n"; + std::ofstream("demo.txt") << 'x'; // prepare the file to read + { + using unique_file_t = std::unique_ptr; + unique_file_t fp(std::fopen("demo.txt", "r"), &close_file); + if (fp) + std::cout << char(std::fgetc(fp.get())) << '\n'; + } // `close_file()` called here (if `fp` is not null) + + std::cout << "\n" "4) Custom lambda-expression deleter and exception safety demo\n"; + try + { + std::unique_ptr p(new D, [](D* ptr) + { + std::cout << "destroying from a custom deleter...\n"; + delete ptr; + }); + + throw std::runtime_error(""); // `p` would leak here if it were a plain pointer + } + catch (const std::exception&) + { + std::cout << "Caught exception\n"; + } + + std::cout << "\n" "5) Array form of unique_ptr demo\n"; + { + std::unique_ptr p(new D[3]); + } // `D::~D()` is called 3 times + + std::cout << "\n" "6) Linked list demo\n"; + { + List wall; + const int enough{1'000'000}; + for (int beer = 0; beer != enough; ++beer) + wall.push(beer); + + std::cout << enough << " bottles of beer on the wall...\n"; + } // destroys all the beers +} diff --git a/.tarpit_sample.json b/tarpit_sample.json similarity index 100% rename from .tarpit_sample.json rename to tarpit_sample.json