From a66ff34f021eed289ad24def303ab9020c613d95 Mon Sep 17 00:00:00 2001 From: corwinn Date: Thu, 3 Aug 2023 15:17:52 +0300 Subject: [PATCH] NOptions --- Makefile | 2 +- nifwind.h | 8 +++---- src/n_main_window.cpp | 10 +++++++++ src/services/n_options.h | 46 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index fca1f18..cc19208 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ CXXFLAGS = -std=c++14 -march=core2 -mtune=core2 \ SAN = -fsanitize=undefined,leak,address CXXFLAGS += $(SAN) -std=c++14 \ -DNIFWIND_VERSION="\"1.0\"" \ - -I. -Isrc $(FFD_CFLAGS) \ + -I. -Isrc -Isrc/services $(FFD_CFLAGS) \ -I${QTDIR}/include -I${QTDIR}/include/QtCore -I${QTDIR}/include/QtWidgets \ -I${QTDIR}/include/QtOpenGL -I${QTDIR}/include/QtGui \ -DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB \ diff --git a/nifwind.h b/nifwind.h index c1a9572..1a545f6 100644 --- a/nifwind.h +++ b/nifwind.h @@ -81,12 +81,12 @@ NIFWIND_NAMESPACE NAMESPACE_NIFWIND #ifdef NIFWIND_TESTING -# define NTHROW(E) throw E; +# define NTHROW(E) {throw E;} #else -# define NTHROW(E) NException::Exit (E); +# define NTHROW(E) {NException::Exit (E);} #endif -#define NSURE(C,M) if (! (C)) \ - { NTHROW ((NAssertionFailed {M, __FILE__, __LINE__})) } +#define NSURE(C,M) { if (! (C)) \ + { NTHROW ((NAssertionFailed {M, __FILE__, __LINE__})) }} #endif diff --git a/src/n_main_window.cpp b/src/n_main_window.cpp index 48bb190..5e6963d 100644 --- a/src/n_main_window.cpp +++ b/src/n_main_window.cpp @@ -35,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **** END LICENCE BLOCK ****/ #include "n_main_window.h" +#include "n_options.h" #include #include @@ -45,6 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "n_tree_model.h" #include "n_ffd_node_adapter.h" @@ -69,6 +71,14 @@ NMainWindow::NMainWindow() auto root = new NFFDNodeAdapter; // can't be shown new NFFDNodeAdapter {nullptr, root}; auto tree = new NTreeModel (root, this); + + // TODO to a unit test: + int arr[] = {1,2,3}; + NOptions::PutIntArr ("foo", QVector {arr, arr+3}); + auto arr2 = NOptions::GetIntArr ("foo"); + qDebug () << arr2; + + printf ("arr %d: %d, %d, %d" EOL, arr2.size (), arr2[0], arr2[1], arr2[2]); auto tv = new QTreeView {}; // I have to sub-class QTreeView just to set its initial size as a docked // tree?! You have got to be nice mountain view kidding me. TODO read diff --git a/src/services/n_options.h b/src/services/n_options.h index 3976299..39a150a 100644 --- a/src/services/n_options.h +++ b/src/services/n_options.h @@ -36,6 +36,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define _N_OPTIONS_H_ #include "nifwind.h" +#include +#include NIFWIND_NAMESPACE @@ -44,13 +46,53 @@ NIFWIND_NAMESPACE // Things you put here could get persisted. // // This is a key-value store: means you have to be careful with the keys: a -// duplicate one will reward you with an exception. Recommended usage: +// duplicate one will reward you with an exception (update = false). Recommended +// usage: // object_name.domain.sub-domain.data_name // example: "mainform.dockwidgets.treeview.x" // A non existent one will reward you with an exception as well. -class NOptions final +// +// You can use Q_DECLARE_METATYPE as well, because this is +// QMap. +// +// Type support shall be added on an as-needed basis, via the macros: +// - n_options.h : N_OPT (type alias, actual type) +// - n_options.cpp: N_PUT_GET (type alias) +// N_PUT_GET_M(type alias) - for Q_DECLARE_METATYPE +// - Q_DECLARE_METATYPE - add to the list below +// (if you get an error with N_PUT_GET you should use N_PUT_GET_M) +// (the type alias is a Get/Put suffix as well - so it must comply with C++) +// Given a type alias "Foo", you can: +// - PutFoo(key, value) - to put a new non-existent key-value +// - PutFoo(key, value, NOptions::Put::Update) - to update an existing value +// - GetFoo(key) - to get a previously stored value +// +// Thread-unsafe. This shall be used by the single main thread only. Its +// persistence shall be made via a copy created by the main thread and queued +// at the async. IO (LATER MemoryStream AsStream(); FromStream(Stream &)). +// +// Please do not store 10000-node-trees here; Please do not query this 60-times +// per second. +class NOptions final // program options { + N_NO_COPY(NOptions) + N_NO_MOVE(NOptions) + private: NOptions() {} + + enum class Put {New, Update}; // because: no named parameters +#define N_OPT(T,R) \ + using T = R; \ + public: static void Put##T(const char * k, T v, Put op = Put::New); \ + public: static T Get##T(const char * k); + + N_OPT(Int, int) + N_OPT(IntArr, QVector) + +#undef N_OPT }; NAMESPACE_NIFWIND + +Q_DECLARE_METATYPE(QVector) + #endif