CC_SYNTHESIZE and CC_PROPERTY

Hi,

First of all I would like to note that Wiki entry for code porting needs updating, as it is mentioning CC*X**_PROPERTY and CC*X**_SYNTHESIZE.

I would like to ask if I could use CC_SYNTHESIZE to create properties for objects like CCNode**?
I’ve seen in for example CCTMXXMLParser that you use CC_PROPERTY for CCNode** and alike.

Thanks

EDIT:

also… I can see that in CCTMXLayerInfo constructor, you are initializing m_pProperties [CCStringToStringDictionary], however you don’t initialize other properties. Is there any rationale behind it?

I believe all these do is create getter and setter functions as a macro definition. Personally, in C++ if I need a getX or setX function, I just write it myself. I don’t think there’s any downside to this. The upside of course being that you can add multiple lines of changes to your getter and setter function if needed. If you’re using the property/synthesize stuff I don’t believe you can.

1 Like

I get what does macros do… I’m just wondering why are they used this way. And why is the constructor used this way.
I don’t want to make stupid mistakes when porting my game. Especially that my c++ knowledge is limited [I’m going to use lua hopefully, but I have to port few classes to c++]

I believe the only reason those macros exist is specifically for people porting Objective-C code written in Cocos2d-iPhone. It just allows them to essentially change their property, andsynthesize calls to macros and maintain equivalent functionality. If you’re coming from an Objective-C port, go wild with them. If not, I’d personally just write the c++.

For example, in Objective-C property (readwrite) bool flag; and thensynthesize flag; allows this variable to now be referenced as [class setFlag:YES]; and bool value = [class getFlag]; These macro’s allow a user to change these calls to class->setFlag(true); etc making it far easier to just do a line for line port without having to determine logic, or pointer handling, etc.

1 Like

Hi William,

thanks for answer, but still. I know what does macros do. I am wondering why in code someone used SYNTHESIZE for int and PROPERTY for a pointer to an object.
In other words, why in CCTMXXMLParser.h it is done like this:

        /// is stroing characters?
        CC_SYNTHESIZE(bool, m_bStoringCharacters, StoringCharacters);
        /// properties
        CC_PROPERTY(CCStringToStringDictionary*, m_pProperties, Properties);

and not like this:

        /// is stroing characters?
        CC_SYNTHESIZE(bool, m_bStoringCharacters, StoringCharacters);
        /// properties
        CC_SYNTHESIZE(CCStringToStringDictionary*, m_pProperties, Properties);

when I can see that the setter and getter are just:

    CCStringToStringDictionary * CCTMXLayerInfo::getProperties()
    {
        return m_pProperties;
    }
    void CCTMXLayerInfo::setProperties(CCStringToStringDictionary* var)
    {
        CC_SAFE_RETAIN(var);
        CC_SAFE_RELEASE(m_pProperties);
        m_pProperties = var;
    }

It seems like the CC_SYNTHESIZE macro is not enough to handle objects, is this correct?

When we started cocos2d-x, we were not very familiar with object-c.
I think CC_PROPERTY should be used for declaring member functions, and CC_SYNTHESIZE to implements them.
I think we should change it in next version.

Hmm…

I’m wondering if it would make sense to create CC_SYNTHESIZE version to include CC_SAFE_RETAIN/CC_SAFE_RELEASE?

If you don’t need retain and release, you can use CC_SYNTHESIZE_READONLY.

Hmm,…
but there’s no retain/release in the SYNTHESIZE macro:

#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: inline varType get##funName(void) const { return varName; }\
public: inline void set##funName(varType var){ varName = var; }