Skip to content

Instantly share code, notes, and snippets.

@wangxiaodong
Created July 9, 2013 10:39
Show Gist options
  • Save wangxiaodong/5956428 to your computer and use it in GitHub Desktop.
Save wangxiaodong/5956428 to your computer and use it in GitHub Desktop.
cocos2dx - property, include base, assign and retain
//
// CCProperty.cpp
// shilipu
//
// Created by wang xiaodong on 13-7-9.
//
//
#include "CCProperty.h"
CCBaseProperty::CCBaseProperty(cocos2d::CCObject *node){
_node = node;
}
CCBaseProperty::~CCBaseProperty(){
_node = NULL;
}
cocos2d::CCObject * CCBaseProperty::get(){
return _node;
}
CCBaseProperty& CCBaseProperty::operator=(cocos2d::CCObject *obj){
_node = obj;
return *this;
}
CCBaseProperty& CCBaseProperty::operator=(CCBaseProperty& property){
_node = property._node;
return *this;
}
cocos2d::CCObject& CCBaseProperty::operator*() const{
assert(_node);
return *_node;
}
cocos2d::CCObject* CCBaseProperty::operator->() const{
return _node;
}
//
// CCProperty.h
// shilipu
//
// Created by wang xiaodong on 13-7-9.
//
//
#ifndef __shilipu__CCProperty__
#define __shilipu__CCProperty__
#include "cocos2d.h"
class CCBaseProperty {
public:
CCBaseProperty(cocos2d::CCObject *node=NULL);
virtual ~CCBaseProperty() = 0;
cocos2d::CCObject * get();
CCBaseProperty& operator=(cocos2d::CCObject *obj);
CCBaseProperty& operator=(CCBaseProperty& property);
cocos2d::CCObject& operator*() const;
cocos2d::CCObject* operator->() const;
private:
cocos2d::CCObject * _node;
};
template <typename T>
class CCProperty : public CCBaseProperty{
public:
CCProperty(T *node=NULL):CCBaseProperty(node){
};
virtual ~CCProperty(){}
T& operator*() const{
return (T&)CCBaseProperty::operator*();
}
T* operator->() const{
return (T*)CCBaseProperty::operator->();
}
T * get(){
return (T*)CCBaseProperty::get();
}
CCProperty<T>& operator=(T * obj){
CCBaseProperty::operator=(obj);
return *this;
}
CCProperty<T>& operator=(CCProperty<T> property){
operator =(property.get());
return *this;
}
};
template <typename T>
class CCAssignProperty : public CCProperty<T>{
public:
CCAssignProperty(T *obj=NULL):CCProperty<T>(obj){
}
~CCAssignProperty(){
this->operator=(NULL);
}
public:
CCAssignProperty<T>& operator=(CCProperty<T>& property){
return CCAssignProperty::operator*(property.get());
}
CCAssignProperty<T>& operator=(T* obj){
CCProperty<T>::operator=(obj);
return *this;
}
};
template <typename T>
class CCRetainProperty : public CCProperty<T> {
public:
CCRetainProperty(T *obj=NULL):CCProperty<T>(obj){
if(obj != NULL){
obj->retain();
}
}
~CCRetainProperty(){
this->operator = (NULL);
}
public:
CCRetainProperty<T>& operator=(CCProperty<T>& property){
return CCRetainProperty::operator=(property.get());
}
CCRetainProperty<T>& operator=(T* obj){
if(obj){
obj->retain();
}
if(CCProperty<T>::get()){
CCProperty<T>::get()->release();
}
CCProperty<T>::operator=(obj);
return *this;
}
};
#endif /* defined(__shilipu__CCProperty__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment