View previous topic :: View next topic |
Author |
Message |
qwijibow
Joined: 28 Mar 2008 Posts: 1
|
Posted: Fri Mar 28, 2008 4:35 pm Post subject: need help replacng c macros with D templates ? |
|
|
I have written some example code partially in D that requires the source to first be run through a C pre-processor.
it uses the macro CHECK_SET_PROPERTY to quicky create many property set functions which first check that a ready flag is set with assert.
How would one go about doing the same thing with D's metaprogramming abilitys ??
Code: |
#define CHECK_SET_PROPERTY( type, functionName, local_var ) \
type functionName (type Parameter) \
{\
assert(m_ready);\
local_var = Parameter;\
return local_var;\
}\
class TryingToLearnDByExamples
{
public:
CHECK_SET_PROPERTY(uint, a, m_a)
CHECK_SET_PROPERTY(uint, b, m_b)
CHECK_SET_PROPERTY(uint, c, m_c)
CHECK_SET_PROPERTY(uint, d, m_d)
CHECK_SET_PROPERTY(bool, e, m_e)
CHECK_SET_PROPERTY(bool, f, m_f)
CHECK_SET_PROPERTY(bool, g, m_g)
CHECK_SET_PROPERTY(bool, h, m_h)
private:
uint m_a,m_b,m_c,m_d;
bool m_e,m_f,m_g,m_h;
bool m_ready;
}
|
Thanks. |
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Fri Mar 28, 2008 5:37 pm Post subject: |
|
|
If you read here you should be able to figure it out: http://www.digitalmars.com/d/1.0/mixin.html
I think this should work, I haven't tested it in a compiler and I'm not sure if all the " are right.
Code: | template CHECK_SET_PROPERTY (type, string functionName, string local_var)
{
const string CHECK_SET_PROPERTY = type ~ " " ~ functionName ~ " (" ~ type ~ " Parameter)
{
assert(m_ready);" ~
local_var ~ " = Parameter;
return " ~ local_var ~ ";
}"
} |
Call using:
Code: | mixin(CHECK_SET_PROPERTY!(uint, "a", "m_a")); |
|
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Sat Mar 29, 2008 5:47 am Post subject: |
|
|
Maybe you also need to do
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|