mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-13 15:45:42 +08:00
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:
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user