标签:获取 nullptr set str 其他 array das char ash
分享一个类似于Qt中QVariant类。
目录:
1 类型定义
2 数值操作
3 万能类型包装
4 使用
——————————————————Begain——————————————————
类型定义
typedef.h
#ifndef TYPEDEF_H
#define TYPEDEF_H
#include <stdint.h>
#include <set>
using namespace std;
enum class VariantType : uint16_t
{
vt_empty = 0, //!< 类型为定义
vt_bool = 1, //!< bool类型
vt_i4 = 2, //!< int32_t 类型
vt_ui4 = 3, //!< uint32_t 类型
vt_i8 = 4, //!< int64_t 类型
vt_ui8 = 5, //!< uint64_t 类型
vt_r4 = 6, //!< float 类型
vt_r8 = 7, //!< double 类型
vt_bstr = 8, //!< const char* 类型
vt_set = 0x1000, //!< 集合类型,该类型的值需要用过特定函数获取,不能直接通过成员变量获取
};
struct VariantValue
{
uint16_t m_vt; //!< 值得类型,取值参看枚举VariantType,其中vt_set可以与其他值联合使用
union
{
bool m_bVal;
int32_t m_i4Val;
uint32_t m_ui4Val;
int64_t m_i8Val;
uint64_t m_ui8Val;
float m_r4Val;
double m_r8Val;
char* m_strVal;
void* m_pSetVal;
};
};
template<typename T>
void VariantSetValueDelete(set<T> *pSetValue)
{
if (pSetValue)
{
delete pSetValue;
pSetValue = nullptr;
}
}
template<typename T>
void VariantSetValueCopy(VariantValue &toValue, const set<T> *pFromValue)
{
if (nullptr == pFromValue)
return;
toValue.m_pSetVal = new set<T>(*pFromValue);
}
template<typename T>
uint32_t VariantSetValueSize(set<T> *pSetValue)
{
return pSetValue ? static_cast<uint32_t>(pSetValue->size()) : 0;
}
template<typename T>
bool VariantSetValueAt(set<T> *pSetValue, uint32_t uIndex, T &subValue)
{
if (nullptr == pSetValue)
return false;
auto it = pSetValue->begin();
advance(it, uIndex);
if (it == pSetValue->end())
return false;
subValue = *it;
return true;
}
template<typename T>
bool VariantSetValueAdd(set<T> *pSetValue, T value)
{
if (nullptr == pSetValue)
return false;
auto it = pSetValue->find(value);
if (it == pSetValue->end())
return false;
pSetValue->insert(value);
return true;
}
template<typename T>
bool VariantSetValueRemove(set<T> *pSetValue, T value)
{
if (nullptr == pSetValue)
return false;
auto it = pSetValue->find(value);
if (it == pSetValue->end())
return false;
pSetValue->erase(it);
return true;
}
#endif // TYPEDEF_H
数值操作
valueopt.h
#ifndef VALUEOPT_H
#define VALUEOPT_H
#include "typedef.h"
#include <stdint.h>
void VariantValueInit(VariantValue &value);
void VariantValueClear(VariantValue &value);
void VariantValueCopy(VariantValue &toValue, const VariantValue &from);
uint32_t VariantValueSetSize(const VariantValue &valueSet);
bool VariantValueSetAt(const VariantValue &valueSet, uint32_t uIndex, VariantValue &value);
bool VariantValueSetAdd(VariantValue &valueSet, const VariantValue &value);
#endif // VALUEOPT_H
valueopt.cpp
#include <iostream>
#include "valueopt.h"
#include <string.h>
#include <valueopt.h>
using namespace std;
void VariantValueInit(VariantValue &value)
{
memset(&value, 0, sizeof(value));
value.m_vt = (uint16_t)VariantType::vt_empty;
value.m_pSetVal = nullptr;
}
void VariantValueClear(VariantValue &value)
{
if (value.m_vt & (uint16_t)VariantType::vt_set)
{
uint16_t uType = (uint16_t)value.m_vt & 0xff;
switch ((VariantType)uType)
{
case VariantType::vt_bool:
VariantSetValueDelete(reinterpret_cast<set<bool> *>(value.m_pSetVal));
break;
case VariantType::vt_i4:
VariantSetValueDelete(reinterpret_cast<set<int32_t> *>(value.m_pSetVal));
break;
case VariantType::vt_ui4:
VariantSetValueDelete(reinterpret_cast<set<uint32_t> *>(value.m_pSetVal));
break;
case VariantType::vt_i8:
VariantSetValueDelete(reinterpret_cast<set<int64_t> *>(value.m_pSetVal));
break;
case VariantType::vt_ui8:
VariantSetValueDelete(reinterpret_cast<set<uint64_t> *>(value.m_pSetVal));
break;
case VariantType::vt_r4:
VariantSetValueDelete(reinterpret_cast<set<float> *>(value.m_pSetVal));
break;
case VariantType::vt_r8:
VariantSetValueDelete(reinterpret_cast<set<double> *>(value.m_pSetVal));
break;
case VariantType::vt_bstr:
VariantSetValueDelete(reinterpret_cast<set<string> *>(value.m_pSetVal));
break;
default:
break;
}
}
else
{
if (((uint16_t)VariantType::vt_bstr == value.m_vt) && value.m_strVal)
delete[]value.m_strVal;
}
memset(&value, 0, sizeof(value));
value.m_vt = (uint16_t)VariantType::vt_empty;
}
void VariantValueCopy(VariantValue &toValue, const VariantValue &from)
{
VariantValueClear(toValue);
if (from.m_vt & (uint16_t)VariantType::vt_set)
{
uint16_t uType = (uint16_t)from.m_vt & 0xff;
switch ((VariantType)uType)
{
case VariantType::vt_bool:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<bool> *>(from.m_pSetVal));
break;
case VariantType::vt_i4:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<int32_t> *>(from.m_pSetVal));
break;
case VariantType::vt_ui4:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<uint32_t> *>(from.m_pSetVal));
break;
case VariantType::vt_i8:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<int64_t> *>(from.m_pSetVal));
break;
case VariantType::vt_ui8:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<uint64_t> *>(from.m_pSetVal));
break;
case VariantType::vt_r4:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<float> *>(from.m_pSetVal));
break;
case VariantType::vt_r8:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<double> *>(from.m_pSetVal));
break;
case VariantType::vt_bstr:
toValue.m_vt = from.m_vt;
VariantSetValueCopy(toValue, reinterpret_cast<set<string> *>(from.m_pSetVal));
break;
default:
break;
}
}
else
{
if ((uint16_t)VariantType::vt_bstr == from.m_vt)
{
toValue.m_vt = from.m_vt;
if (nullptr != from.m_strVal)
{
uint32_t uLen = strlen(from.m_strVal);
toValue.m_strVal = new char[uLen + 1];
memcpy(toValue.m_strVal, from.m_strVal, uLen);
toValue.m_strVal[uLen] = ‘\0‘;
}
else
{
toValue.m_strVal = nullptr;
}
}
else
{
memcpy(&toValue, &from, sizeof(from));
}
}
}
uint32_t VariantValueSetSize(const VariantValue &valueSet)
{
uint32_t uCount = 0;
if (valueSet.m_vt & (uint16_t)VariantType::vt_set)
{
uint16_t uType = (uint16_t)valueSet.m_vt & 0xff;
switch ((VariantType)uType)
{
case VariantType::vt_bool:
uCount = VariantSetValueSize(reinterpret_cast<set<bool> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_i4:
uCount = VariantSetValueSize(reinterpret_cast<set<int32_t> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_ui4:
uCount = VariantSetValueSize(reinterpret_cast<set<uint32_t> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_i8:
uCount = VariantSetValueSize(reinterpret_cast<set<int64_t> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_ui8:
uCount = VariantSetValueSize(reinterpret_cast<set<uint64_t> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_r4:
uCount = VariantSetValueSize(reinterpret_cast<set<float> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_r8:
uCount = VariantSetValueSize(reinterpret_cast<set<double> *>(valueSet.m_pSetVal));
break;
case VariantType::vt_bstr:
uCount = VariantSetValueSize(reinterpret_cast<set<string> *>(valueSet.m_pSetVal));
break;
default:
break;
}
}
else
{
uCount = 1;
}
return uCount;
}
bool VariantValueSetAt(const VariantValue &valueSet, uint32_t uIndex, VariantValue &value)
{
bool bResult = false;
VariantValueClear(value);
if (valueSet.m_vt & (uint16_t)VariantType::vt_set)
{
VariantValue value;
value.m_vt = (uint16_t)valueSet.m_vt & 0xff;
switch ((VariantType)value.m_vt)
{
case VariantType::vt_bool:
bResult = VariantSetValueAt(reinterpret_cast<set<bool> *>(valueSet.m_pSetVal), uIndex, value.m_bVal);
break;
case VariantType::vt_i4:
bResult = VariantSetValueAt(reinterpret_cast<set<int32_t> *>(valueSet.m_pSetVal), uIndex, value.m_i4Val);
break;
case VariantType::vt_ui4:
bResult = VariantSetValueAt(reinterpret_cast<set<uint32_t> *>(valueSet.m_pSetVal), uIndex, value.m_ui4Val);
break;
case VariantType::vt_i8:
bResult = VariantSetValueAt(reinterpret_cast<set<int64_t> *>(valueSet.m_pSetVal), uIndex, value.m_i8Val);
break;
case VariantType::vt_ui8:
bResult = VariantSetValueAt(reinterpret_cast<set<uint64_t> *>(valueSet.m_pSetVal), uIndex, value.m_ui8Val);
break;
case VariantType::vt_r4:
bResult = VariantSetValueAt(reinterpret_cast<set<float> *>(valueSet.m_pSetVal), uIndex, value.m_r4Val);
break;
case VariantType::vt_r8:
bResult = VariantSetValueAt(reinterpret_cast<set<double> *>(valueSet.m_pSetVal), uIndex, value.m_r8Val);
break;
case VariantType::vt_bstr:
{
string strValue;
bResult = VariantSetValueAt(reinterpret_cast<set<string> *>(valueSet.m_pSetVal), uIndex, strValue);
if (bResult)
{
uint32_t uLen = (uint32_t)strValue.length();
value.m_strVal = new char[uLen + 1];
memcpy(value.m_strVal, strValue.c_str(), uLen);
value.m_strVal[uLen] = ‘\0‘;
}
}
break;
default:
bResult = false;
break;
}
}
else
{
VariantValueCopy(value, valueSet);
}
return bResult;
}
bool VariantValueSetAdd(VariantValue &valueSet, const VariantValue &value)
{
bool bResult = false;
if (valueSet.m_vt == (uint16_t)VariantType::vt_empty)
{
if (value.m_vt == (uint16_t)VariantType::vt_empty)
return false;
valueSet.m_vt = (uint16_t)VariantType::vt_set | (uint16_t)value.m_vt;
}
if (valueSet.m_vt & (uint16_t)VariantType::vt_set)
{
uint16_t uType = (uint16_t)valueSet.m_vt & 0xff;
if (uType != (uint16_t)value.m_vt)
return false;
switch ((VariantType)uType)
{
case VariantType::vt_bool:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<bool>();
bResult = VariantSetValueAdd(reinterpret_cast<set<bool> *>(valueSet.m_pSetVal), value.m_bVal);
break;
case VariantType::vt_i4:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<int32_t>();
bResult = VariantSetValueAdd(reinterpret_cast<set<int32_t> *>(valueSet.m_pSetVal), value.m_i4Val);
break;
case VariantType::vt_ui4:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<uint32_t>();
bResult = VariantSetValueAdd(reinterpret_cast<set<uint32_t> *>(valueSet.m_pSetVal), value.m_ui4Val);
break;
case VariantType::vt_i8:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<int64_t>();
bResult = VariantSetValueAdd(reinterpret_cast<set<int64_t> *>(valueSet.m_pSetVal), value.m_i8Val);
break;
case VariantType::vt_ui8:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<uint64_t>();
bResult = VariantSetValueAdd(reinterpret_cast<set<uint64_t> *>(valueSet.m_pSetVal), value.m_ui8Val);
break;
case VariantType::vt_r4:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<float>();
bResult = VariantSetValueAdd(reinterpret_cast<set<float> *>(valueSet.m_pSetVal), value.m_r4Val);
break;
case VariantType::vt_r8:
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<double>();
bResult = VariantSetValueAdd(reinterpret_cast<set<double> *>(valueSet.m_pSetVal), value.m_r8Val);
break;
case VariantType::vt_bstr:
if (value.m_strVal)
{
if (nullptr == valueSet.m_pSetVal)
valueSet.m_pSetVal = new set<string>();
bResult = VariantSetValueAdd(reinterpret_cast<set<string> *>(valueSet.m_pSetVal), string(value.m_strVal));
}
else
{
bResult = false;
}
break;
default:
bResult = false;
break;
}
}
else
{
bResult = false;
}
return bResult;
}
万能类型包装
CustomVariantValue.h
#ifndef CUSTOMVARIANTVALUE_H
#define CUSTOMVARIANTVALUE_H
#include <iostream>
#include <stdint.h>
#include <set>
#include <vector>
#include <cstring>
#include <typedef.h>
#include <assert.h>
using namespace std;
class CustomVariantValue : private VariantValue
{
public:
CustomVariantValue();
CustomVariantValue(bool bValue);
CustomVariantValue(int32_t nValue);
CustomVariantValue(uint32_t uValue);
CustomVariantValue(int64_t nValue);
CustomVariantValue(uint64_t uValue);
CustomVariantValue(float fValue);
CustomVariantValue(double dValue);
CustomVariantValue(const char *pChar);
CustomVariantValue(const std::string &strChar);
CustomVariantValue(const std::vector<bool> &valueVector);
CustomVariantValue(const std::vector<int32_t> &valueVector);
CustomVariantValue(const std::vector<uint32_t> &valueVector);
CustomVariantValue(const std::vector<int64_t> &valueVector);
CustomVariantValue(const std::vector<uint64_t> &valueVector);
CustomVariantValue(const std::vector<float> &valueVector);
CustomVariantValue(const std::vector<double> &valueVector);
CustomVariantValue(const std::vector<std::string> &valueVector);
CustomVariantValue(const std::set<bool> &valueSet);
CustomVariantValue(const std::set<int32_t> &valueSet);
CustomVariantValue(const std::set<uint32_t> &valueSet);
CustomVariantValue(const std::set<int64_t> &valueSet);
CustomVariantValue(const std::set<uint64_t> &valueSet);
CustomVariantValue(const std::set<float> &valueSet);
CustomVariantValue(const std::set<double> &valueSet);
CustomVariantValue(const std::set<std::string> &valueSet);
CustomVariantValue(const VariantValue &value);
CustomVariantValue(const CustomVariantValue &other);
public:
CustomVariantValue& operator = (bool bValue);
CustomVariantValue& operator = (int32_t nValue);
CustomVariantValue& operator = (uint32_t uValue);
CustomVariantValue& operator = (int64_t nValue);
CustomVariantValue& operator = (uint64_t uValue);
CustomVariantValue& operator = (float fValue);
CustomVariantValue& operator = (double dValue);
CustomVariantValue& operator = (const char *pChar);
CustomVariantValue& operator = (const std::string &strChar);
CustomVariantValue& operator = (const std::vector<bool> &valueVector);
CustomVariantValue& operator = (const std::vector<int32_t> &valueVector);
CustomVariantValue& operator = (const std::vector<uint32_t> &valueVector);
CustomVariantValue& operator = (const std::vector<int64_t> &valueVector);
CustomVariantValue& operator = (const std::vector<uint64_t> &valueVector);
CustomVariantValue& operator = (const std::vector<float> &valueVector);
CustomVariantValue& operator = (const std::vector<double> &valueVector);
CustomVariantValue& operator = (const std::vector<std::string> &valueVector);
CustomVariantValue& operator = (const std::set<bool> &valueSet);
CustomVariantValue& operator = (const std::set<int32_t> &valueSet);
CustomVariantValue& operator = (const std::set<uint32_t> &valueSet);
CustomVariantValue& operator = (const std::set<int64_t> &valueSet);
CustomVariantValue& operator = (const std::set<uint64_t> &valueSet);
CustomVariantValue& operator = (const std::set<float> &valueSet);
CustomVariantValue& operator = (const std::set<double> &valueSet);
CustomVariantValue& operator = (const std::set<std::string> &valueSet);
CustomVariantValue& operator = (const VariantValue &value);
CustomVariantValue& operator = (const CustomVariantValue &other);
bool operator==(const VariantValue& value);
bool operator==(const CustomVariantValue& value);
bool operator!=(const VariantValue& value);
bool operator!=(const CustomVariantValue& value);
operator bool() const
{
assert((uint16_t)VariantType::vt_bool == m_vt);
return m_bVal;
}
operator int32_t() const
{
assert((uint16_t)VariantType::vt_i4 == m_vt);
return m_i4Val;
}
operator uint32_t() const
{
assert((uint16_t)VariantType::vt_ui4 == m_vt);
return m_ui4Val;
}
operator int64_t() const
{
assert((uint16_t)VariantType::vt_i8 == m_vt);
return m_i8Val;
}
operator uint64_t() const
{
assert((uint16_t)VariantType::vt_ui8 == m_vt);
return m_ui4Val;
}
operator float() const
{
assert((uint16_t)VariantType::vt_r4 == m_vt);
return m_r4Val;
}
operator double() const
{
assert((uint16_t)VariantType::vt_r8 == m_vt);
return m_r8Val;
}
operator const char*() const
{
assert((uint16_t)VariantType::vt_bstr == m_vt);
return m_strVal;
}
std::vector<bool> ToBoolArray() const;
std::vector<int32_t> ToInt32Array() const;
std::vector<uint32_t> ToUint32Array() const;
std::vector<int64_t> ToInt64Array() const;
std::vector<uint64_t> ToUint64Array() const;
std::vector<float> ToFloatArray() const;
std::vector<double> ToDoubleArray() const;
std::vector<std::string> ToStringArray() const;
std::set<bool>& ToBoolSet();
std::set<int32_t>& ToInt32Set();
std::set<uint32_t>& ToUint32Set();
std::set<int64_t>& ToInt64Set();
std::set<uint64_t>& ToUint64Set();
std::set<float>& ToFloatSet();
std::set<double>& ToDoubleSet();
std::set<std::string>& ToStringSet();
private:
private:
bool Compare(const VariantValue& value);
};
#endif // CUSTOMVARIANTVALUE_H
CustomVariantValue.cpp
#include "customvariantvalue.h"
#include "valueopt.h"
namespace
{
template<typename T>
bool VariantSetValueComplie(std::set<T> *pSetValue1, std::set<T> *pSetValue2)
{
if ((nullptr == pSetValue1) && (nullptr == pSetValue2))
return true;
if ((nullptr == pSetValue1) || (nullptr == pSetValue2))
return false;
return (*pSetValue1) == (*pSetValue2);
}
}
CustomVariantValue::CustomVariantValue()
{
VariantValueInit(*this);
}
CustomVariantValue::CustomVariantValue(bool bValue)
{
m_vt = (uint16_t)VariantType::vt_bool;
m_bVal = bValue;
}
CustomVariantValue::CustomVariantValue(int32_t nValue)
{
m_vt = (uint16_t)VariantType::vt_i4;
m_i4Val = nValue;
}
CustomVariantValue::CustomVariantValue(uint32_t uValue)
{
m_vt = (uint16_t)VariantType::vt_ui4;
m_ui4Val = uValue;
}
CustomVariantValue::CustomVariantValue(int64_t nValue)
{
m_vt = (uint16_t)VariantType::vt_i8;
m_i8Val = nValue;
}
CustomVariantValue::CustomVariantValue(uint64_t uValue)
{
m_vt = (uint16_t)VariantType::vt_ui8;
m_ui8Val = uValue;
}
CustomVariantValue::CustomVariantValue(float fValue)
{
m_vt = (uint16_t)VariantType::vt_r4;
m_r4Val = fValue;
}
CustomVariantValue::CustomVariantValue(double dValue)
{
m_vt = (uint16_t)VariantType::vt_r8;
m_r8Val = dValue;
}
CustomVariantValue::CustomVariantValue(const char *pChar)
{
m_vt = (uint16_t)VariantType::vt_bstr;
if (nullptr != pChar)
{
uint32_t uLen = strlen(pChar);
m_strVal = new char[uLen + 1];
memcpy(m_strVal, pChar, uLen);
m_strVal[uLen] = ‘\0‘;
}
else
{
m_strVal = nullptr;
}
}
CustomVariantValue::CustomVariantValue(const std::string &strChar)
{
uint32_t uLen = static_cast<uint32_t>(strChar.length());
m_vt = (uint16_t)VariantType::vt_bstr;
if (0 != uLen)
{
m_strVal = new char[uLen + 1];
memcpy(m_strVal, strChar.c_str(), uLen);
m_strVal[uLen] = ‘\0‘;
}
else
{
m_strVal = nullptr;
}
}
CustomVariantValue::CustomVariantValue(const std::vector<bool> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_bool | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<bool>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<int32_t> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_i4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<int32_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<uint32_t> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_ui4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<uint32_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<int64_t> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_i8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<int64_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<uint64_t> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_ui8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<uint64_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<float> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_r4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<float>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<double> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_r8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<double>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::vector<std::string> &valueVector)
{
m_vt = (uint16_t)VariantType::vt_bstr | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<std::string>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
}
CustomVariantValue::CustomVariantValue(const std::set<bool> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_bool | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<bool>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<int32_t> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_i4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<int32_t>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<uint32_t> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_ui4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<uint32_t>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<int64_t> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_i8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<int64_t>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<uint64_t> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_ui8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<uint64_t>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<float> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_r4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<float>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<double> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_r8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<double>(valueSet);
}
CustomVariantValue::CustomVariantValue(const std::set<std::string> &valueSet)
{
m_vt = (uint16_t)VariantType::vt_bstr | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<std::string>(valueSet);
}
CustomVariantValue::CustomVariantValue(const VariantValue &value)
{
VariantValueInit(*this);
VariantValueCopy(*this, value);
}
CustomVariantValue::CustomVariantValue(const CustomVariantValue &other)
{
VariantValueInit(*this);
VariantValueCopy(*this, other);
}
CustomVariantValue& CustomVariantValue::operator = (bool bValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bool;
m_bVal = bValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (int32_t nValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i4;
m_i4Val = nValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (uint32_t uValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui4;
m_ui4Val = uValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (int64_t nValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i8;
m_i8Val = nValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (uint64_t uValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui8;
m_ui8Val = uValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (float fValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r4;
m_r4Val = fValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (double dValue)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r8;
m_r8Val = dValue;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const char *pChar)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bstr;
if (nullptr != pChar)
{
uint32_t uLen = strlen(pChar);
m_strVal = new char[uLen + 1];
memcpy(m_strVal, pChar, uLen);
m_strVal[uLen] = ‘\0‘;
}
else
{
m_strVal = nullptr;
}
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::string &strChar)
{
VariantValueClear(*this);
uint32_t uLen = static_cast<uint32_t>(strChar.length());
m_vt = (uint16_t)VariantType::vt_bstr;
if (0 != uLen)
{
m_strVal = new char[uLen + 1];
memcpy(m_strVal, strChar.c_str(), uLen);
m_strVal[uLen] = ‘\0‘;
}
else
{
m_strVal = nullptr;
}
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<bool> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bool | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<bool>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<int32_t> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<int32_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<uint32_t> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<uint32_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<int64_t> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<int64_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<uint64_t> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<uint64_t>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<float> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r4 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<float>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<double> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r8 | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<double>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::vector<std::string> &valueVector)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bstr | (uint16_t)VariantType::vt_set;
auto pSetVal = new std::set<std::string>();
for (auto value : valueVector)
pSetVal->insert(value);
m_pSetVal = pSetVal;
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<bool> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bool | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<bool>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<int32_t> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<int32_t>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<uint32_t> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<uint32_t>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<int64_t> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_i8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<int64_t>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<uint64_t> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_ui8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<uint64_t>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<float> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r4 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<float>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<double> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_r8 | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<double>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const std::set<std::string> &valueSet)
{
VariantValueClear(*this);
m_vt = (uint16_t)VariantType::vt_bstr | (uint16_t)VariantType::vt_set;
m_pSetVal = new std::set<std::string>(valueSet);
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const VariantValue &value)
{
if (this != &value)
{
VariantValueClear(*this);
VariantValueCopy(*this, value);
}
return *this;
}
CustomVariantValue& CustomVariantValue::operator = (const CustomVariantValue &other)
{
if (this != &other)
{
VariantValueClear(*this);
VariantValueCopy(*this, other);
}
return *this;
}
bool CustomVariantValue::operator==(const VariantValue& value)
{
return Compare(value);
}
bool CustomVariantValue::operator==(const CustomVariantValue& value)
{
return Compare(value);
}
bool CustomVariantValue::operator!=(const VariantValue& value)
{
return !Compare(value);
}
bool CustomVariantValue::operator!=(const CustomVariantValue& value)
{
return !Compare(value);
}
std::vector<bool> CustomVariantValue::ToBoolArray() const
{
std::vector<bool> arrayValue;
std::set<bool> *pSetVal = static_cast<std::set<bool> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_bool) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<int32_t> CustomVariantValue::ToInt32Array() const
{
std::vector<int32_t> arrayValue;
std::set<int32_t> *pSetVal = static_cast<std::set<int32_t> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_i4) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<uint32_t> CustomVariantValue::ToUint32Array() const
{
std::vector<uint32_t> arrayValue;
std::set<uint32_t> *pSetVal = static_cast<std::set<uint32_t> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_ui4) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<int64_t> CustomVariantValue::ToInt64Array() const
{
std::vector<int64_t> arrayValue;
std::set<int64_t> *pSetVal = static_cast<std::set<int64_t> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_i8) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<uint64_t> CustomVariantValue::ToUint64Array() const
{
std::vector<uint64_t> arrayValue;
std::set<uint64_t> *pSetVal = static_cast<std::set<uint64_t> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_ui8) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<float> CustomVariantValue::ToFloatArray() const
{
std::vector<float> arrayValue;
std::set<float> *pSetVal = static_cast<std::set<float> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_r4) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<double> CustomVariantValue::ToDoubleArray() const
{
std::vector<double> arrayValue;
std::set<double> *pSetVal = static_cast<std::set<double> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_r8) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::vector<std::string> CustomVariantValue::ToStringArray() const
{
std::vector<std::string> arrayValue;
std::set<std::string> *pSetVal = static_cast<std::set<std::string> *>(m_pSetVal);
if (pSetVal)
{
uint32_t i = 0;
assert(m_vt == (((uint16_t)VariantType::vt_bstr) | (uint16_t)VariantType::vt_set));
arrayValue.resize(pSetVal->size());
for (auto subValue : *pSetVal)
arrayValue[i++] = subValue;
}
else
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
}
return arrayValue;
}
std::set<bool>& CustomVariantValue::ToBoolSet()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_bool) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<bool>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_bool) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<bool> *>(m_pSetVal);
}
std::set<int32_t>& CustomVariantValue::ToInt32Set()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_i4) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<int32_t>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_i4) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<int32_t> *>(m_pSetVal);
}
std::set<uint32_t>& CustomVariantValue::ToUint32Set()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_ui4) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<uint32_t>();
}
else
{
assert((m_vt == (((uint16_t)VariantType::vt_ui4) | (uint16_t)VariantType::vt_set)));
}
return *static_cast<std::set<uint32_t> *>(m_pSetVal);
}
std::set<int64_t>& CustomVariantValue::ToInt64Set()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_i8) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<int64_t>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_i8) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<int64_t> *>(m_pSetVal);
}
std::set<uint64_t>& CustomVariantValue::ToUint64Set()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_ui8) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<uint64_t>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_ui8) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<uint64_t> *>(m_pSetVal);
}
std::set<float>& CustomVariantValue::ToFloatSet()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_r4) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<float>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_r4) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<float> *>(m_pSetVal);
}
std::set<double>& CustomVariantValue::ToDoubleSet()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_r8) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<double>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_r8) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<double> *>(m_pSetVal);
}
std::set<std::string>& CustomVariantValue::ToStringSet()
{
if (nullptr == m_pSetVal)
{
assert(m_vt == (uint16_t)VariantType::vt_empty);
m_vt = (((uint16_t)VariantType::vt_bstr) | (uint16_t)VariantType::vt_set);
m_pSetVal = new std::set<std::string>();
}
else
{
assert(m_vt == (((uint16_t)VariantType::vt_bstr) | (uint16_t)VariantType::vt_set));
}
return *static_cast<std::set<std::string> *>(m_pSetVal);
}
bool CustomVariantValue::Compare(const VariantValue& value)
{
if (m_vt != value.m_vt)
return false;
if (m_vt & (uint16_t)VariantType::vt_set)
{
uint16_t uType = (uint16_t)value.m_vt & 0xff;
bool bResult = false;
switch ((VariantType)uType)
{
case VariantType::vt_bool:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<bool> *>(m_pSetVal), reinterpret_cast<std::set<bool> *>(value.m_pSetVal));
break;
case VariantType::vt_i4:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<int32_t> *>(m_pSetVal), reinterpret_cast<std::set<int32_t> *>(value.m_pSetVal));
break;
case VariantType::vt_ui4:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<uint32_t> *>(m_pSetVal), reinterpret_cast<std::set<uint32_t> *>(value.m_pSetVal));
break;
case VariantType::vt_i8:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<int64_t> *>(m_pSetVal), reinterpret_cast<std::set<int64_t> *>(value.m_pSetVal));
break;
case VariantType::vt_ui8:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<uint64_t> *>(m_pSetVal), reinterpret_cast<std::set<uint64_t> *>(value.m_pSetVal));
break;
case VariantType::vt_r4:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<float> *>(m_pSetVal), reinterpret_cast<std::set<float> *>(value.m_pSetVal));
break;
case VariantType::vt_r8:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<double> *>(m_pSetVal), reinterpret_cast<std::set<double> *>(value.m_pSetVal));
break;
case VariantType::vt_bstr:
bResult = VariantSetValueComplie(reinterpret_cast<std::set<std::string> *>(m_pSetVal), reinterpret_cast<std::set<std::string> *>(value.m_pSetVal));
break;
default:
assert(false);
break;
}
return bResult;
}
else
{
if (m_vt != (uint16_t)VariantType::vt_bstr)
{
return 0 == memcmp(this, &value, sizeof(value));
}
else
{
if ((nullptr == m_strVal) && (nullptr == value.m_strVal))
return true;
if ((nullptr == m_strVal) || (nullptr == value.m_strVal))
return false;
return 0 == strcmp(m_strVal, value.m_strVal);
}
}
}
使用
main.cpp
#include <iostream> #include <CustomVariantValue.h> using namespace std; int main(int argc, char *argv[]) { CustomVariantValue vInt = 1; CustomVariantValue vBool = true; int rvInt = vInt; cout<<rvInt<<endl; if(vInt == vBool) cout<<"vInt == vBool"<<endl; else cout<<"vInt!=vBool"<<endl; return 0; }
——
标签:获取 nullptr set str 其他 array das char ash
原文地址:https://www.cnblogs.com/wangkeqin/p/12024348.html