Originally Posted by
anon
It would help if you provided code that actually produces these errors.
One thing to note is that your getters are not const correct. (The error messages seem to say that this is const where you are using it, and you are trying to use it to call non-const getters.)
(Using macros to create getters and setters seems a bit of an abomination. Clearly you need to type more to use DECLARE_RO_PROPERTY and DECLARE_WO_PROPERTY that just typing the declaration yourself?)
Yes anon I realized that, so now I wrote kind of a Property class to get the same result.
This is the property H file:
PHP Code:
#ifndef _PROPERTY_H
#define _PROPERTY_H
#define READACCESS(m) (((m&PROP_READ) == PROP_READ)? true : false)
#define WRITEACCESS(m) (((m&PROP_READ) == PROP_READ)? true : false)
enum PROPERTY_ACCESS_MODE{
PROP_NULL = (0<<0),
PROP_READ = (1<<0),
PROP_WRITE = (1<<1),
PROP_READWRITE = PROP_READ|PROP_WRITE
};
namespace GUI{
template<class C, class T, PROPERTY_ACCESS_MODE mode = PROP_READWRITE> class CProperty{
public:
typedef void(C::*write_callback)(T);
typedef T(C::*read_callback)(void);
CProperty();
CProperty(T);
CProperty(const CProperty&);
~CProperty();
void owner(C*);
void RegisterReadCallback(read_callback);
void RegisterWriteCallback(write_callback);
T operator = (const T& v);
operator T();
protected:
private:
PROPERTY_ACCESS_MODE m_mode;
C* m_class;
read_callback m_read_callback;
write_callback m_write_callback;
};
};
#endif
This is the property CPP file:
PHP Code:
#include "property.h"
#include <cassert>
using namespace GUI;
template<class C, class T,PROPERTY_ACCESS_MODE m> CProperty<C,T,m>::CProperty()
{
}
template<class C, class T,PROPERTY_ACCESS_MODE m> CProperty<C,T,m>::CProperty(T v)
{
assert(WRITEACCESS(this->m_mode));
assert(this->m_write_callback!=NULL);
this->m_write_callback(v);
}
template<class C, class T,PROPERTY_ACCESS_MODE m> void CProperty<C,T,m>::owner(C* c)
{
assert(c!= NULL);
this->m_class = c;
}
template<class C, class T,PROPERTY_ACCESS_MODE m> CProperty<C,T,m>::CProperty(const CProperty& p)
{
if(this == &p) return;
this->m_class = p.m_class;
this->m_mode = p.m_mode;
this->m_read_callback = p.m_read_callback;
this->m_write_callback = p.m_write_callback;
}
template<class C, class T,PROPERTY_ACCESS_MODE m> CProperty<C,T,m>::~CProperty()
{}
template<class C, class T,PROPERTY_ACCESS_MODE m> T CProperty<C,T,m>::operator = (const T& v)
{
assert(WRITEACCESS(this->m_mode));
assert(this->m_write_callback!= NULL);
this->m_write_callback(v);
return v;
}
template<class C, class T,PROPERTY_ACCESS_MODE m> void CProperty<C,T,m>::RegisterReadCallback(read_callback c)
{
if(!READACCESS(this->m_mode)) this->m_read_callback = NULL;
else this->m_read_callback = c;
}
template<class C, class T,PROPERTY_ACCESS_MODE m> void CProperty<C,T,m>::RegisterWriteCallback(write_callback c)
{
if(!WRITEACCESS(this->m_mode)) this->m_write_callback = NULL;
else this->m_write_callback = c;
}
template<class C, class T,PROPERTY_ACCESS_MODE m> CProperty<C,T,m>::operator T()
{
assert(READACCESS(this->m_mode));
assert(this->m_read_callback != NULL);
return this->m_read_callback();
}
And here is how I'm using it:
PHP Code:
CWnd::CWnd(const CWnd& w)
{
if(this != &w)
{
this->parent = w.parent;
this->m_hWnd = w.hwnd; // see error 1
}
}
PHP Code:
CControl& CControl::operator = (const CControl& c)
{
if(this == &c) return *this;
this->name = c.name;
this->m_hWnd = c.hwnd; // see error 1
this->parent = c.parent;
return *this;
}
PHP Code:
CObject& CContainer::operator ()(std::string n)
{
std::vector<CObject*>::iterator m_iterator = this->m_elements.begin();
while(m_iterator != this->m_elements.end())
{
if(m_iterator == this->m_elements.end()) throw("Object not found");
if(((CObject*)(*m_iterator))->name == n) return *(CObject*)(*m_iterator); // see error 2
++m_iterator;
}
throw("Object not found");
}
Now I get the following errors:
PHP Code:
(1) error C2440: '=' : cannot convert from 'const GUI::CProperty<C,T,mode>' to 'HWND'
(2) error C2678: binary '==' : no operator found which takes a left-hand operand of type 'GUI::CProperty<C,T>' (or there is no acceptable conversion)
Name is declared in CObject as:
PHP Code:
CProperty<CObject,std::string> name;
And hwnd is declared in CWControl as:
PHP Code:
CProperty<CControl,HWND,PROP_READ> hwnd;
I know it has something to do with the const keywords, but how do I fix it?
Grtz