From 3d4210107b4bed586798423dd2a0cee80a3699bb Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 24 Feb 2010 14:01:34 +0000 Subject: [PATCH] Geom/Coords: Removed dependency on TCore git-svn-id: svn://ultimatepp.org/upp/trunk@2143 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Geom/Coords/Coords.upp | 1 - uppsrc/Geom/Coords/GeomCoords.h | 146 ++++++++++++++++++++++++++++++-- uppsrc/Geom/Coords/coords.cpp | 14 ++- uppsrc/Geom/Coords/method.cpp | 2 +- uppsrc/TCore/setop.h | 2 +- uppsrc/TCore/template.h | 4 +- 6 files changed, 154 insertions(+), 15 deletions(-) diff --git a/uppsrc/Geom/Coords/Coords.upp b/uppsrc/Geom/Coords/Coords.upp index 3c0b8876e..8eac56533 100644 --- a/uppsrc/Geom/Coords/Coords.upp +++ b/uppsrc/Geom/Coords/Coords.upp @@ -1,5 +1,4 @@ uses - TCore, Geom; file diff --git a/uppsrc/Geom/Coords/GeomCoords.h b/uppsrc/Geom/Coords/GeomCoords.h index 6238e712a..383bc7c84 100644 --- a/uppsrc/Geom/Coords/GeomCoords.h +++ b/uppsrc/Geom/Coords/GeomCoords.h @@ -2,7 +2,7 @@ #define _Geom_Coords_Coords_h_ #include -#include +//#include NAMESPACE_UPP @@ -31,6 +31,118 @@ inline double Determinant(double a1, double a2, double a3, double b1, double b2, return a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1); } +class GeomRefBase +{ +public: + GeomRefBase() + { + refcount = 0; +#ifdef REF_DEBUG + allocindex = ++nextindex; +#endif//REF_DEBUG + } + + GeomRefBase(const GeomRefBase& rb) + { + refcount = 0; +#ifdef REF_DEBUG + allocindex = ++nextindex; +#endif//REF_DEBUG + } + + virtual ~GeomRefBase() + { + ASSERT(refcount == 0); + } + + void AddRef() const { if(this) AtomicInc(refcount); } + int GetRefCount() const { return AtomicXAdd(refcount, 0); } + void Release() const { if(this && !AtomicDec(refcount)) delete this; } +#ifdef REF_DEBUG + int GetAllocIndex() const { return allocindex; } +#endif//REF_DEBUG + +private: + mutable Atomic refcount; +#ifdef REF_DEBUG + int allocindex; + static int nextindex; +#endif//REF_DEBUG + +private: + GeomRefBase& operator = (const GeomRefBase& rb) { NEVER(); return *this; } +}; + +template +class GeomRefCon : Moveable< GeomRefCon > +{ +public: + GeomRefCon(const Nuller& = Null) : t(0) {} + GeomRefCon(const T *t); + GeomRefCon(const GeomRefCon& rp); + ~GeomRefCon(); + + void Clear() { if(t) { t->Release(); t = NULL; } } + bool IsNullInstance() const { return !t; } + dword GetHashValue() const { return UPP::GetHashValue((unsigned)(uintptr_t)t); } + + GeomRefCon& operator = (const GeomRefCon& rp); + + bool operator ! () const { return !t; } + const T *Get() const { return t; } + const T *operator ~ () const { return t; } + const T *operator -> () const { ASSERT(t); return t; } + const T& operator * () const { ASSERT(t); return *t; } + + String ToString() const { return t ? AsString(*t) : String("NULL"); } + + friend bool operator == (GeomRefCon a, GeomRefCon b) { return a.t == b.t; } + friend bool operator != (GeomRefCon a, GeomRefCon b) { return a.t != b.t; } + +protected: + const T *t; +}; + +template +GeomRefCon::GeomRefCon(const T *t) +: t(t) +{ + t->AddRef(); +#ifdef REF_DEBUG + if(t && t->GetRefCount() == 1) + RefMemStat::App().Add(typeid(*t).name(), t->GetAllocIndex()); +#endif//REF_DEBUG +} + +template +GeomRefCon::GeomRefCon(const GeomRefCon& rp) +: t(rp.t) +{ t->AddRef(); } + +template +GeomRefCon::~GeomRefCon() +{ +#ifdef REF_DEBUG + if(t && t->GetRefCount() == 1) + RefMemStat::App().Remove(typeid(*t).name(), t->GetAllocIndex()); +#endif//REF_DEBUG + t->Release(); +} + +template +GeomRefCon& GeomRefCon::operator = (const GeomRefCon& rp) +{ + const T *old = t; + t = rp.t; + t->AddRef(); +#ifdef REF_DEBUG + if(old && old->GetRefCount() == 1) + RefMemStat::App().Remove(typeid(*old).name(), old->GetAllocIndex()); +#endif//REF_DEBUG + old->Release(); + return *this; +} + class GisBSPTree { public: @@ -56,7 +168,7 @@ public: Node plus; // np - c > 0 }; - struct Tree : RefBase + struct Tree : GeomRefBase { Tree(pick_ Node& root) : root(root) {} @@ -73,7 +185,23 @@ public: const Node& GetRoot() const { return tree->root; } private: - RefCon tree; + GeomRefCon tree; +}; + +template +class GeomRefPtr : public GeomRefCon, public Moveable< GeomRefPtr > +{ +public: + GeomRefPtr(const Nuller& = Null) {} + GeomRefPtr(T *t) : GeomRefCon(t) {} + GeomRefPtr(const GeomRefPtr& rp) : GeomRefCon(rp) {} + + GeomRefPtr& operator = (const GeomRefPtr& rp) { GeomRefCon::operator = (rp); return *this; } + + T *Get() const { return const_cast(this->t); } + T *operator ~ () const { return Get(); } + T *operator -> () const { ASSERT(this->t); return Get(); } + T& operator * () const { ASSERT(this->t); return *Get(); } }; class ConvertDegree : public Convert @@ -406,7 +534,7 @@ public: String help_topic; }; - class Data : public RefBase + class Data : public GeomRefBase { public: Data(); @@ -447,7 +575,7 @@ public: }; GisCoords(const Nuller& = Null) {} - GisCoords(RefPtr data) : data(data) {} + GisCoords(GeomRefPtr data) : data(data) {} GisCoords(Data *data) : data(data) {} GisCoords(const Value& v) { if(IsTypeRaw(v)) *this = ValueTo(v); } @@ -499,7 +627,7 @@ public: static GisCoords GetEPSG(int code); public: - RefPtr data; + GeomRefPtr data; }; inline bool operator == (const GisCoords& a, const GisCoords& b) { return a.Equals(b); } @@ -508,7 +636,7 @@ inline bool operator != (const GisCoords& a, const GisCoords& b) { return !a.Equ class GisTransform : Moveable { public: - class Data : public RefBase + class Data : public GeomRefBase { public: virtual ~Data() {} @@ -541,7 +669,7 @@ public: public: GisTransform(const Nuller& = Null); - GisTransform(RefPtr data) : data(data) {} + GisTransform(GeomRefPtr data) : data(data) {} GisTransform(Data *data) : data(data) {} GisTransform(GisCoords source, GisCoords target); @@ -576,7 +704,7 @@ public: bool Equals(const GisTransform& t) const; public: - RefPtr data; + GeomRefPtr data; }; inline bool operator == (const GisTransform& a, const GisTransform& b) { return a.Equals(b); } diff --git a/uppsrc/Geom/Coords/coords.cpp b/uppsrc/Geom/Coords/coords.cpp index d3eee6bb5..f0e8b40d7 100644 --- a/uppsrc/Geom/Coords/coords.cpp +++ b/uppsrc/Geom/Coords/coords.cpp @@ -86,6 +86,18 @@ GisCoords::Arg GisCoords::Arg::DropList(String& v, String ident, String name, St return out; } +struct GeomBoolRef : public RefManager +{ + virtual int GetType() { return UNKNOWN_V; } + virtual Value GetValue(const void *x) { return *(const bool *)x ? 1 : 0; } + virtual void SetValue(void *x, const Value& v) { *(bool *)x = !UPP::IsNull(v) && (double)v; } + virtual void SetNull(void *x) { *(bool *)x = false; } + + static RefManager *Manager() { static GeomBoolRef m; return &m; } +}; + +inline Ref GeomBoolAsRef(bool& b) { return Ref(&b, GeomBoolRef::Manager()); } + GisCoords::Arg GisCoords::Arg::Option(bool& b, String ident, String name, String help_topic) { Arg out; @@ -95,7 +107,7 @@ GisCoords::Arg GisCoords::Arg::Option(bool& b, String ident, String name, String out.help_topic = help_topic; out.style = STYLE_OPTION; out.not_null = true; - out.ref = BoolAsRef(b); + out.ref = GeomBoolAsRef(b); return out; } diff --git a/uppsrc/Geom/Coords/method.cpp b/uppsrc/Geom/Coords/method.cpp index d5e5e2357..c4ac8292e 100644 --- a/uppsrc/Geom/Coords/method.cpp +++ b/uppsrc/Geom/Coords/method.cpp @@ -716,7 +716,7 @@ const Vector& GisCoords::EnumEPSG() GisCoords GisCoords::GetEPSG(int code) { - RefPtr gc; + GeomRefPtr gc; switch(code) { case 2065: diff --git a/uppsrc/TCore/setop.h b/uppsrc/TCore/setop.h index 8b68462cf..af009fb85 100644 --- a/uppsrc/TCore/setop.h +++ b/uppsrc/TCore/setop.h @@ -13,7 +13,7 @@ enum SetOpCode SET_OP2_XOR, // symmetrical difference }; -template <> inline unsigned UPP::GetHashValue(const SetOpCode& c) { return c; } +template <> inline unsigned GetHashValue(const SetOpCode& c) { return c; } NTL_MOVEABLE(SetOpCode); diff --git a/uppsrc/TCore/template.h b/uppsrc/TCore/template.h index f44b2cca5..be10f4cb5 100644 --- a/uppsrc/TCore/template.h +++ b/uppsrc/TCore/template.h @@ -117,8 +117,8 @@ protected: public: PtrValue(T *x) : Value(new Rep(x)) {} - T *Get(const Value& v) { return ::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); } - static T *Extract(const Value& v) { return ::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); } + T *Get(const Value& v) { return UPP::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); } + static T *Extract(const Value& v) { return UPP::IsNull(v) ? 0 : Rep::Cast(v.GetVoidPtr())->Get(); } }; template