default smart pointer type switched to intrusive_ptr

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@337 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-05-11 21:34:53 +00:00
parent 3b6743b110
commit 33fd2b5e18
103 changed files with 368 additions and 256 deletions
+8 -2
View File
@@ -13,7 +13,7 @@
#include <script/value.hpp>
class Script;
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
DECLARE_POINTER_TYPE(Script);
template <typename T> class Defaultable;
template <typename T> class Scriptable;
@@ -39,6 +39,9 @@ class GetDefaultMember {
/// Handle an object: we don't match things with a name
template <typename T>
void handle(const Char* name, const T& object) {}
/// Don't handle a value
template <typename T>
inline void handleNoScript(const Char* name, T& value) {}
/// Handle an object: investigate children, or store it if we know how
void handle(const Char *);
@@ -50,7 +53,7 @@ class GetDefaultMember {
template <typename T> void handle(const vector<T>& c) { value = to_script(&c); }
template <typename K, typename V> void handle(const map<K,V>& c) { value = to_script(&c); }
template <typename K, typename V> void handle(const IndexMap<K,V>& c) { value = to_script(&c); }
template <typename T> void handle(const shared_ptr<T>& p) { value = to_script(p); }
template <typename T> void handle(const intrusive_ptr<T>& p) { value = to_script(p); }
void handle(const ScriptValueP&);
void handle(const ScriptP&);
private:
@@ -85,6 +88,9 @@ class GetMember : private GetDefaultMember {
gdm.handle(object);
}
}
/// Don't handle a value
template <typename T>
inline void handleNoScript(const Char* name, T& value) {}
/// Handle an object: investigate children
template <typename T> void handle(const T&);
/// Handle an index map: invistigate keys
+2 -2
View File
@@ -412,9 +412,9 @@ IMPLEMENT_REFLECTION(Packaged) {
REFLECT(short_name);
REFLECT(full_name);
REFLECT_N("icon", icon_filename);
REFLECT(position_hint);
REFLECT_NO_SCRIPT(position_hint);
REFLECT(version);
REFLECT_N("depends ons", dependencies); // hack for singular_form
REFLECT_NO_SCRIPT_N("depends ons", dependencies); // hack for singular_form
}
Packaged::Packaged()
+2 -2
View File
@@ -45,7 +45,7 @@ DECLARE_DYNAMIC_ARG(Package*, clipboard_package);
*
* TODO: maybe support sub packages (a package inside another package)?
*/
class Package {
class Package : public IntrusivePtrVirtualBase {
public:
// --------------------------------------------------- : Managing the outside of the package
@@ -177,7 +177,7 @@ class Package {
// ----------------------------------------------------------------------------- : Packaged
/// Dependencies of a package
class PackageDependency {
class PackageDependency : public IntrusivePtrBase<PackageDependency> {
public:
String package; ///< Name of the package someone depends on
Version version; ///< Minimal required version of that package
+6 -6
View File
@@ -65,12 +65,12 @@ PackagedP PackageManager::openAny(const String& name, bool just_header) {
return p;
} else {
// load with the right type, based on extension
if (fn.GetExt() == _("mse-game")) p = new_shared<Game>();
else if (fn.GetExt() == _("mse-style")) p = new_shared<StyleSheet>();
else if (fn.GetExt() == _("mse-locale")) p = new_shared<Locale>();
else if (fn.GetExt() == _("mse-include")) p = new_shared<IncludePackage>();
else if (fn.GetExt() == _("mse-symbol-font")) p = new_shared<SymbolFont>();
else if (fn.GetExt() == _("mse-export-template")) p = new_shared<ExportTemplate>();
if (fn.GetExt() == _("mse-game")) p = new_intrusive<Game>();
else if (fn.GetExt() == _("mse-style")) p = new_intrusive<StyleSheet>();
else if (fn.GetExt() == _("mse-locale")) p = new_intrusive<Locale>();
else if (fn.GetExt() == _("mse-include")) p = new_intrusive<IncludePackage>();
else if (fn.GetExt() == _("mse-symbol-font")) p = new_intrusive<SymbolFont>();
else if (fn.GetExt() == _("mse-export-template")) p = new_intrusive<ExportTemplate>();
else {
throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name);
}
+3 -3
View File
@@ -33,19 +33,19 @@ class PackageManager {
/// Open a package with the specified name (including extension)
template <typename T>
shared_ptr<T> open(const String& name) {
intrusive_ptr<T> open(const String& name) {
wxFileName fn(data_directory + _("/") + name);
fn.Normalize();
String filename = fn.GetFullPath();
// Is this package already loaded?
PackagedP& p = loaded_packages[filename];
shared_ptr<T> typedP = dynamic_pointer_cast<T>(p);
intrusive_ptr<T> typedP = dynamic_pointer_cast<T>(p);
if (typedP) {
typedP->loadFully();
return typedP;
} else {
// not loaded, or loaded with wrong type (i.e. with just_header)
p = typedP = new_shared<T>();
p = typedP = new_intrusive<T>();
typedP->open(filename);
return typedP;
}
+9 -5
View File
@@ -82,14 +82,18 @@ class Reader {
exitBlock();
}
}
/// Handle a value
template <typename T>
inline void handleNoScript(const Char* name, T& value) { handle(name,value); }
/// Reads a vector from the input stream
template <typename T>
void handle(const Char* name, vector<T>& vector);
/// Reads an object of type T from the input stream
template <typename T> void handle(T& object);
/// Reads a shared_ptr from the input stream
template <typename T> void handle(shared_ptr<T>& pointer);
/// Reads a intrusive_ptr from the input stream
template <typename T> void handle(intrusive_ptr<T>& pointer);
/// Reads a map from the input stream
template <typename V> void handle(map<String,V>& m);
/// Reads an IndexMap from the input stream, reads only keys that already exist in the map
@@ -178,8 +182,8 @@ class Reader {
* This function can be overloaded to provide different behaviour
*/
template <typename T>
shared_ptr<T> read_new(Reader& reader) {
return new_shared<T>();
intrusive_ptr<T> read_new(Reader& reader) {
return new_intrusive<T>();
}
/// Update the 'index' member of a value for use by IndexMap
@@ -197,7 +201,7 @@ void Reader::handle(const Char* name, vector<T>& vector) {
}
template <typename T>
void Reader::handle(shared_ptr<T>& pointer) {
void Reader::handle(intrusive_ptr<T>& pointer) {
if (!pointer) pointer = read_new<T>(*this);
handle(*pointer);
}
+7 -3
View File
@@ -45,6 +45,10 @@ class Writer {
handle(object);
exitBlock();
}
/// Handle a value
template <typename T>
inline void handleNoScript(const Char* name, T& value) { handle(name,value); }
/// Write a vector to the output stream
template <typename T>
void handle(const Char* name, const vector<T>& vector);
@@ -55,8 +59,8 @@ class Writer {
/// Write an object of type T to the output stream
template <typename T> void handle(const T& object);
/// Write a shared_ptr to the output stream
template <typename T> void handle(const shared_ptr<T>& pointer);
/// Write a intrusive_ptr to the output stream
template <typename T> void handle(const intrusive_ptr<T>& pointer);
/// Write a map to the output stream
template <typename K, typename V> void handle(const map<K,V>& map);
/// Write an IndexMap to the output stream
@@ -107,7 +111,7 @@ void Writer::handle(const Char* name, const vector<T>& vec) {
}
template <typename T>
void Writer::handle(const shared_ptr<T>& pointer) {
void Writer::handle(const intrusive_ptr<T>& pointer) {
if (pointer) handle(*pointer);
}
+1 -1
View File
@@ -16,7 +16,7 @@
/// Object that cashes an ordered version of a list of items, for finding the position of objects
/** Can be used as a map "void* -> int" for finding the position of an object */
template <typename T>
class OrderCache {
class OrderCache : public IntrusivePtrBase<OrderCache> {
public:
/// Initialize the order cache, ordering the keys by their string values from the other vector
/** @pre keys.size() == values.size() */
+17 -1
View File
@@ -84,13 +84,24 @@
template <class Tag> \
void Cls::reflect_impl(Tag& tag)
/// Implement the refelection of a class type Cls, but only for Reader and Writer
/// Implement the refelection of a class type Cls, but only for Reader and Writer,
/** There is custom code for GetMember and GetDefaultMember */
#define IMPLEMENT_REFLECTION_NO_GET_MEMBER(Cls) \
REFLECT_OBJECT_READER(Cls) \
REFLECT_OBJECT_WRITER(Cls) \
template <class Tag> \
void Cls::reflect_impl(Tag& tag)
/// Implement the refelection of a class type Cls, but only for Reader and Writer
/** There is no code for GetMember and GetDefaultMember */
#define IMPLEMENT_REFLECTION_NO_SCRIPT(Cls) \
REFLECT_OBJECT_READER(Cls) \
REFLECT_OBJECT_WRITER(Cls) \
REFLECT_OBJECT_GET_DEFAULT_MEMBER_NOT(Cls) \
REFLECT_OBJECT_GET_MEMBER_NOT(Cls) \
template <class Tag> \
void Cls::reflect_impl(Tag& tag)
/// Reflect a variable
#define REFLECT(var) tag.handle(_(#var), var)
/// Reflect a variable under the given name
@@ -130,6 +141,11 @@
*/
#define REFLECT_ALIAS(version, old, new) tag.addAlias(version, _(old), _(new))
/// Reflect a variable, ignores the variable for scripting
#define REFLECT_NO_SCRIPT(var) tag.handleNoScript(_(#var), var)
/// Reflect a variable under the given name
#define REFLECT_NO_SCRIPT_N(name, var) tag.handleNoScript(_(name), var)
// ----------------------------------------------------------------------------- : Reflecting enums
/// Implement the refelection of a enumeration type Enum
+63 -14
View File
@@ -29,7 +29,7 @@ using namespace boost;
// ----------------------------------------------------------------------------- : Declaring
/// Declares the type TypeP as a shared_ptr<Type>
#define DECLARE_POINTER_TYPE(Type) \
#define DECLARE_SHARED_POINTER_TYPE(Type) \
class Type; \
typedef shared_ptr<Type> Type##P;
@@ -91,8 +91,8 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
#ifdef USE_INTRUSIVE_PTR
/// Declares the type TypeP as a intrusive_ptr<Type>
#define DECLARE_INTRUSIVE_POINTER_TYPE(Type) \
class Type; \
#define DECLARE_POINTER_TYPE(Type) \
class Type; \
typedef intrusive_ptr<Type> Type##P;
@@ -147,11 +147,52 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8));
}
/// Base class for objects wishing to use intrusive_ptrs
class IntrusivePtrBase {
// ----------------------------------------------------------------------------- : Intrusive pointer base
/// Base class for objects wishing to use intrusive_ptrs.
/** There is no implicit virtual destructor, objects are destructed as type T
* Usage:
* @code
* DECLARE_POINTER_TYPE(MyClass);
* class MyClass : public IntrusivePtrBase<MyClass> { ... }
* @endcode
*/
template <typename T> class IntrusivePtrBase {
public:
inline IntrusivePtrBase() : ref_count(0) {}
virtual ~IntrusivePtrBase() {}
inline IntrusivePtrBase() : ref_count(0) {}
inline IntrusivePtrBase(const IntrusivePtrBase&) : ref_count(0) {} // don't copy construct the reference count!
private:
AtomicInt ref_count;
template <typename T> friend void intrusive_ptr_add_ref(IntrusivePtrBase*);
template <typename T> friend void intrusive_ptr_release(IntrusivePtrBase*);
};
template <typename T> inline void intrusive_ptr_add_ref(IntrusivePtrBase<T>* p) {
++p->ref_count;
}
template <typename T> inline void intrusive_ptr_release(IntrusivePtrBase<T>* p) {
if (--p->ref_count == 0) {
delete static_cast<T*>(p);
}
}
// ----------------------------------------------------------------------------- : Intrusive pointer base : virtual
/// IntrusivePtrBase with a virtual destructor
class IntrusivePtrVirtualBase : public IntrusivePtrBase<IntrusivePtrVirtualBase> {
public:
virtual ~IntrusivePtrVirtualBase() {}
};
// ----------------------------------------------------------------------------- : Intrusive pointer base : with delete
/// Base class for objects wishing to use intrusive_ptrs, using a manual delete function
class IntrusivePtrBaseWithDelete {
public:
inline IntrusivePtrBaseWithDelete() : ref_count(0) {}
inline IntrusivePtrBaseWithDelete(const IntrusivePtrBaseWithDelete&)
: ref_count(0) {} // don't copy construct the reference count!
virtual ~IntrusivePtrBaseWithDelete() {}
protected:
/// Delete this object
virtual void destroy() {
@@ -159,21 +200,21 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
}
private:
AtomicInt ref_count;
friend void intrusive_ptr_add_ref(IntrusivePtrBase*);
friend void intrusive_ptr_release(IntrusivePtrBase*);
friend void intrusive_ptr_add_ref(IntrusivePtrBaseWithDelete*);
friend void intrusive_ptr_release(IntrusivePtrBaseWithDelete*);
};
inline void intrusive_ptr_add_ref(IntrusivePtrBase* p) {
inline void intrusive_ptr_add_ref(IntrusivePtrBaseWithDelete* p) {
++p->ref_count;
}
inline void intrusive_ptr_release(IntrusivePtrBase* p) {
inline void intrusive_ptr_release(IntrusivePtrBaseWithDelete* p) {
if (--p->ref_count == 0) {
p->destroy();
}
}
#else
#define DECLARE_INTRUSIVE_POINTER_TYPE DECLARE_POINTER_TYPE
#define DECLARE_POINTER_TYPE DECLARE_SHARED_POINTER_TYPE
#define intrusive_ptr shared_ptr
#define new_intrusive new_shared
#define new_intrusive1 new_shared1
@@ -186,9 +227,17 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
#define new_intrusive8 new_shared8
#define new_intrusive9 new_shared9
class IntrusivePtrBase {
template <typename T> class IntrusivePtrBase {};
/// IntrusivePtrBase with a virtual destructor
class IntrusivePtrVirtualBase : public IntrusivePtrBase<IntrusivePtrVirtualBase> {
public:
virtual ~IntrusivePtrBase() {};
virtual ~IntrusivePtrVirtualBase() {}
};
class IntrusivePtrBaseWithDelete {
public:
virtual ~IntrusivePtrBaseWithDelete() {}
protected:
/// Delete this object
virtual void destroy() {