Skip to content

Instantly share code, notes, and snippets.

@ss1978
Created July 15, 2011 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ss1978/1084340 to your computer and use it in GitHub Desktop.
Save ss1978/1084340 to your computer and use it in GitHub Desktop.
FXAutoPtr.h patch for FOX toolkit 1.7, to provide capability of returning FXAutoPtr stuff from methods.
--- 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()); }
+
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment