From 703a43c6fb0537e584cdbbaca384e1d08c3ac913 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 19 Mar 2016 01:44:29 +0100 Subject: [PATCH] refs #621, allow to load setup from resource dir file * CValueObjects can be loaded from JSON file * get bootstrap file location --- src/blackcore/data/globalsetup.h | 3 +++ src/blackmisc/json.h | 27 +++++++++++++++++++++++++-- src/blackmisc/project.cpp | 15 +++++++++++++++ src/blackmisc/project.h | 3 +++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/blackcore/data/globalsetup.h b/src/blackcore/data/globalsetup.h index 80fef3e96..311cded06 100644 --- a/src/blackcore/data/globalsetup.h +++ b/src/blackcore/data/globalsetup.h @@ -144,6 +144,9 @@ namespace BlackCore //! Build bootstrap file URL static QString buildBootstrapFileUrl(const QString &candidate); + //! Object initialized by JSON file + static CGlobalSetup fromJsonFile(const QString &fileNameAndPath); + private: BLACK_ENABLE_TUPLE_CONVERSION(BlackCore::Data::CGlobalSetup) diff --git a/src/blackmisc/json.h b/src/blackmisc/json.h index 792f89a45..788fbe680 100644 --- a/src/blackmisc/json.h +++ b/src/blackmisc/json.h @@ -15,6 +15,7 @@ #include "blackmisc/blackmiscexport.h" #include "blackmisc/tuple.h" #include "blackmisc/inheritancetraits.h" +#include "blackmisc/fileutils.h" #include #include #include @@ -164,6 +165,28 @@ namespace BlackMisc //! Merges an incremental json object into an existing one BLACKMISC_EXPORT QJsonObject applyIncrementalObject(const QJsonObject &previousObject, const QJsonObject &incrementalObject); + /*! + * Load JSON file and init by that + */ + template + bool loadFromJsonFile(T &object, const QString &fileNameAndPath) + { + const QString jsonString(CFileUtils::readFileToString(fileNameAndPath)); + if (jsonString.isEmpty()) { return false; } + object.convertFromJson(jsonString); + return true; + } + + /*! + * Save to JSON file + */ + template + bool saveToJsonFile(const T &object, const QString &fileNameAndPath) + { + const QString jsonString(object.toJsonString()); + if (jsonString.isEmpty()) { return false; } + return CFileUtils::writeStringToFile(jsonString, fileNameAndPath); + } } // Json namespace Mixin @@ -266,8 +289,8 @@ namespace BlackMisc * the derived class uses this macro to disambiguate the inherited members. */ # define BLACKMISC_DECLARE_USING_MIXIN_JSON(DERIVED) \ - using ::BlackMisc::Mixin::JsonByTuple::toJson; \ - using ::BlackMisc::Mixin::JsonByTuple::convertFromJson; + using ::BlackMisc::Mixin::JsonByTuple::toJson; \ + using ::BlackMisc::Mixin::JsonByTuple::convertFromJson; } // Mixin } // BlackMisc diff --git a/src/blackmisc/project.cpp b/src/blackmisc/project.cpp index 088bcf330..31a4cdc3b 100644 --- a/src/blackmisc/project.cpp +++ b/src/blackmisc/project.cpp @@ -305,6 +305,21 @@ namespace BlackMisc return s; } + const QString getBootstrapResourceFileImpl() + { + const QString d(CProject::getSwiftResourceDir()); + if (d.isEmpty()) { return ""; } + const QFile file(CFileUtils::appendFilePaths(d, "shared/boostrap/boostrap.json")); + Q_ASSERT_X(file.exists(), Q_FUNC_INFO, "missing dir"); + return QFileInfo(file).absoluteFilePath(); + } + + const QString &CProject::getBootstrapResourceFile() + { + static const QString s(getBootstrapResourceFileImpl()); + return s; + } + QString getSwiftStaticDbFilesDirImpl() { const QString d(CProject::getSwiftResourceDir()); diff --git a/src/blackmisc/project.h b/src/blackmisc/project.h index 1ec0bfa40..b09c1ebd3 100644 --- a/src/blackmisc/project.h +++ b/src/blackmisc/project.h @@ -96,6 +96,9 @@ namespace BlackMisc //! Where resource files (static DB files, ...) etc are located static const QString &getSwiftResourceDir(); + //! Bootstrap resource directory + static const QString &getBootstrapResourceFile(); + //! Where static DB files are located static const QString &getSwiftStaticDbFilesDir();