201 lines
7.6 KiB
C++
201 lines
7.6 KiB
C++
#ifndef _STORAGE_DATA_FORMAT_
|
||
#define _STORAGE_DATA_FORMAT_
|
||
|
||
#include <QString>
|
||
#include <string>
|
||
#include <map>
|
||
#include <vector>
|
||
#include <QDebug>
|
||
#include "Error.h"
|
||
#include "nlohmann/json.hpp"
|
||
#include "UtilityGlobal.h"
|
||
|
||
#ifdef __cplusplus
|
||
extern"C"{
|
||
#endif
|
||
|
||
using json = nlohmann::json;
|
||
using namespace std;
|
||
|
||
typedef json storageformat;
|
||
|
||
#define GET_DATA_BOOL(storage_data, key) storage_data.contains(key) ? storage_data[key] : false;
|
||
#define GET_DATA_NUM(storage_data, key) storage_data.contains(key) ? storage_data[key] : 0;
|
||
#define GET_DATA_STR(storage_data, key) storage_data.contains(key) ? storage_data[key] : "";
|
||
|
||
class UTILITY_API cssStorageDataFormat
|
||
{
|
||
public:
|
||
//cssStorageDataFormat(storageformat* p_memory) : _storage_data(p_memory) {};
|
||
cssStorageDataFormat(storageformat* p_memory){
|
||
if(nullptr == p_memory) {
|
||
throw std::invalid_argument("Value can not be nullptr!");
|
||
}
|
||
_storage_data = p_memory;
|
||
qDebug() << "sucess to initial";
|
||
}
|
||
|
||
~cssStorageDataFormat() {};
|
||
|
||
private:
|
||
storageformat* _storage_data;
|
||
|
||
private:
|
||
bool _check_value_exist(const char* key) { return (*_storage_data).contains(key); };
|
||
|
||
public:
|
||
static storageformat fromStdString(const std::string& input_data) { return json::parse(input_data); };
|
||
std::string toStdString(void) { return (*_storage_data).dump(4); };
|
||
static storageformat fromQString(const QString& input_data) { return json::parse(input_data.toStdString()); };
|
||
QString toQString(void) { return QString::fromStdString((*_storage_data).dump(4)); }
|
||
|
||
/* xml与json 均不需要 datatype char, 用string代替,char*和string二选一即可,根据库不同来替换,我们统一为std::string */
|
||
/* nlohmann::json 可以直接存std::map, std::vector 这两个容器,大家可以试用一下,减少工作量,但如果使用了,转xml就比较麻烦了 */
|
||
public:
|
||
auto get_data(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return storageformat ();
|
||
// storageformat () =
|
||
// nlohmann::json_abi_v3_11_3::basic_json<
|
||
// std::map,std::vector,std::string,
|
||
// bool,int64_t,uint64_t,double,
|
||
// std::allocator,nlohmann::json_abi_v3_11_3::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>,
|
||
// void> ();
|
||
}
|
||
/* 根据不同xml,json库,接口很难统一,目前先已经调研过的情况都做 */
|
||
// 有些库,只返回string(或char*)输入数据类型来读那么大的数据
|
||
std::string get_data(const char* key, const char* type)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return storageformat ();
|
||
}
|
||
bool get_bool(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return false;
|
||
}
|
||
short get_short(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
int get_int(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
unsigned int get_uint(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
long get_long(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
unsigned long get_ulong(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
long long get_longlong(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
unsigned long long get_ulonglong(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
float get_float(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
double get_double(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return 0;
|
||
}
|
||
std::string get_stdstr(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return "";
|
||
}
|
||
storageformat get_strorage_format(const char* key)
|
||
{
|
||
int err = EXECUTE_SUCCESS;
|
||
LOG_CHECK_EXIT(_check_value_exist(key), USR_DESIGN_ERROR, "The key didn't exist:%s", key);
|
||
return (*_storage_data)[key];
|
||
cleanup:
|
||
return storageformat();
|
||
}
|
||
// map 和 vector 用storageformat返回之后再接隐式转换,不用在这里处理,如果不支持,那一定是基础库,也就根本没有map和vector
|
||
|
||
public:
|
||
void store_data(const char* key, const bool value)
|
||
{ // 如果key已存在 则覆盖其value
|
||
(*_storage_data)[key] = value;
|
||
}
|
||
void store_data(const char* key, const short value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const int value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const unsigned int value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const long& value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const unsigned long& value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const long long& value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const float value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const double& value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const std::string& value) { (*_storage_data)[key] = value; }
|
||
void store_data(const char* key, const storageformat& value) { (*_storage_data)[key] = value; }
|
||
// static void store_data(storageformat& _storage_data, const char* key, const std::map<std::string, storageformat>& value) { (*_storage_data)[key] = value; } // test ok
|
||
// map 和 vector 用storageformat输入之后再接隐式转换,不用在这里处理,如果不支持,那一定是基础库,也就根本没有map和vector
|
||
|
||
};
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif// _STORAGE_DATA_FORMAT_
|