Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Генерация кода C++. Описание структуры на C++ и оператора ее сравнения не пишется программистом вручную, а генерируется скриптом на основе описания структуры на каком-то другом входном языке. Данный подход, с моей точки зрения, является идеальным...
DECLARE_MEMBER_PARAMSTRUCT(double, karma);
double karma;
#define GETSET(type, name, name2) \
private: type name; \
public: TypeTraits<type>::ParameterType get##name2() const { return name; } \
void set##name2(TypeTraits<type>::ParameterType a##name2) { name = a##name2; }
bool testclass::operator==(testclass& comp)
{
int s = sizeof(*this);
for (int i = 0; i < s; i+=4)
{
if (*(int*)((int)this + i) != *(int*)((int)&comp + i)) return false;
}
return true;
}
bool testclass::operator==(testclass& comp)
{
int s = sizeof(*this);
for (int i = 0; i < s; i+=4)
{
if (*(int*)((int)this + i) != *(int*)((int)&comp + i)) return false;
}
return true;
}
struct MyClass {
char x;
int y;
};
struct MANPARAMS
{
std::string name;
int age;
std::vector<std::string> friend_names;
double karma;
GENERATE_COMPARE_FUNC(name, age, friend_names, karma);
};
struct point_type {
int x, y;
};
— сколько ваше решение потребует выделений/освобождений памяти.struct point_type {
int x, y;
bool operator==(const point_type& other) {
return x==other.x && y==other.y;
}
};
template<typename T>
class CValue : public IValue
{...};
class CMyStruct
{
public:
CValue<int> id;
CValue<std::string> name;
private:
std::vector<IValue*> m_values;
};
template<typename T>
class CValue : public IValue
{
public:
CValue& operator=(const T& rhs)
{
m_value = rhs;
return *this;
}
operator T()const
{
return m_value;
}
};
std::map<DWORD, CStructBase*> g_mapByThreadId;
CCriticalSection g_cs;
class IValue
{};
class CStructBase
{
public:
CStructBase()
{
CScopeLock scopeLock(g_cs);
g_mapByThreadId[GetCurrentThreadId()] = this;
}
static void RegisterValue(IValue* value)
{
CScopeLock scopeLock(g_cs);
g_mapByThreadId[GetCurrentThreadId()]->m_values.push_back(value);
}
private:
std::vector<IValue*> m_values;
};
class CValue : public IValue
{
public:
CValue()
{
CStructBase::RegisterValue(this);
}
};
class CMyStruct : public CStructBase
{
public:
CValue<int> id;
CValue<std::string> name;
};
Автоматическая генерация операторов сравнения структур в C++