-
May 16th, 2013, 07:47 AM
#1
Creating a Property (Get/Set) Class
Hi there,
I've just recently moved from Visual Basic 6 over to C++ and decided that I would try and write a class to mimic the Get/Set behavior seen in a variety of other programming languages. So instead of just using an overloaded function, i.e:
Code:
class NoProp
{
LPCWSTR m_Title;
public:
void Title(LPCWSTR NewValue) {m_Title = NewValue;};
LPCWSTR Title() {return m_Title;};
};
// ...
instNoProp->Title(L"New title!"); // Set
MessageBox(NULL, instNoProp->Title(), NULL, 0); // Get
I could just write the following:
Code:
instProp->Title = L"New title!"; // Set
MessageBox(NULL, instProp->Title, NULL, 0); // Get
After about a week of playing around I came up with the following Property class, and another class named InitProp as a helper.
Code:
#include <windows.h>
template <class tParent, class tProperty>class Property;
template <class tParent, class tProperty>
class InitProp
{
typedef tProperty (tParent::*GET_PROP)(tParent*);
typedef void (tParent::*SET_PROP)(tParent*, tProperty);
public:
InitProp(tParent *cParent,
Property<tParent, tProperty> *cProperty,
tProperty *&tVariable,
GET_PROP Get_Property,
SET_PROP Set_Property)
{
cProperty->m_Property = new tProperty;
*cProperty->m_Property = NULL;
tVariable = cProperty->m_Property;
cProperty->m_Parent = cParent;
cProperty->m_Copy = 0;
cProperty->m_Get_Property = Get_Property;
cProperty->m_Set_Property = Set_Property;
};
};
template <class tParent, class tProperty>
class Property
{
typedef tProperty (tParent::*GET_PROP)(tParent*);
typedef void (tParent::*SET_PROP)(tParent*, tProperty);
public:
friend class InitProp<tParent, tProperty>;
Property()
{
m_Parent = NULL;
m_Set_Property = NULL;
m_Get_Property = NULL;
};
~Property()
{
if (m_Copy-- == 0)
delete m_Property;
};
Property operator= (tProperty NewValue)
{
if (m_Set_Property != NULL)
(m_Parent->*m_Set_Property)(m_Parent, NewValue);
return *this;
}
Property(const Property& Prop)
{
++m_Copy;
};
operator tProperty()
{
if (m_Get_Property != NULL)
return (m_Parent->*m_Get_Property)(m_Parent);
else
return NULL;
};
private:
int m_Copy;
tParent *m_Parent;
tProperty *m_Property;
GET_PROP m_Get_Property;
SET_PROP m_Set_Property;
};
class Form
{
public:
Property<Form, LPCWSTR> Title;
Form()
{
InitProp<Form, LPCWSTR> pTitle(this, &Title, m_Title, &Form::g_Title, &Form::s_Title);
};
private:
LPCWSTR *m_Title;
LPCWSTR g_Title(Form *Sender)
{
// User called Form.Title
return *Sender->m_Title;
};
void s_Title(Form *Sender, LPCWSTR NewValue)
{
// User set Form.Title =
*Sender->m_Title = NewValue;
};
};
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Form *frmMain;
frmMain = new Form;
frmMain->Title = L"This is just a test!";
MessageBox(NULL, frmMain->Title, NULL, 0);
delete frmMain;
return 0;
}
So far this code only works with the LPCWSTR type (const wchar_t*). Unfortunately I got to this stage using only that type, and now when I try another type, such as int, it fails. When using the int type I believe it creates an error because the constructor of InitProp expects a type of pointer to be parsed. When I try using it with int* type (the private variable then becoming int **m_Variable) it compiles, thought I cannot access the property like a normal int.
My best guess from here is that I probably have to overload the InitProp constructor in a way that it can setup the Property classes for base-types and pointer-types, and then also modify the Property class to handle both.
I'd appreciate any suggestions and input as I'm becoming lost in template->pointer-land.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|