public
Last active

FXAutoPtr.h patch for FOX toolkit 1.7, to provide capability of returning FXAutoPtr stuff from methods.

  • Download Gist
FXAutoPtr.h.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
--- FXAutoPtr.h 2011-04-13 02:13:56.000000000 +0200
+++ /tmp/FXAutoPtr.h 2011-07-14 21:08:28.400223296 +0200
@@ -23,6 +23,20 @@
namespace FX {
+/**
+ * A wrapper class to provide FXAutoPtr with reference semantics.
+ * For example, an FXAutoPtr can be assigned (or constructed from)
+ * the result of a function which returns an FXAutoPtr by value.
+ *
+ * All the FXAutoPtrRef stuff should happen behind the scenes.
+ */
+template<typename EType>
+struct FXAutoPtrRef
+{
+ EType* ptr;
+
+ explicit FXAutoPtrRef(EType* src): ptr(src) { }
+};
/// Automatic pointer
template <class EType> class FXAutoPtr {
@@ -31,13 +45,13 @@
public:
/// Construct from optional pointer
- FXAutoPtr(EType* src=NULL):ptr(src){ }
+ explicit FXAutoPtr(EType* src=NULL):ptr(src){ }
/// Construct from another automatic pointer
FXAutoPtr(FXAutoPtr<EType>& src):ptr(src.release()){ }
/// Construct from another automatic pointer of compatible type
- template <class T> FXAutoPtr(FXAutoPtr<T>& src):ptr(src.release()){ }
+ template <class T> explicit FXAutoPtr(FXAutoPtr<T>& src):ptr(src.release()){ }
/// Assign from pointer
FXAutoPtr& operator=(EType *src){ ptr=src; return *this; }
@@ -68,6 +82,27 @@
/// Destruction deletes pointer
~FXAutoPtr(){ delete ptr; }
+
+ /**
+ * @brief Automatic conversions
+ *
+ * These operations convert an %FXAutoPtr into and from an FXAutoPtrRef
+ * automatically as needed. This allows constructs such as
+ * @code
+ * FXAutoPtr<Derived> getFXAutoPtr(.....);
+ * ...
+ * FXAutoPtr<Base> ptr = getFXAutoPtr(.....);
+ * @endcode
+ */
+
+ FXAutoPtr(FXAutoPtrRef<EType> src) : ptr(src.ptr) { }
+
+ FXAutoPtr& operator=(FXAutoPtrRef<EType> src) { if (src.ptr != this->get()){delete ptr;ptr = src.ptr;}return *this;}
+
+ template<typename EType1> operator FXAutoPtrRef<EType1>() throw() { return FXAutoPtrRef<EType1>(this->release()); }
+
+ template<typename EType1> operator FXAutoPtr<EType1>() throw() { return FXAutoPtr<EType1>(this->release()); }
+
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.