302 lines
6.6 KiB
C++
302 lines
6.6 KiB
C++
#pragma once
|
|
#include <set>
|
|
#include <vector>
|
|
#include "str_convert.h"
|
|
|
|
#if ARX > 25
|
|
#include "AecDbObjXDataMap.h"
|
|
#else
|
|
class AecDbObjXDataMap;
|
|
#endif
|
|
|
|
namespace SimpleProperty
|
|
{
|
|
enum PropertyType
|
|
{
|
|
eInt = 0,
|
|
eDouble,
|
|
eString,
|
|
};
|
|
|
|
struct Property
|
|
{
|
|
|
|
CString m_sPropertyName; // 属性名称
|
|
int m_iIndex; // 属性索引
|
|
bool m_bCanModify; // 属性是否可修改
|
|
PropertyType m_type; // 属性值类型
|
|
|
|
// 属性值
|
|
int m_iValueInt; // 整型值
|
|
double m_dValueDouble; // 双精度值
|
|
CString m_sValueString; // 字符串值
|
|
|
|
std::vector<CString> m_arOptions; // 可选值列表
|
|
|
|
CString m_sUIContent; // 在界面上显示的内容
|
|
bool m_bIsChange; // 是否被用户修改?
|
|
int m_iColor; // 在界面上显示的颜色,-1表示默认颜色
|
|
|
|
Property()
|
|
: m_iIndex(0)
|
|
, m_bCanModify(true)
|
|
, m_type(eInt)
|
|
, m_iValueInt(0)
|
|
, m_dValueDouble(0.0)
|
|
, m_sValueString(_T(""))
|
|
, m_sPropertyName(_T(""))
|
|
, m_bIsChange(false)
|
|
, m_sUIContent(_T(""))
|
|
, m_iColor(-1)
|
|
{ }
|
|
|
|
Property(LPCTSTR pszPropertyName)
|
|
: m_sPropertyName(pszPropertyName)
|
|
, m_iIndex(0)
|
|
, m_bCanModify(true)
|
|
, m_type(eInt)
|
|
, m_iValueInt(0)
|
|
, m_dValueDouble(0.0)
|
|
, m_sValueString(_T(""))
|
|
, m_bIsChange(false)
|
|
, m_sUIContent(_T(""))
|
|
, m_iColor(-1)
|
|
{ }
|
|
|
|
Property(LPCTSTR pszPropertyName
|
|
, const PropertyType &type
|
|
, const bool &bCanModify
|
|
, const int &iValueInt
|
|
, const double &dValueDouble
|
|
, const CString &sValueString)
|
|
: m_sPropertyName(pszPropertyName)
|
|
, m_iIndex(0)
|
|
, m_bCanModify(bCanModify)
|
|
, m_type(type)
|
|
, m_iValueInt(iValueInt)
|
|
, m_dValueDouble(dValueDouble)
|
|
, m_sValueString(sValueString)
|
|
, m_bIsChange(false)
|
|
, m_sUIContent(_T(""))
|
|
, m_iColor(-1)
|
|
{ }
|
|
|
|
template <typename T>
|
|
inline bool getAt(T &value) const
|
|
{
|
|
return false;
|
|
}
|
|
|
|
template <>
|
|
inline bool getAt<int>(int &value) const
|
|
{
|
|
if (m_type != eInt)
|
|
return false;
|
|
return StrConverter::ToInt(m_sUIContent, value);
|
|
}
|
|
|
|
template <>
|
|
inline bool getAt<double>(double &value) const
|
|
{
|
|
if (m_type != eDouble)
|
|
return false;
|
|
return StrConverter::ToDouble(m_sUIContent, value);
|
|
}
|
|
|
|
template <>
|
|
inline bool getAt<CString>(CString &value) const
|
|
{
|
|
if (m_type != eString)
|
|
return false;
|
|
value = m_sUIContent;
|
|
return true;
|
|
}
|
|
|
|
};
|
|
|
|
struct compare_properties
|
|
{
|
|
bool operator ()(const Property &lhs, const Property &rhs) const
|
|
{
|
|
// 先比名称
|
|
int nrel = lhs.m_sPropertyName.Compare(rhs.m_sPropertyName);
|
|
if (nrel != 0)
|
|
return lhs.m_sPropertyName < rhs.m_sPropertyName;
|
|
|
|
// 名称一样,比类型
|
|
if (lhs.m_type != rhs.m_type)
|
|
return lhs.m_type < rhs.m_type;
|
|
|
|
// 类型一样,判断一下 arOption
|
|
// 名称一样,类型一样,arOptions的长度一样,99%是一样的属性了
|
|
return lhs.m_arOptions.size() < rhs.m_arOptions.size();
|
|
}
|
|
};
|
|
|
|
struct compare_by_name
|
|
{
|
|
bool operator ()(const Property &lhs, const Property &rhs) const
|
|
{
|
|
return lhs.m_sPropertyName.Compare(rhs.m_sPropertyName) < 0;
|
|
}
|
|
};
|
|
|
|
struct compare_by_index
|
|
{
|
|
bool operator ()(const Property &lhs, const Property &rhs) const
|
|
{
|
|
return lhs.m_iIndex < rhs.m_iIndex;
|
|
}
|
|
};
|
|
|
|
struct PropertySetByIndex
|
|
{
|
|
typedef std::set<Property, compare_by_index> SetType;
|
|
SetType m_set;
|
|
inline bool find(LPCTSTR name, const Property *&prop) const
|
|
{
|
|
SetType::const_iterator it = m_set.begin();
|
|
for (; it != m_set.end(); ++it)
|
|
{
|
|
if (it->m_sPropertyName == name)
|
|
{
|
|
prop = &(*it);
|
|
return true;
|
|
}
|
|
}
|
|
prop = NULL;
|
|
return false;
|
|
}
|
|
};
|
|
|
|
struct PropertySetByName
|
|
{
|
|
typedef std::set<Property, compare_by_name> SetType;
|
|
SetType m_set;
|
|
|
|
inline bool find(LPCTSTR name, const Property *&prop) const
|
|
{
|
|
SetType::const_iterator it = m_set.find(Property(name));
|
|
if (it != m_set.end())
|
|
{
|
|
prop = &(*it);
|
|
return true;
|
|
}
|
|
prop = NULL;
|
|
return false;
|
|
}
|
|
|
|
template <typename T>
|
|
inline bool findAndSet(LPCTSTR name, T &v)
|
|
{
|
|
const Property *pProp(NULL);
|
|
if (find(name, pProp) && pProp->getAt(v))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
template <typename T>
|
|
inline bool findAndSet(LPCTSTR name, AecDbObjXDataMap *pData, LPCTSTR key)
|
|
{
|
|
const Property *pProp(NULL);
|
|
T v;
|
|
if (find(name, pProp) && pProp->getAt(v))
|
|
{
|
|
pData->SetAt(key, v);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template <int N>
|
|
inline bool findAndSet(LPCTSTR name, TCHAR(&va)[N])
|
|
{
|
|
const Property *pProp(NULL);
|
|
CString s;
|
|
if (find(name, pProp) && pProp->getAt(s))
|
|
{
|
|
_tcsncpy_s(va, N, s, N - 1);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
|
|
template <typename T>
|
|
static Property makeProperty(LPCTSTR pszPropertyName
|
|
, const T &value
|
|
, const bool &bCanMoidfy)
|
|
{
|
|
return Property();
|
|
}
|
|
|
|
// 针对 int 的重载 (普通函数)
|
|
inline Property makeProperty(LPCTSTR pszPropertyName, int value, const bool &bCanModify)
|
|
{
|
|
return Property(pszPropertyName, eInt, bCanModify, value, 0., _T(""));
|
|
}
|
|
|
|
// 针对 double 的重载 (普通函数)
|
|
inline Property makeProperty(LPCTSTR pszPropertyName, double value, const bool &bCanModify)
|
|
{
|
|
return Property(pszPropertyName, eDouble, bCanModify, 0, value, _T(""));
|
|
}
|
|
|
|
// 针对 CString 的重载 (普通函数)
|
|
inline Property makeProperty(LPCTSTR pszPropertyName, const CString &value, const bool &bCanModify)
|
|
{
|
|
return Property(pszPropertyName, eString, bCanModify, 0, 0., value);
|
|
}
|
|
|
|
// 针对 LPCTSTR (const TCHAR*) 的重载 (普通函数)
|
|
inline Property makeProperty(LPCTSTR pszPropertyName, LPCTSTR value, const bool &bCanModify)
|
|
{
|
|
return Property(pszPropertyName, eString, bCanModify, 0, 0., value);
|
|
}
|
|
|
|
template <int N>
|
|
static Property makeProperty(LPCTSTR pszPropertyName
|
|
, const TCHAR(&value)[N]
|
|
, const bool &bCanMoidfy)
|
|
{
|
|
return Property(pszPropertyName, eString, bCanMoidfy, 0, 0., value);
|
|
}
|
|
|
|
template <typename T>
|
|
inline bool makeAndInsert(PropertySetByIndex &ss
|
|
, LPCTSTR pszPropertyName
|
|
, const T &value
|
|
, const bool &bCanMoidfy
|
|
, const std::vector<CString> &arOptions
|
|
, const int &clr = -1)
|
|
{
|
|
Property prop = makeProperty(pszPropertyName, value, bCanMoidfy);
|
|
prop.m_iIndex = (int)ss.m_set.size();
|
|
prop.m_arOptions = arOptions;
|
|
prop.m_iColor = clr;
|
|
return ss.m_set.insert(prop).second;
|
|
}
|
|
|
|
template <typename T>
|
|
inline bool makeAndInsert(PropertySetByIndex &ss
|
|
, LPCTSTR pszPropertyName
|
|
, const T &value
|
|
, const bool &bCanMoidfy
|
|
, const int &clr = -1)
|
|
{
|
|
Property prop = makeProperty(pszPropertyName, value, bCanMoidfy);
|
|
prop.m_iIndex = (int)ss.m_set.size();
|
|
prop.m_iColor = clr;
|
|
return ss.m_set.insert(prop).second;
|
|
}
|
|
|
|
inline void convertSetByName(const PropertySetByIndex &ss, PropertySetByName &yy)
|
|
{
|
|
yy.m_set.clear();
|
|
yy.m_set.insert(ss.m_set.begin(), ss.m_set.end());
|
|
}
|
|
};
|
|
|
|
|
|
|