FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

need help replacng c macros with D templates ?

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
qwijibow



Joined: 28 Mar 2008
Posts: 1

PostPosted: Fri Mar 28, 2008 4:35 pm    Post subject: need help replacng c macros with D templates ? Reply with quote

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
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Fri Mar 28, 2008 5:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Sat Mar 29, 2008 5:47 am    Post subject: Reply with quote

Maybe you also need to do
Code:
type.stringof
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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