From fc03b5efa152a92895f12dffc68625357c6e4597 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sun, 28 Jan 2007 19:13:01 +0000 Subject: [PATCH] Fixed compilation errors for gcc@linux (not all yet). git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@181 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/format/formats.hpp | 1 + src/data/set.hpp | 3 +- src/gui/thumbnail_thread.hpp | 5 +- src/script/scriptable.hpp | 2 +- src/script/value.hpp | 43 ++---------------- src/util/action_stack.hpp | 1 + src/util/age.hpp | 24 +++------- src/util/atomic.hpp | 88 ++++++++++++++++++++++++++++++++++++ src/util/error.hpp | 6 +-- src/util/for_each.hpp | 2 + src/util/index_map.hpp | 6 +-- src/util/io/package.hpp | 1 + src/util/prec.hpp | 1 + src/util/smart_ptr.hpp | 45 ++++++++++++++++-- src/util/vector2d.hpp | 2 +- 15 files changed, 159 insertions(+), 71 deletions(-) create mode 100644 src/util/atomic.hpp diff --git a/src/data/format/formats.hpp b/src/data/format/formats.hpp index 0d91a4c9..ddd87a03 100644 --- a/src/data/format/formats.hpp +++ b/src/data/format/formats.hpp @@ -22,6 +22,7 @@ DECLARE_POINTER_TYPE(FileFormat); /// A filter for a specific file format class FileFormat { public: + virtual ~FileFormat() {} /// File extension used by this file format virtual String extension() = 0; /// Name of the filter diff --git a/src/data/set.hpp b/src/data/set.hpp index b889f5d6..336bf834 100644 --- a/src/data/set.hpp +++ b/src/data/set.hpp @@ -13,6 +13,7 @@ #include #include #include +#include // for Set::value #include DECLARE_POINTER_TYPE(Card); @@ -131,7 +132,7 @@ class SetView : public ActionListener { public: SetView(); ~SetView(); - + /// Get the set that is currently being viewed inline SetP getSet() { return set; } /// Change the set that is being viewed diff --git a/src/gui/thumbnail_thread.hpp b/src/gui/thumbnail_thread.hpp index ffaf000f..5f9822b3 100644 --- a/src/gui/thumbnail_thread.hpp +++ b/src/gui/thumbnail_thread.hpp @@ -15,6 +15,7 @@ #include DECLARE_POINTER_TYPE(ThumbnailRequest); +class ThumbnailThreadWorker; // ----------------------------------------------------------------------------- : ThumbnailRequest @@ -24,6 +25,8 @@ class ThumbnailRequest { ThumbnailRequest(void* owner, const String& cache_name, const wxDateTime& modified) : owner(owner), cache_name(cache_name), modified(modified) {} + virtual ~ThumbnailRequest() {} + /// Generate the thumbnail, called in another thread virtual Image generate() = 0; /// Store the thumbnail, called from the main thread @@ -66,7 +69,7 @@ class ThumbnailThread { deque open_requests; ///< Requests on which work hasn't finished vector > closed_requests; ///< Requests for which work is completed - set request_names; ///< Requests that haven't been stored yet, to prevent duplicates + set request_names; ///< Requests that haven't been stored yet, to prevent duplicates friend class ThumbnailThreadWorker; ThumbnailThreadWorker* worker; ///< The worker thread. invariant: no requests ==> worker==nullptr }; diff --git a/src/script/scriptable.hpp b/src/script/scriptable.hpp index 4f38c559..4cf14926 100644 --- a/src/script/scriptable.hpp +++ b/src/script/scriptable.hpp @@ -141,7 +141,7 @@ void Reader::handle(Scriptable& s) { if (starts_with(s.script.unparsed, _("script:"))) { s.script.unparsed = s.script.unparsed.substr(7); s.script.parse(*this); - } else if (s.script.unparsed.find_first_of('{') != String.npos) { + } else if (s.script.unparsed.find_first_of('{') != String::npos) { s.script.parse(*this, true); } else { handle(s.value); diff --git a/src/script/value.hpp b/src/script/value.hpp index 1302f1b3..1da28030 100644 --- a/src/script/value.hpp +++ b/src/script/value.hpp @@ -15,17 +15,6 @@ class Context; class Dependency; -#ifdef _MSC_VER - extern "C" { - LONG __cdecl _InterlockedIncrement(LONG volatile *Addend); - LONG __cdecl _InterlockedDecrement(LONG volatile *Addend); - } - #pragma intrinsic (_InterlockedIncrement) - #define InterlockedIncrement _InterlockedIncrement - #pragma intrinsic (_InterlockedDecrement) - #define InterlockedDecrement _InterlockedDecrement -#endif - // ----------------------------------------------------------------------------- : ScriptValue DECLARE_INTRUSIVE_POINTER_TYPE(ScriptValue); @@ -46,7 +35,7 @@ enum ScriptType /// A value that can be handled by the scripting engine. /// Actual values are derived types -class ScriptValue { +class ScriptValue : public IntrusivePtrBase { public: inline ScriptValue() #ifdef USE_INTRUSIVE_PTR @@ -87,34 +76,8 @@ class ScriptValue { virtual ScriptValueP next(); /// Return the number of items in this value (assuming it is a collection) virtual int itemCount() const; - - - protected: - /// Delete this object - virtual void destroy() { - delete this; - } -#ifdef USE_INTRUSIVE_PTR - private: - volatile LONG refCount; - friend void intrusive_ptr_add_ref(ScriptValue*); - friend void intrusive_ptr_release(ScriptValue*); -#endif }; -#ifdef USE_INTRUSIVE_PTR - inline void intrusive_ptr_add_ref(ScriptValue* p) { - //p->refCount += 1; - InterlockedIncrement(&p->refCount); - } - inline void intrusive_ptr_release(ScriptValue* p) { - if (InterlockedDecrement(&p->refCount) == 0) { - //if (--p->refCount == 0) { - p->destroy(); - } - } -#endif - extern ScriptValueP script_nil; ///< The preallocated nil value extern ScriptValueP script_true; ///< The preallocated true value extern ScriptValueP script_false; ///< The preallocated false value @@ -181,7 +144,7 @@ class ScriptCollection : public ScriptValue { template ScriptValueP get_member(const map& m, const String& name) { - map::const_iterator it = m.find(name); + typename map::const_iterator it = m.find(name); if (it != m.end()) { return toScript(it->second); } else { @@ -191,7 +154,7 @@ ScriptValueP get_member(const map& m, const String& name) { template ScriptValueP get_member(const IndexMap& m, const String& name) { - IndexMap::const_iterator it = m.find(name); + typename IndexMap::const_iterator it = m.find(name); if (it != m.end()) { return toScript(*it); } else { diff --git a/src/util/action_stack.hpp b/src/util/action_stack.hpp index d4f8c62e..a3f6aa29 100644 --- a/src/util/action_stack.hpp +++ b/src/util/action_stack.hpp @@ -48,6 +48,7 @@ class Action { /// Base class/interface for objects that listen to actions class ActionListener { public: + virtual ~ActionListener() {} /// Notification that an action a has been performed or undone virtual void onAction(const Action& a, bool undone) = 0; }; diff --git a/src/util/age.hpp b/src/util/age.hpp index f86890d9..1e06c1b6 100644 --- a/src/util/age.hpp +++ b/src/util/age.hpp @@ -11,17 +11,7 @@ #include #include - -#ifdef _MSC_VER - extern "C" { - LONG __cdecl _InterlockedIncrement(LONG volatile *Addend); - LONG __cdecl _InterlockedDecrement(LONG volatile *Addend); - } - #pragma intrinsic (_InterlockedIncrement) - #define InterlockedIncrement _InterlockedIncrement - #pragma intrinsic (_InterlockedDecrement) - #define InterlockedDecrement _InterlockedDecrement -#endif +#include // ----------------------------------------------------------------------------- : Age @@ -33,24 +23,24 @@ class Age { Age() { update(); } - Age(LONG age) : age(age) {} + Age(AtomicIntEquiv age) : age(age) {} /// Update the age to become the newest one inline void update() { - age = InterlockedIncrement(&new_age); + age = ++new_age; } /// Compare two ages, smaller means earlier inline bool operator < (Age a) const { return age < a.age; } /// A number corresponding to the age - inline LONG get() const { return age; } + inline AtomicIntEquiv get() const { return age; } private: /// This age - LONG age; + AtomicIntEquiv age; /// Global age counter, value of the last age created - static volatile LONG new_age; + static AtomicInt new_age; }; @@ -60,7 +50,7 @@ class Age { * if last_update_age > 0 they return whether the image is still up to date * if last_update_age == 0 they generate the image */ -DECLARE_DYNAMIC_ARG (long, last_update_age); +DECLARE_DYNAMIC_ARG (AtomicIntEquiv, last_update_age); // ----------------------------------------------------------------------------- : EOF #endif diff --git a/src/util/atomic.hpp b/src/util/atomic.hpp new file mode 100644 index 00000000..a9332632 --- /dev/null +++ b/src/util/atomic.hpp @@ -0,0 +1,88 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +#ifndef HEADER_UTIL_ATOMIC +#define HEADER_UTIL_ATOMIC + +/** @file util/atomic.hpp + * + * @brief Provides the type AtomicInt, which is an integer that can be incremented and decremented atomicly + */ + +// ----------------------------------------------------------------------------- : Includes + +// ----------------------------------------------------------------------------- : AtomicInt : windows + +#ifdef _WX_MSW_ + + #ifdef _MSC_VER + extern "C" { + LONG __cdecl _InterlockedIncrement(LONG volatile *Addend); + LONG __cdecl _InterlockedDecrement(LONG volatile *Addend); + } + #pragma intrinsic (_InterlockedIncrement) + #define InterlockedIncrement _InterlockedIncrement + #pragma intrinsic (_InterlockedDecrement) + #define InterlockedDecrement _InterlockedDecrement + #endif + + /// An integer which is equivalent to an AtomicInt, but which doesn't support attomic operations + typedef LONG AtomicIntEquiv; + + /// An integer that can be incremented and decremented atomicly + class AtomicInt { + public: + AtomicInt(AtomicIntEquiv v) : v(v) {} + inline operator AtomicIntEquiv() const { + return v; + } + /// Attomicly increments this AtomicInt, returns the new value + inline AtomicIntEquiv operator ++ () { + return InterlockedIncrement(&v); + } + /// Attomicly decrements this AtomicInt, returns the new value + inline AtomicIntEquiv operator -- () { + return InterlockedDecrement(&v); + } + private: + AtomicIntEquiv v; ///< The value + }; + + /// We have a fast AtomicInt + #define HAVE_FAST_ATOMIC + +// ----------------------------------------------------------------------------- : AtomicInt : portable +#else + + /// An integer which is equivalent to an AtomicInt, but which doesn't support attomic operations + typedef long AtomicIntEquiv; + + /// An integer that can be incremented and decremented atomicly + class AtomicInt { + public: + AtomicInt(AtomicIntEquiv v) : v(v) {} + inline operator AtomicIntEquiv() const { + return v; + } + /// Attomicly increments this AtomicInt, returns the new value + inline AtomicIntEquiv operator ++ () { + wxCriticalSectionLocker lock(cs); + return ++v; + } + /// Attomicly decrements this AtomicInt, returns the new value + inline AtomicIntEquiv operator -- () { + wxCriticalSectionLocker lock(cs); + return --v; + } + private: + AtomicIntEquiv v; ///< The value + wxCriticalSection cs; ///< Critical section protecting v + }; + +#endif + +// ----------------------------------------------------------------------------- : EOF +#endif diff --git a/src/util/error.hpp b/src/util/error.hpp index b09f66de..422539d2 100644 --- a/src/util/error.hpp +++ b/src/util/error.hpp @@ -36,7 +36,7 @@ class Error { class InternalError : public Error { public: inline InternalError(const String& str) - : Error(_ERROR_1_("internal error",str)) + : Error(_ERROR_1_("internal error", str.c_str())) {} }; @@ -52,7 +52,7 @@ class PackageError : public Error { class FileNotFoundError : public PackageError { public: inline FileNotFoundError(const String& file, const String& package) - : PackageError(_ERROR_2_("file not found",file,package)) + : PackageError(_ERROR_2_("file not found", file.c_str(), package.c_str())) {} }; @@ -68,7 +68,7 @@ class ParseError : public Error { class FileParseError : public ParseError { public: inline FileParseError(const String& err, const String& file) : - ParseError(_ERROR_2_("file parse error",file,err)) + ParseError(_ERROR_2_("file parse error", file.c_str(), err.c_str())) {} }; diff --git a/src/util/for_each.hpp b/src/util/for_each.hpp index 4422d7f0..16132d04 100644 --- a/src/util/for_each.hpp +++ b/src/util/for_each.hpp @@ -21,6 +21,8 @@ #ifdef __GNUC__ // GCC has a buildin typeof function, so it doesn't need (as much) hacks #define DECLARE_TYPEOF(T) + #define DECLARE_TYPEOF_NO_REV(T) + #define DECLARE_TYPEOF_CONST(T) #define DECLARE_TYPEOF_COLLECTION(T) #define TYPEOF(Value) __typeof(Value) diff --git a/src/util/index_map.hpp b/src/util/index_map.hpp index 7b363ccc..caefbb8d 100644 --- a/src/util/index_map.hpp +++ b/src/util/index_map.hpp @@ -46,7 +46,7 @@ class IndexMap : private vector { void init(const vector& keys) { if (this->size() == keys.size()) return; this->reserve(keys.size()); - for(vector::const_iterator it = keys.begin() ; it != keys.end() ; ++it) { + for(typename vector::const_iterator it = keys.begin() ; it != keys.end() ; ++it) { const Key& key = *it; assert(key); if (key->index >= this->size()) this->resize(key->index + 1); @@ -82,8 +82,8 @@ class IndexMap : private vector { /// Find a value given the key name, return an iterator template - const_iterator find(const Name& key) const { - for(vector::const_iterator it = begin() ; it != end() ; ++it) { + typename vector::const_iterator find(const Name& key) const { + for(typename vector::const_iterator it = begin() ; it != end() ; ++it) { if (get_key_name(*it) == key) return it; } return end(); diff --git a/src/util/io/package.hpp b/src/util/io/package.hpp index 88ca9781..d978479b 100644 --- a/src/util/io/package.hpp +++ b/src/util/io/package.hpp @@ -11,6 +11,7 @@ #include #include +#include class Package; class wxFileInputStream; diff --git a/src/util/prec.hpp b/src/util/prec.hpp index 8287757f..a46978d1 100644 --- a/src/util/prec.hpp +++ b/src/util/prec.hpp @@ -25,6 +25,7 @@ // Wx headers #include #include +#include #include #include diff --git a/src/util/smart_ptr.hpp b/src/util/smart_ptr.hpp index b122313d..198e97f2 100644 --- a/src/util/smart_ptr.hpp +++ b/src/util/smart_ptr.hpp @@ -14,9 +14,11 @@ // ----------------------------------------------------------------------------- : Includes -// MOVEME -/// Using intrusive_ptr where possible? (as opposed to smart_ptr) -#define USE_INTRUSIVE_PTR +#include +#ifdef HAVE_FAST_ATOMIC + /// Using intrusive_ptr where possible? (as opposed to smart_ptr) + #define USE_INTRUSIVE_PTR +#endif #include #ifdef USE_INTRUSIVE_PTR @@ -99,7 +101,31 @@ inline shared_ptr new_shared7(const A0& a0, const A1& a1, const A2& a2, const inline intrusive_ptr new_intrusive2(const A0& a0, const A1& a1) { return intrusive_ptr(new T(a0, a1)); } - + + /// Base class for objects wishing to use intrusive_ptrs + class IntrusivePtrBase { + public: + virtual ~IntrusivePtrBase(); + protected: + /// Delete this object + virtual void destroy() { + delete this; + } + private: + volatile AtomicInt ref_count; + friend void intrusive_ptr_add_ref(IntrusivePtrBase*); + friend void intrusive_ptr_release(IntrusivePtrBase*); + }; + + inline void intrusive_ptr_add_ref(IntrusivePtrBase* p) { + ++p->ref_count; + } + inline void intrusive_ptr_release(IntrusivePtrBase* p) { + if (--p->ref_count == 0) { + p->destroy(); + } + } + #else #define DECLARE_INTRUSIVE_POINTER_TYPE DECLARE_POINTER_TYPE #define intrusive_ptr shared_ptr @@ -107,6 +133,17 @@ inline shared_ptr new_shared7(const A0& a0, const A1& a1, const A2& a2, const #define new_intrusive1 new_shared1 #define new_intrusive2 new_shared2 #define new_intrusive3 new_shared3 + + class IntrusivePtrBase { + public: + virtual ~IntrusivePtrBase(); + protected: + /// Delete this object + virtual void destroy() { + delete this; + } + }; + #endif // ----------------------------------------------------------------------------- : EOF diff --git a/src/util/vector2d.hpp b/src/util/vector2d.hpp index c0c2102d..cbf9b519 100644 --- a/src/util/vector2d.hpp +++ b/src/util/vector2d.hpp @@ -18,7 +18,7 @@ /** Intentionally uses slightly less then 0.5, to give a more consistent result * when for instance something like "x/2" is used. */ inline int to_int(double d) { - return d > 0 ? d + 0.4999995 : d - 0.4999995; + return static_cast(d > 0 ? d + 0.4999995 : d - 0.4999995); } // ----------------------------------------------------------------------------- : Vector2D