diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/crepe/DB.cpp | 67 | ||||
-rw-r--r-- | src/crepe/DB.h | 34 | ||||
-rw-r--r-- | src/crepe/ValueBroker.h | 27 | ||||
-rw-r--r-- | src/crepe/ValueBroker.hpp | 40 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 20 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 48 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.hpp | 4 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/util/Proxy.h | 23 | ||||
-rw-r--r-- | src/crepe/util/Proxy.hpp | 22 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/example/db.cpp | 30 | ||||
-rw-r--r-- | src/example/proxy.cpp | 48 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 31 |
18 files changed, 411 insertions, 1 deletions
@@ -1,6 +1,7 @@ build doxygen .cache +*.db CMakeLists.txt.user CMakeCache.txt @@ -14,3 +15,4 @@ compile_commands.json CTestTestfile.cmake _deps CMakeUserPresets.json + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b60a0cd..de9e990 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) +find_library(BERKELEY_DB db) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -23,6 +24,7 @@ target_link_libraries(crepe PRIVATE soloud PUBLIC SDL2 PUBLIC SDL2_image + PUBLIC ${BERKELEY_DB} ) add_subdirectory(crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d938eb8..750a47d 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -12,8 +12,8 @@ target_sources(crepe PUBLIC CollisionSystem.cpp Collider.cpp SDLContext.cpp - RenderSystem.cpp + DB.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -31,6 +31,9 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h SDLContext.h RenderSystem.h + ValueBroker.h + ValueBroker.hpp + DB.h ) add_subdirectory(api) diff --git a/src/crepe/DB.cpp b/src/crepe/DB.cpp new file mode 100644 index 0000000..b8448a7 --- /dev/null +++ b/src/crepe/DB.cpp @@ -0,0 +1,67 @@ +#include <cstring> + +#include "util/log.h" + +#include "DB.h" + +using namespace std; +using namespace crepe; + +DB::DB(const char * path) { + dbg_trace(); + int ret; + + // init database struct + libdb::DB * db; + if ((ret = libdb::db_create(&db, NULL, 0)) != 0) + throw nullptr; // TODO: exception + this->db = { db, [] (libdb::DB * db) { db->close(db, 0); } }; + + // load or create database file + if ((ret = this->db->open(this->db.get(), NULL, path, NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) { + throw nullptr; + } + + // create cursor + libdb::DBC * cursor; + if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) { + throw nullptr; + } + this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } }; +} + + +libdb::DBT DB::to_thing(const string & thing) { + libdb::DBT thang; + memset(&thang, 0, sizeof(libdb::DBT)); + thang.data = (void *) thing.data(); + thang.size = thing.size(); + return thang; +} + +string DB::get(const string & key) { + libdb::DBT db_key = this->to_thing(key); + libdb::DBT db_val; + memset(&db_val, 0, sizeof(libdb::DBT)); + + // int ret = this->cursor->get(this->cursor.get(), NULL, &db_key, &db_val); + return ""; +} + +void DB::set(const string & key, const string & value) { + libdb::DBT db_key = this->to_thing(key); + libdb::DBT db_val = this->to_thing(value); + int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0); + // TODO: check flags + // TODO: check ret +} + +bool DB::has(const std::string & key) { + try { + this->get(key); + } catch (...) { + return false; + } + return true; +} + diff --git a/src/crepe/DB.h b/src/crepe/DB.h new file mode 100644 index 0000000..010ef42 --- /dev/null +++ b/src/crepe/DB.h @@ -0,0 +1,34 @@ +#pragma once + +#include <string> +#include <functional> +#include <memory> + +namespace libdb { +extern "C" { +#include <db.h> +} +} + +namespace crepe { + +class DB { +public: + DB(const char * path); + virtual ~DB() = default; + +public: + std::string get(const std::string & key); + void set(const std::string & key, const std::string & value); + bool has(const std::string & key); + +private: + std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db; + std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor; + +private: + libdb::DBT to_thing(const std::string & thing); +}; + +} + diff --git a/src/crepe/ValueBroker.h b/src/crepe/ValueBroker.h new file mode 100644 index 0000000..9d30a2e --- /dev/null +++ b/src/crepe/ValueBroker.h @@ -0,0 +1,27 @@ +#pragma once + +#include <functional> + +namespace crepe { + +template <typename T> +class ValueBroker { +public: + virtual void set(const T &); + virtual const T & get(); + + typedef std::function<void(T & value, const T & target)> setter_t; + typedef std::function<const T & (T & value)> getter_t; +private: + T & value; + setter_t setter; + getter_t getter; +public: + ValueBroker(T &, const setter_t &, const getter_t &); + ValueBroker(T &); +}; + +} + +#include "ValueBroker.hpp" + diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp new file mode 100644 index 0000000..ef31c17 --- /dev/null +++ b/src/crepe/ValueBroker.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include <memory> + +#include "ValueBroker.h" + +namespace crepe { + +template <typename T> +ValueBroker<T>::ValueBroker(T & value) : + value(value), + setter([] (T & value, const T & target) { + value = std::move(target); + }), + getter([] (T & value) -> const int & { + return value; + }) + { +} + +template <typename T> +ValueBroker<T>::ValueBroker(T & value, const setter_t & setter, const getter_t & getter) : + value(value), + setter(setter), + getter(getter) + { +} + +template <typename T> +const T & ValueBroker<T>::get() { + return this->getter(this->value); +} + +template <typename T> +void ValueBroker<T>::set(const T & value) { + this->setter(this->value, value); +} + +} + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..97bc6b1 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + SaveManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +29,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + SaveManager.h + SaveManager.hpp ) diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp new file mode 100644 index 0000000..83ff0fa --- /dev/null +++ b/src/crepe/api/SaveManager.cpp @@ -0,0 +1,20 @@ +#include "../DB.h" +#include "util/log.h" + +#include "SaveManager.h" + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +SaveManager::SaveManager() { + dbg_trace(); + this->db = make_unique<DB>("./save.crepe.db"); +} + +SaveManager & SaveManager::get_instance() { + dbg_trace(); + static SaveManager instance; + return instance; +} + diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h new file mode 100644 index 0000000..110735d --- /dev/null +++ b/src/crepe/api/SaveManager.h @@ -0,0 +1,48 @@ +#pragma once + +#include <memory> + +#include "../ValueBroker.h" + +namespace crepe { +class DB; +} + +namespace crepe::api { + +class SaveManager { +public: + //! Get a reference to a value and initialize it with a value if it does not yet exist + template <typename T> + ValueBroker<T> & get(const char * key, const T & default_value); + + //! Get a reference to a value + template <typename T> + ValueBroker<T> & get(const char * key); + + //! Set a value directly + template <typename T> + void set(const char * key, const T & value); + + //! Check if the save file has a value for this \c key + bool has(const char * key); + +private: + SaveManager(); + virtual ~SaveManager() = default; + +public: + // singleton + static SaveManager & get_instance(); + SaveManager(const SaveManager &) = delete; + SaveManager(SaveManager &&) = delete; + SaveManager & operator = (const SaveManager &) = delete; + SaveManager & operator = (SaveManager &&) = delete; + +private: + std::unique_ptr<DB> db = nullptr; +}; + +} + +#include "SaveManager.hpp" diff --git a/src/crepe/api/SaveManager.hpp b/src/crepe/api/SaveManager.hpp new file mode 100644 index 0000000..44f58ed --- /dev/null +++ b/src/crepe/api/SaveManager.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "SaveManager.h" + diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index bbeaad9..01d8f22 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -8,5 +8,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES color.h log.h fmt.h + Proxy.h + Proxy.hpp ) diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h new file mode 100644 index 0000000..f8eb1f2 --- /dev/null +++ b/src/crepe/util/Proxy.h @@ -0,0 +1,23 @@ +#pragma once + +#include "ValueBroker.h" + +namespace crepe::util { + +template <typename T> +class Proxy { +public: + Proxy & operator = (const T &); + operator const T & () const; + +public: + Proxy(ValueBroker<T> &); + +private: + ValueBroker<T> & broker; +}; + +} + +#include "Proxy.hpp" + diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp new file mode 100644 index 0000000..5738d9c --- /dev/null +++ b/src/crepe/util/Proxy.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "Proxy.h" + +namespace crepe::util { + +template <typename T> +Proxy<T>::Proxy(ValueBroker<T> & broker) : broker(broker) { } + +template <typename T> +Proxy<T> & Proxy<T>::operator = (const T & val) { + this->broker.set(val); + return *this; +} + +template <typename T> +Proxy<T>::operator const T & () const { + return this->broker.get(); +} + +} + diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index fea6f60..9698c41 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -24,3 +24,7 @@ add_example(rendering) add_example(asset_manager) add_example(particle) add_example(physics) +add_example(savemgr) +add_example(proxy) +add_example(db) + diff --git a/src/example/db.cpp b/src/example/db.cpp new file mode 100644 index 0000000..b1ab196 --- /dev/null +++ b/src/example/db.cpp @@ -0,0 +1,30 @@ +#include <crepe/DB.h> +#include <crepe/api/Config.h> +#include <crepe/util/log.h> + +using namespace crepe; +using namespace std; + +// run before main +static auto _ = [] () { + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::TRACE; + return 0; +}(); + +int main() { + dbg_trace(); + + DB db("file.db"); + + const char * test_key = "test-key"; + string test_data = "Hello world!"; + + dbg_logf("DB has key = %d", db.has(test_key)); + + db.set(test_key, test_data); + + dbg_logf("key = \"%s\"", db.get(test_key).c_str()); + + return 0; +} diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp new file mode 100644 index 0000000..7c2cb8d --- /dev/null +++ b/src/example/proxy.cpp @@ -0,0 +1,48 @@ +/** \file + * + * Standalone example for usage of the proxy type + */ + +#include "ValueBroker.h" +#include <crepe/api/Config.h> +#include <crepe/util/log.h> +#include <crepe/util/Proxy.h> + +using namespace std; +using namespace crepe; +using namespace crepe::util; + +void test_ro_ref(const int & val) { } +void test_rw_ref(int & val) { } +void test_ro_val(int val) { } + +int main() { + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::DEBUG; + + int real_value = 0; + + ValueBroker<int> broker { + real_value, + [] (int & value, const int & target) { + dbg_logf("set %s to %s", to_string(value).c_str(), to_string(target).c_str()); + value = target; + }, + [] (int & value) -> const int & { + dbg_logf("get %s", to_string(value).c_str()); + return value; + }, + }; + + Proxy<int> proxy { broker }; + + broker.set(54); + proxy = 84; + + test_ro_ref(proxy); // this is allowed + // test_rw_ref(proxy); // this should throw a compile error + test_ro_val(proxy); + + return 0; +} + diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp new file mode 100644 index 0000000..2c03e3a --- /dev/null +++ b/src/example/savemgr.cpp @@ -0,0 +1,31 @@ +/** \file + * + * Standalone example for usage of the save manager + */ + +#include <cassert> +#include <crepe/util/log.h> +#include <crepe/util/Proxy.h> +#include <crepe/api/SaveManager.h> + +using namespace crepe; +using namespace crepe::api; +using namespace crepe::util; + +int main() { + const char * key = "mygame.test"; + + SaveManager & mgr = SaveManager::get_instance(); + + ValueBroker<unsigned int> & prop = mgr.get<unsigned int>(key, 0); + Proxy<unsigned int> val = mgr.get<unsigned int>(key, 0); + + prop.set(1); + val = 2; + mgr.set<unsigned int>(key, 3); + + assert(true == mgr.has(key)); + + return 0; +} + |