G
Guest
My English is poor, so there are many syntax error, i am sorry.
In the past time ,we write code that can be reuse.
But OOP only let us reuse the framework and the design mode.
So, now, we write GP code with template, but today, we found GP also can not satisfy us.
For Instance, the boost library's bind had written as very long code, but the code is very similar.
pelese look at this code:
//**********************macro version
#define PRODUCE_CLASS(classname, member) \
template<typename T> \
class classname \
{ \
protected: \
T m_##member; \
public: \
classname() : m_##member(T()) { } \
void set_##member(T& member) { m_##member = member; } \
T& get_##member() { return m_##member; } \
};
//**********************GP version
template<typename T>
class Base
{
protected:
T m_data;
public:
classname() : m_data( T()) { }
void set_data(T& data) { m_data = member; }
T& get_data() { return m_data; }
};
//*********************use macro version
PRODUCE_CLASS(Alpha, alpha);
PRODUCE_CLASS(Color, color);
struct A : public Alpha<float>, public Color<int>
{
void func()
{
set_alpha(0.1f);
set_color(0);
}
};
extern A* p;
float a = p->get_alpha();
int c = p->get_color();
//this code is very definitude, but can not debug!
//*********************use GP version
template<typename T>
class Alpha : public Base<T> { };
template<typename T>
class Color : public Base<T> { };
template<class Dest, class Src>
inline Dest* get_ptr(Src* srcptr)
{
return static_cast<Dest*>(srcptr);
}
struct A : public Alpha<float>, public Color<int>
{
void func()
{
get_ptr< Alpha<float> >(this)->set_data(0.1f);
get_ptr< Color<int> >(this)->set_data(0);
}
};
extern A* p;
float a = get_ptr< Alpha<float> >(p)->get_data();
int c = get_ptr< Color<int> >(p)->get_data();
//this code is not definitude, but can debug!
//**********************************************************
Summarize , wo need debug the Macro code, and we think this is not too difficulty to Developers.
In the past time ,we write code that can be reuse.
But OOP only let us reuse the framework and the design mode.
So, now, we write GP code with template, but today, we found GP also can not satisfy us.
For Instance, the boost library's bind had written as very long code, but the code is very similar.
pelese look at this code:
//**********************macro version
#define PRODUCE_CLASS(classname, member) \
template<typename T> \
class classname \
{ \
protected: \
T m_##member; \
public: \
classname() : m_##member(T()) { } \
void set_##member(T& member) { m_##member = member; } \
T& get_##member() { return m_##member; } \
};
//**********************GP version
template<typename T>
class Base
{
protected:
T m_data;
public:
classname() : m_data( T()) { }
void set_data(T& data) { m_data = member; }
T& get_data() { return m_data; }
};
//*********************use macro version
PRODUCE_CLASS(Alpha, alpha);
PRODUCE_CLASS(Color, color);
struct A : public Alpha<float>, public Color<int>
{
void func()
{
set_alpha(0.1f);
set_color(0);
}
};
extern A* p;
float a = p->get_alpha();
int c = p->get_color();
//this code is very definitude, but can not debug!
//*********************use GP version
template<typename T>
class Alpha : public Base<T> { };
template<typename T>
class Color : public Base<T> { };
template<class Dest, class Src>
inline Dest* get_ptr(Src* srcptr)
{
return static_cast<Dest*>(srcptr);
}
struct A : public Alpha<float>, public Color<int>
{
void func()
{
get_ptr< Alpha<float> >(this)->set_data(0.1f);
get_ptr< Color<int> >(this)->set_data(0);
}
};
extern A* p;
float a = get_ptr< Alpha<float> >(p)->get_data();
int c = get_ptr< Color<int> >(p)->get_data();
//this code is not definitude, but can debug!
//**********************************************************
Summarize , wo need debug the Macro code, and we think this is not too difficulty to Developers.