6 Star 4 Fork 2

GPLme / SG-Database

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
typeHelper.hpp 4.20 KB
一键复制 编辑 原始数据 按行查看 历史
ECIR 提交于 2021-04-07 15:50 . 存储时将,转义
#pragma once
#include "basicType.h"
#include <vector>
#include <set>
#include <algorithm>
class typeHelper:public QObject
{
Q_OBJECT
typeHelper(){}
public:
static typeHelper* typehelper;
static typeHelper* getTypeHelper(){
if(typehelper==nullptr){
typehelper=new typeHelper () ;
}
return typehelper;
}
Q_INVOKABLE Basic* copy(Basic* v)
{
//调用前应该对参数类型进行检查
if(v->getType()==INT)
return new Int(((Int*)(v))->val);
if(v->getType()==FLOAT)
return new Float(((Float*)(v))->val);
if(v->getType()==BOOL)
return new Bool(((Bool*)(v))->val);
if(v->getType()==STR)
return new Str(((Str*)(v))->val);
if(v->getType()==_NULL)
return new Basic();
if(v->getType()==PLACEHOLDER)
return new Placeholder();
return nullptr; //如果进行参数检查了不会走到这一步
}
#define CONVCMP(strType,type) if(v1->getType()==strType) { \
auto nv1=((type*)(v1));\
auto nv2=((type*)(v2));\
return nv1->val==nv2->val;\
}
Q_INVOKABLE bool isEqu(Basic* v1, Basic* v2)
{
if(v1->getType()!=v2->getType())
return false;
{
CONVCMP(INT,Int)
CONVCMP(FLOAT,Float)
CONVCMP(BOOL,Bool)
CONVCMP(STR,Str)
return false;
}
}
TYPE judgeType(string& val){
if(val=="NULL"){
return _NULL;
}
else if(val=="placeholder"){
return PLACEHOLDER;
}
else if(val=="true"||val=="false"){
return BOOL;
}
else if(val[0]=='\''&&val[val.size()-1]=='\''){
val=val.substr(1,val.size()-2);
return STR;
}
for(const char& c:val){
if(c=='.'){
return FLOAT;
}
}
return INT;
}
Q_INVOKABLE Basic* strToBasic(const QString& val);
Basic* strToBasic(const string& val, TYPE type) //val需要与Basic.toStr结果对应
{
if(type==_NULL)
return new Basic();
if(type==PLACEHOLDER)
return new Placeholder();
if(type==INT)
return new Int(stoi(val));
if(type==FLOAT)
return new Float(stof(val));
if(type==STR)
return new Str(val);
if(type==BOOL)
{
if(val=="true")
return new Bool(true);
else
return new Bool(false);
}
return nullptr;
}
};
class helper
{
public:
template<class T>
static int find(const vector<T> &invec, int value)
{
int low = 0, high = invec.size()-1;
//assert(!invec.empty() && pos>=0);
while(low <=high)
{
int mid = (low+high)/2;
if(invec[mid] == value)
return mid;
else if(invec[mid] < value)
low = mid+1;
else
high = mid-1;
}
return -1;
}
static void sort(vector<int> &vec)
{
std::sort(vec.begin(),vec.end());
}
static vector<int> getRange(int start,int end)
{
vector<int> result;
for(int i=start;i<=end;i++)
result.push_back(i);
return result;
}
static string toEscapeStr(const string& data){
string newData;
for(int i=0;i<data.length();++i){
char tmpChar=data[i];
if(tmpChar==','){
newData.append("/,");
continue;
}
newData.push_back(tmpChar);
}
return newData;
}
static string reEscape(const string& data){
string newData;
newData.push_back(data[0]);
for(int i=1;i<data.length();++i){
char tmpChar=data[i];
if(tmpChar==','&&data[i-1]=='/'){
newData.push_back(',');
continue;
}
newData.push_back(tmpChar);
}
return newData;
}
static void markInd(vector<char>& allInds,const vector<char>& tmpInd){
for(int i=0;i<allInds.size();++i){
if(tmpInd[i])
allInds[i]+=1;
}
}
};
C++
1
https://gitee.com/sg-first/SG-Database.git
git@gitee.com:sg-first/SG-Database.git
sg-first
SG-Database
SG-Database
master

搜索帮助