Merge pull request #4685 from dumganhar/develop

Restricts the element type for Vector<T> and Map<K, V>, it has to be `Object*` or `ObjectSubClass*`.
This commit is contained in:
James Chen 2013-12-26 06:03:11 -08:00
commit a73991dcc1
2 changed files with 10 additions and 3 deletions

View File

@ -26,7 +26,7 @@
#define __CCMAP_H__ #define __CCMAP_H__
#include "ccMacros.h" #include "ccMacros.h"
#include "CCObject.h"
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
@ -62,6 +62,7 @@ public:
Map<K, V>() Map<K, V>()
: _data() : _data()
{ {
static_assert(std::is_convertible<V, Object*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the default constructor of Map!"); CCLOGINFO("In the default constructor of Map!");
} }
@ -69,6 +70,7 @@ public:
explicit Map<K, V>(ssize_t capacity) explicit Map<K, V>(ssize_t capacity)
: _data() : _data()
{ {
static_assert(std::is_convertible<V, Object*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the constructor with capacity of Map!"); CCLOGINFO("In the constructor with capacity of Map!");
_data.reserve(capacity); _data.reserve(capacity);
} }
@ -76,6 +78,7 @@ public:
/** Copy constructor */ /** Copy constructor */
Map<K, V>(const Map<K, V>& other) Map<K, V>(const Map<K, V>& other)
{ {
static_assert(std::is_convertible<V, Object*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the copy constructor of Map!"); CCLOGINFO("In the copy constructor of Map!");
_data = other._data; _data = other._data;
addRefForAllObjects(); addRefForAllObjects();
@ -84,6 +87,7 @@ public:
/** Move constructor */ /** Move constructor */
Map<K, V>(Map<K, V>&& other) Map<K, V>(Map<K, V>&& other)
{ {
static_assert(std::is_convertible<V, Object*>::value, "Invalid Type for cocos2d::Map<K, V>!");
CCLOGINFO("In the move constructor of Map!"); CCLOGINFO("In the move constructor of Map!");
_data = std::move(other._data); _data = std::move(other._data);
} }

View File

@ -26,7 +26,7 @@ THE SOFTWARE.
#define __CCVECTOR_H__ #define __CCVECTOR_H__
#include "ccMacros.h" #include "ccMacros.h"
#include "CCObject.h"
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <algorithm> // for std::find #include <algorithm> // for std::find
@ -68,13 +68,14 @@ public:
Vector<T>() Vector<T>()
: _data() : _data()
{ {
static_assert(std::is_convertible<T, Object*>::value, "Invalid Type for cocos2d::Vector<T>!");
} }
/** Constructor with a capacity */ /** Constructor with a capacity */
explicit Vector<T>(ssize_t capacity) explicit Vector<T>(ssize_t capacity)
: _data() : _data()
{ {
static_assert(std::is_convertible<T, Object*>::value, "Invalid Type for cocos2d::Vector<T>!");
CCLOGINFO("In the default constructor with capacity of Vector."); CCLOGINFO("In the default constructor with capacity of Vector.");
reserve(capacity); reserve(capacity);
} }
@ -89,6 +90,7 @@ public:
/** Copy constructor */ /** Copy constructor */
Vector<T>(const Vector<T>& other) Vector<T>(const Vector<T>& other)
{ {
static_assert(std::is_convertible<T, Object*>::value, "Invalid Type for cocos2d::Vector<T>!");
CCLOGINFO("In the copy constructor!"); CCLOGINFO("In the copy constructor!");
_data = other._data; _data = other._data;
addRefForAllObjects(); addRefForAllObjects();
@ -97,6 +99,7 @@ public:
/** Move constructor */ /** Move constructor */
Vector<T>(Vector<T>&& other) Vector<T>(Vector<T>&& other)
{ {
static_assert(std::is_convertible<T, Object*>::value, "Invalid Type for cocos2d::Vector<T>!");
CCLOGINFO("In the move constructor of Vector!"); CCLOGINFO("In the move constructor of Vector!");
_data = std::move(other._data); _data = std::move(other._data);
} }