W
W1k3
Guest
Hello, I've been creating and wrapping a large project with C++/CLI and I got really tired creating hundreds of properties for classes and structs, most of which wrap simple variables.
These are my first ever macros. I looked at inline functions, but I believe what I want is only possible with macros as the purpose is to create a shortcut for the C++/CLI property syntax. The "getSet" macro won't work for anything besides simple framework data types, but I included a couple additional macros I've been using for my most common property types.
Please excuse the formating.
#define getSet(item, type) { type get(){ \
return item;}\
void set(type value){\
item = value;}}
#define getSetRef(item, type) { type ^ get(){ \
return gcnew type(item);}\
void set(type ^ value){\
item = value;}}
#define getSetVec(item) { vec2 ^ get(){ \
return gcnew vec2(item);}\
void set(vec2 ^ v){\
item = glm::vec2(v->x, v->y);}}
This allows me to turn this:
property float Angle {
float get() {
return s->angle;
}
void set(float value) {
s->angle = value;
}
}
property bool hidden {
bool get() {
return s->hidden;
}
void set(bool value) {
s->hidden = value;
}
}
property unmanaged_color ^ Color {
unmanaged_color ^ get() {
return gcnew unmanaged_color(s->color);
}
void set(unmanaged_color ^ value) {
s->color = value;
}
}
property vec2 ^ Scale {
vec2 ^ get() {
return gcnew vec2(s->scale);
}
void set(vec2 ^ v) {
s->scale = glm::vec2(v->x, v->y);
}
}
shape* s;
Into this:
property float Angle getSet(s->angle, float);
property bool hidden getSet(s - hidden, bool);
property unmanaged_color ^ Color getSetRef(s->color, unmanaged_color);
property vec2 ^ Scale getSetVec(s->scale);
shape* s;
With these properties that act as simple pass throughs to my managed shape "s", the macros collapse seven lines of code into one.
I'm really interested to hear if you think these macros seem useful as well as if you have any better ideas for simplifying C++/CLI properties.
Continue reading...
These are my first ever macros. I looked at inline functions, but I believe what I want is only possible with macros as the purpose is to create a shortcut for the C++/CLI property syntax. The "getSet" macro won't work for anything besides simple framework data types, but I included a couple additional macros I've been using for my most common property types.
Please excuse the formating.
#define getSet(item, type) { type get(){ \
return item;}\
void set(type value){\
item = value;}}
#define getSetRef(item, type) { type ^ get(){ \
return gcnew type(item);}\
void set(type ^ value){\
item = value;}}
#define getSetVec(item) { vec2 ^ get(){ \
return gcnew vec2(item);}\
void set(vec2 ^ v){\
item = glm::vec2(v->x, v->y);}}
This allows me to turn this:
property float Angle {
float get() {
return s->angle;
}
void set(float value) {
s->angle = value;
}
}
property bool hidden {
bool get() {
return s->hidden;
}
void set(bool value) {
s->hidden = value;
}
}
property unmanaged_color ^ Color {
unmanaged_color ^ get() {
return gcnew unmanaged_color(s->color);
}
void set(unmanaged_color ^ value) {
s->color = value;
}
}
property vec2 ^ Scale {
vec2 ^ get() {
return gcnew vec2(s->scale);
}
void set(vec2 ^ v) {
s->scale = glm::vec2(v->x, v->y);
}
}
shape* s;
Into this:
property float Angle getSet(s->angle, float);
property bool hidden getSet(s - hidden, bool);
property unmanaged_color ^ Color getSetRef(s->color, unmanaged_color);
property vec2 ^ Scale getSetVec(s->scale);
shape* s;
With these properties that act as simple pass throughs to my managed shape "s", the macros collapse seven lines of code into one.
I'm really interested to hear if you think these macros seem useful as well as if you have any better ideas for simplifying C++/CLI properties.
Continue reading...