NSUKit 1.4.0
板卡级统一交互接口
载入中...
搜索中...
未找到
json_icd_wrapper.h
浏览该文件的文档.
1//
2// Created by 56585 on 2023/10/11.
3//
4
5#ifndef NSUKIT_JSON_ICD_WRAPPER_H
6#define NSUKIT_JSON_ICD_WRAPPER_H
7
9#include "json/json.h"
10
11namespace nsukit {
13 public:
14 Json::Reader ICDReader; //icd.json读取器
15 Json::Value icdRoot{};
16 Json::Value *icdParams{};
17 Json::Value *icdCommands{};
18
19 bool InitICD(std::string_view path);
20
28 template<class T>
29 CommandPack *FmtRegister(const Json::Value &reg, const T &value);
30
31 CommandPack *FmtRegister(const Json::Value &reg, const Json::Value &value);
32
33 CommandPack *FmtRegister(const Json::Value &reg, const std::string &value);
34
35 CommandPack *FmtFile(const std::string &file_path, bool need_size = false);
36
45 template<class T>
46 nsukitStatus_t set_icd_param(nsuCSParam_t &param_name, const T &value);
47
48
56 template<typename T>
57 T get_icd_param(nsuCSParam_t &param_name);
58 };
59
60
61 template<class T>
62 CommandPack *JsonWrapper::FmtRegister(const Json::Value &reg, const T &value) {
63 auto *res = new CommandPack;
64 int size;
65 size = TypeSizeMap[reg[0].asString()];
66 res->bytes = new char[size + 1]();
67 res->length = size;
68 memcpy(res->bytes, &value, size);
69 return res;
70 }
71
72
73 template<class T>
75 Json::Value *param;
76 if (!icdParams->isMember(param_name)) return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
77 param = &(*icdParams)[param_name];
78 (*param)[1] = Json::Value(value);
79 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
80 }
81
82
83 template<class T>
85 Json::Value reg, value;
86 T _value;
87 if (!icdParams->isMember(param_name)){
88 throw std::runtime_error("此参数不存在");
89 }
90 reg = (*icdParams)[param_name];
91 value = reg[1];
92
93 if (value.isString() && StringStartWith(value.asString(), "0x")) {
94 _value = (T) std::stoll(value.asString(), nullptr, 16);
95 } else if (value.isString() && StringStartWith(value.asString(), "0b")) {
96 _value = (T) std::stoll(value.asString().substr(2), nullptr, 2);
97 } else if (value.isString() && reg[0].asString() == ICD_File && std::is_base_of<T, std::string>::value) {
98 _value = get_icd_param<T>(param_name);
99 } else if (value.isNumeric()) {
100 std::string value_type = reg[0].asString();
101 if (value_type == ICD_Float) {
102 _value = (T) value.asFloat();
103 } else if (value_type == ICD_Double) {
104 _value = (T) value.asDouble();
105 } else if (value_type == ICD_Uint8) {
106 _value = (T) value.asUInt();
107 } else if (value_type == ICD_Int8) {
108 _value = (T) value.asInt();
109 } else if (value_type == ICD_Uint16) {
110 _value = (T) value.asUInt();
111 } else if (value_type == ICD_Int16) {
112 _value = (T) value.asInt();
113 } else if (value_type == ICD_Uint32) {
114 _value = (T) value.asUInt();
115 } else if (value_type == ICD_Int32) {
116 _value = (T) value.asInt();
117 } else {
118 std::cout << "未识别的值类型 " << value.toStyledString() << std::endl;
119 throw std::runtime_error("未识别的寄存器值类型");
120 }
121 } else {
122 std::cout << "未识别的值类型 " << value.toStyledString() << std::endl;
123 throw std::runtime_error("未识别的寄存器值类型");
124 }
125 return _value;
126 }
127
133 template<>
134 inline std::string JsonWrapper::get_icd_param<std::string>(nsuCSParam_t &param_name) {
135 if (!icdParams->isMember(param_name)) return "";
136 return (*icdParams)[param_name][1].asString();
137 }
138}
139
140#endif //NSUKIT_JSON_ICD_WRAPPER_H
CommandPack * FmtFile(const std::string &file_path, bool need_size=false)
Definition: icd_parser.cpp:299
Json::Reader ICDReader
Json::Value * icdParams
Json::Value * icdCommands
CommandPack * FmtRegister(const Json::Value &reg, const T &value)
nsukitStatus_t set_icd_param(nsuCSParam_t &param_name, const T &value)
T get_icd_param(nsuCSParam_t &param_name)
bool InitICD(std::string_view path)
Definition: icd_parser.cpp:29
#define ICD_Double
Definition: icd_parser.h:22
#define ICD_Int32
Definition: icd_parser.h:20
#define ICD_Float
Definition: icd_parser.h:21
#define ICD_Int8
Definition: icd_parser.h:16
#define ICD_Uint32
Definition: icd_parser.h:19
#define ICD_Int16
Definition: icd_parser.h:18
#define ICD_Uint16
Definition: icd_parser.h:17
#define ICD_File
Definition: icd_parser.h:23
#define ICD_Uint8
Definition: icd_parser.h:15
static std::map< std::string, int > TypeSizeMap
Definition: icd_parser.h:29
NSU_DLLEXPORT bool StringStartWith(const std::string &str, const std::string &head)
Definition: icd_parser.cpp:480
nsukitStatus_t
Definition: type.h:53
DLLEXTERN typedef std::string nsuCSParam_t
Definition: type.h:91