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 21:49:40 +08:00
parent 9679b0c8a7
commit a8d93ddf72
2 changed files with 10 additions and 3 deletions

View File

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

View File

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