On Thu, 2 Oct 2003 11:58:49 +0200 (Romance Daylight Time) Vadim Zeitlin
<[EMAIL PROTECTED]> wrote:
> On Thu, 2 Oct 2003 11:44:04 +0200 (CEST) Robert Vazan <[EMAIL PROTECTED]> wrote:
>
> RV> These macros cannot be used in headers,
>
> Sorry, why? I do use them in the headers (just do grep...), what's the
> problem?
Using them excessively in headers would lead to awful header dependencies.
> Indeed, these macros don't support fwd declarations (just as
> std::auto_ptr). Only boost::shared_ptr<> (or equivalent) would allow this
Boost's shared_ptr requires complete types at point of instantiation. This
makes it impossible to define functions manipulating smart pointers as
inlined methods in headers. It is possible to make our smart pointer behave
like bare pointer with few macros.
> RV> So changes that I would want to make are (a) use templates, (b) allow
> RV> forward declarations, and (c) use separate type for return values. Only (b)
> RV> is essential to make smart pointers useful, but (a) and (c) make it better.
>
> Sorry, what do you mean by (c)?
It gives performance improvements. Return pointer with different type can
have different, more efficient assignments. Assigning from return pointer
to normal pointer or to another return pointer transfers ownership skipping
one IncRef/DecRef round.
> I agree that (b) would be nice (although not essential: you can use raw
> pointers as members as you don't risk to leak them -- there is just one
> DecRef() in dtor to add
It's on different place than smart pointer declaration. That's the same as
new/delete pair.
> -- the smart pointers arem ore useful elsewhere,
> i.e. for the stack pointers) but I'm still worried about (a). We can try it
> but I think it would still be more prudent to start by rewriting
> DEFINE_AUTOPTR() using templates first and comparing the compile times.
Gcc gives very little difference. Macros compile in 12 minutes and 43
seconds. Templates compile in 12 minutes and 38 seconds. I am attaching
patch so you can try it with Visual C++.
> Also, please don't forget that I'm using VC6 which is not the best
> compiler from the point of view of template support...
I think of templates as comfortable macros. Compiler that inlines all
template code is good enough for templates that I would write (not counting
this patch -- it's intentionally similar to macro version).
Index: include/MObject.h
===================================================================
RCS file: /cvsroot/mahogany/M/include/MObject.h,v
retrieving revision 1.29
diff -u -2 -r1.29 MObject.h
--- include/MObject.h 27 Sep 2003 17:18:06 -0000 1.29
+++ include/MObject.h 2 Oct 2003 16:10:01 -0000
@@ -260,7 +260,51 @@
// declare a class which is an auto ptr to the given MObjectRC-derived type
-#define DECLARE_AUTOPTR(classname) \
- BEGIN_DECLARE_AUTOPTR(classname) \
- END_DECLARE_AUTOPTR()
+//#define DECLARE_AUTOPTR(classname)
+// BEGIN_DECLARE_AUTOPTR(classname)
+// END_DECLARE_AUTOPTR()
+
+template<class Type> class Pointer
+{
+public:
+ Pointer(Type *ptr = NULL) { m_ptr = ptr; }
+
+ void Attach(Type *ptr)
+ {
+ ASSERT_MSG( !m_ptr, _T("should have used Detach() first") );
+
+ m_ptr = ptr;
+ }
+
+ Type *Detach()
+ {
+ Type *ptr = m_ptr;
+ m_ptr = NULL;
+ return ptr;
+ }
+
+ Type *Get() const { return m_ptr; }
+
+ Type *operator->() const { return Get(); }
+
+ void Swap(Pointer<Type>& other)
+ {
+ Type *tmp = other.m_ptr;
+ other.m_ptr = m_ptr;
+ m_ptr = tmp;
+ }
+
+private:
+ Type *m_ptr;
+
+ Pointer(const Pointer<Type> &);
+ Pointer<Type>& operator=(const Pointer<Type> &);
+
+public:
+ ~Pointer() { if ( m_ptr ) m_ptr->DecRef(); }
+ operator bool() const { return m_ptr != NULL; }
+};
+
+#define DECLARE_AUTOPTR(classname) \
+ typedef Pointer<classname> classname##_obj;
// declare an auto ptr with implicit conversion to its real pointer class:
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mahogany-Developers mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/mahogany-developers