Codegen. Better DTO.

This commit is contained in:
lganzzzo 2020-05-15 04:31:54 +03:00
parent b209cc2684
commit 337fb49835
3 changed files with 63 additions and 36 deletions

View File

@ -77,41 +77,53 @@ public: \
#define OATPP_MACRO_DTO_FIELD_1(TYPE, NAME) \
\
oatpp::data::mapping::type::Type::Property* Z__CLASS_FIELD_##NAME = \
Z__CLASS_GET_FIELD_##NAME(static_cast<oatpp::base::Countable*>(this), \
(oatpp::data::mapping::type::Void*)(&NAME)); \
\
static oatpp::data::mapping::type::Type::Property* \
Z__CLASS_GET_FIELD_##NAME(oatpp::base::Countable* _this, \
oatpp::data::mapping::type::Void* _reg) { \
static oatpp::data::mapping::type::Type::Property* field = \
new oatpp::data::mapping::type::Type::Property(Z__CLASS_GET_FIELDS_MAP(), \
(v_int64) _reg - (v_int64) _this, \
#NAME, \
TYPE::__Wrapper::Class::getType()); \
return field; \
static v_int64 Z__PROPERTY_OFFSET_##NAME() { \
char buffer[sizeof(Z__CLASS)]; \
auto obj = static_cast<Z__CLASS*>((void*)buffer); \
auto ptr = &obj->NAME; \
return (v_int64) ptr - (v_int64) buffer; \
} \
\
TYPE::__Wrapper NAME
static oatpp::data::mapping::type::Type::Property* Z__PROPERTY_SINGLETON_##NAME() { \
static oatpp::data::mapping::type::Type::Property* property = \
new oatpp::data::mapping::type::Type::Property(Z__PROPERTY_OFFSET_##NAME(), \
#NAME, \
TYPE::__Wrapper::Class::getType()); \
return property; \
} \
\
static TYPE::__Wrapper Z__PROPERTY_INITIALIZER_PROXY_##NAME() { \
static oatpp::data::mapping::type::Type::Property* property = \
Z__CLASS_GET_FIELDS_MAP()->pushBack(Z__PROPERTY_SINGLETON_##NAME()); \
return TYPE::__Wrapper(); \
} \
\
TYPE::__Wrapper NAME = Z__PROPERTY_INITIALIZER_PROXY_##NAME()
#define OATPP_MACRO_DTO_FIELD_2(TYPE, NAME, QUALIFIER) \
\
oatpp::data::mapping::type::Type::Property* Z__CLASS_FIELD_##NAME = \
Z__CLASS_GET_FIELD_##NAME(static_cast<oatpp::base::Countable*>(this), \
(oatpp::data::mapping::type::Void*)(&NAME)); \
\
static oatpp::data::mapping::type::Type::Property* \
Z__CLASS_GET_FIELD_##NAME(oatpp::base::Countable* _this, \
oatpp::data::mapping::type::Void* _reg) { \
static oatpp::data::mapping::type::Type::Property* field = \
new oatpp::data::mapping::type::Type::Property(Z__CLASS_GET_FIELDS_MAP(), \
(v_int64) _reg - (v_int64) _this, \
QUALIFIER, \
TYPE::__Wrapper::Class::getType()); \
return field; \
static v_int64 Z__PROPERTY_OFFSET_##NAME() { \
char buffer[sizeof(Z__CLASS)]; \
auto obj = static_cast<Z__CLASS*>((void*)buffer); \
auto ptr = &obj->NAME; \
return (v_int64) ptr - (v_int64) buffer; \
} \
\
TYPE::__Wrapper NAME
static oatpp::data::mapping::type::Type::Property* Z__PROPERTY_SINGLETON_##NAME() { \
static oatpp::data::mapping::type::Type::Property* property = \
new oatpp::data::mapping::type::Type::Property(Z__PROPERTY_OFFSET_##NAME(), \
QUALIFIER, \
TYPE::__Wrapper::Class::getType()); \
return property; \
} \
\
static TYPE::__Wrapper Z__PROPERTY_INITIALIZER_PROXY_##NAME() { \
static oatpp::data::mapping::type::Type::Property* property = \
Z__CLASS_GET_FIELDS_MAP()->pushBack(Z__PROPERTY_SINGLETON_##NAME()); \
return TYPE::__Wrapper(); \
} \
\
TYPE::__Wrapper NAME = Z__PROPERTY_INITIALIZER_PROXY_##NAME()
/**
* Codegen macro to generate fields of DTO object.

View File

@ -56,9 +56,10 @@ int ClassId::getClassCount() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Type::Properties
void Type::Properties::pushBack(Property* property) {
Type::Property* Type::Properties::pushBack(Property* property) {
m_map.insert({property->name, property});
m_list.push_back(property);
return property;
}
void Type::Properties::pushFrontAll(Properties* properties) {
@ -69,13 +70,11 @@ void Type::Properties::pushFrontAll(Properties* properties) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Type::Property
Type::Property::Property(Properties* properties, v_int64 pOffset, const char* pName, Type* pType)
Type::Property::Property(v_int64 pOffset, const char* pName, Type* pType)
: offset(pOffset)
, name(pName)
, type(pType)
{
properties->pushBack(this);
}
{}
void Type::Property::set(void* object, const Void& value) {
Void* property = (Void*)(((v_int64) object) + offset);

View File

@ -245,7 +245,7 @@ public:
* Add property to the end of the list.
* @param property
*/
void pushBack(Property* property);
Property* pushBack(Property* property);
/**
* Add all properties to the beginning of the list.
@ -277,18 +277,29 @@ public:
* Class to map object properties.
*/
class Property {
public:
/**
* Editional Info about Property.
*/
struct Info {
/**
* Description.
*/
std::string description = "";
};
private:
const v_int64 offset;
public:
/**
* Constructor.
* @param properties - &l:Type::Properties;*. to push this property to.
* @param pOffset - memory offset of object field from object start address.
* @param pName - name of the property.
* @param pType - &l:Type; of the property.
*/
Property(Properties* properties, v_int64 pOffset, const char* pName, Type* pType);
Property(v_int64 pOffset, const char* pName, Type* pType);
/**
* Property name.
@ -300,6 +311,11 @@ public:
*/
const Type* const type;
/**
* Property additional info.
*/
Info info;
/**
* Set value of object field mapped by this property.
* @param object - object address.