Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
NOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Aug 3, 2023
1 parent 9d9a29a commit a66ff34
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
8 changes: 4 additions & 4 deletions nifwind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions src/n_main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QAction>
#include <QMenuBar>
Expand All @@ -45,6 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <QDockWidget>
#include <QVector>
#include <QString>
#include <QDebug>

#include "n_tree_model.h"
#include "n_ffd_node_adapter.h"
Expand All @@ -69,6 +71,14 @@ NMainWindow::NMainWindow()
auto root = new NFFDNodeAdapter; // can't be shown
new NFFDNodeAdapter {nullptr, root};
auto tree = new NTreeModel<NFFDNodeAdapter> (root, this);

// TODO to a unit test:
int arr[] = {1,2,3};
NOptions::PutIntArr ("foo", QVector<int> {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
Expand Down
46 changes: 44 additions & 2 deletions src/services/n_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define _N_OPTIONS_H_

#include "nifwind.h"
#include <QVector>
#include <QMetaType>

NIFWIND_NAMESPACE

Expand All @@ -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<QString, QVariant>.
//
// 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<int>)

#undef N_OPT
};

NAMESPACE_NIFWIND

Q_DECLARE_METATYPE(QVector<int>)

#endif

0 comments on commit a66ff34

Please sign in to comment.