refs #621, allow to load setup from resource dir file

* CValueObjects can be loaded from JSON file
* get bootstrap file location
This commit is contained in:
Klaus Basan
2016-03-19 01:44:29 +01:00
parent 87a0c75f43
commit 703a43c6fb
4 changed files with 46 additions and 2 deletions

View File

@@ -15,6 +15,7 @@
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/tuple.h"
#include "blackmisc/inheritancetraits.h"
#include "blackmisc/fileutils.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
@@ -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 <class T>
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 <class T>
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<DERIVED>::toJson; \
using ::BlackMisc::Mixin::JsonByTuple<DERIVED>::convertFromJson;
using ::BlackMisc::Mixin::JsonByTuple<DERIVED>::toJson; \
using ::BlackMisc::Mixin::JsonByTuple<DERIVED>::convertFromJson;
} // Mixin
} // BlackMisc

View File

@@ -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());

View File

@@ -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();