Hi,
first I would like to apologize about my question because usually I hate
specific implementation but in this case I really would like an answer.
Besides my example is an example of what's not to be done but I am aware
of that.
Don't loose time tell me that I should use standard implementation and
consider my question as theoretical.
So I am using Microsoft compiler and I have a class with lots of methods
with get and set :
class foo
{
public:
....
LPCTSTR GetName();
void SetName(LPCTSTR Value);
A GetABC();
void SetABC(A Value);
A GetABC();
void SetDEF(B Value);
B GetDEF();
....
};
So I would like to mix preprocessor tricks(it's bad I know) and template :
I am thinkinf of something like :
DECLARE_PROPERTY(Name, LPCTSTR);
DECLARE_PROPERTY(ABC, A);
DECLARE_PROPERTY(DEF, B);
and after maybe
Property.Name = L"Vincent";
LPCTSTR tszName = Property.Name;
And here is what I have done for now :
template<class TYPE>
struct Property
{
TYPE m_Type;
TYPE Get()
{
g_pClient->Get ...( m_Type); // FILL BY MACRO
return TYPE;
}
void Set()
{
g_pClient->Set ...( m_Type); // FILL BY MACRO
}
__declspec(property(get = Get, put = Put)) TYPE the_prop;
};
// template specialization for C strings
template<>
struct Property<LPCTSTR>
{
... blabla
}..
I am sur there is something to do with all that but I still cannot find
the way.
I would try something like
DECLARE_PROPERTY(PropName, TYPE) \
template<class TYPE\
struct Property \
{ \
TYPE m_Type; \
TYPE Get() \
{ \
g_pClient->Get ...( m_Type); \
return TYPE; \
} \
void Set() \
{ \
g_pClient->Set ...( m_Type); \
} \
__declspec(property(get = Get, put = Put)) TYPE PropName; \
}; \
typedef something ...
Please give me your feedback
//------------------------ MS property ---------------------//
/ declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}