aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt4
-rw-r--r--src/example/db.cpp30
-rw-r--r--src/example/proxy.cpp47
-rw-r--r--src/example/savemgr.cpp47
4 files changed, 128 insertions, 0 deletions
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..0be4fac
--- /dev/null
+++ b/src/example/proxy.cpp
@@ -0,0 +1,47 @@
+/** \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] (const int & target) {
+ dbg_logf("set %s to %s", to_string(real_value).c_str(), to_string(target).c_str());
+ real_value = target;
+ },
+ [&real_value] () -> const int & {
+ dbg_logf("get %s", to_string(real_value).c_str());
+ return real_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..6d011e5
--- /dev/null
+++ b/src/example/savemgr.cpp
@@ -0,0 +1,47 @@
+/** \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>
+#include <crepe/api/Config.h>
+
+using namespace crepe;
+using namespace crepe::api;
+using namespace crepe::util;
+
+// unrelated setup code
+int _ = [] () {
+ // make sure all log messages get printed
+ auto & cfg = Config::get_instance();
+ cfg.log.level = util::LogLevel::TRACE;
+
+ return 0; // satisfy compiler
+} ();
+
+int main() {
+ const char * key = "mygame.test";
+
+ SaveManager & mgr = SaveManager::get_instance();
+
+ dbg_logf("has key = %s", mgr.has(key) ? "true" : "false");
+ ValueBroker<int> prop = mgr.get<int>(key, 0);
+ Proxy<int> val = mgr.get<int>(key, 0);
+
+ dbg_logf("val = %d", mgr.get<int>(key).get());
+ prop.set(1);
+ dbg_logf("val = %d", mgr.get<int>(key).get());
+ val = 2;
+ dbg_logf("val = %d", mgr.get<int>(key).get());
+ mgr.set<int>(key, 3);
+ dbg_logf("val = %d", mgr.get<int>(key).get());
+
+ dbg_logf("has key = %s", mgr.has(key) ? "true" : "false");
+ assert(true == mgr.has(key));
+
+ return 0;
+}
+