2009-12-01 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / ptr.h
blob5f5d91f97c43551093717ce8d040efb798b056b0
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Contact:
4 * Moonlight List (moonlight-list@lists.ximian.com)
6 * Copyright 2007 Novell, Inc. (http://www.novell.com)
8 * See the LICENSE file included with the distribution for details.
12 #ifndef __MONO_PTR_H__
13 #define __MONO_PTR_H__
14 // debug
15 #define ds(x)
17 #include "debug.h"
19 // to prevent unwanted assignments
20 class PtrBase {
21 private:
22 void operator==(const PtrBase &b) const;
23 void operator!=(const PtrBase &b) const;
24 operator Value();
27 /*************************************************************
28 DOPtr takes ownership of a refcounted object, so it won't
29 touch the initial refcount, it will only unref when destroyed
30 **************************************************************/
31 template<class T>
32 class DOPtr : private PtrBase {
33 public:
34 DOPtr(T* ptr = 0) : value(ptr), initted(false) {
35 init();
38 ~DOPtr() {
39 ds (printf("~DOPtr %p %p %d\n", this, value, initted));
40 if (value && initted)
41 value->unref();
43 operator bool() const { return(value != 0); }
45 DOPtr* operator=(T* ptr) {
46 if (value == ptr)
47 return this;
48 T *old = value;
49 value = ptr;
50 if (old && initted) old->unref();
51 initted = false;
52 init ();
53 return this;
55 T* get() const { return value; }
56 T* operator->() const { return value; }
57 T& operator*() const { return *value; }
59 operator T*() { return value; }
61 template <class U>
62 operator U*() { return static_cast<U*> (value); }
64 private:
65 T *value;
66 bool initted;
68 void init() {
69 ds (printf("init %p %p\n", this, value));
70 if (!value)
71 return;
72 initted = true;
76 #endif