summaryrefslogtreecommitdiffstats
path: root/das-cpp/trunk/runtime/core/include/apache/das/RefCountingPointer.h
blob: 22a2ef7f08de0ae4e75d185e16ff25aeeaaface4 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef REF_COUNTING_POINTER_H
#define REF_COUNTING_POINTER_H

#include <iostream>

#include "apache/das/DASNullPointerException.h"

namespace apache {
	namespace das {

	template <class T>
class RefCountingPointer {
	
    public:
        /*SDO_API*/ RefCountingPointer(T* realPtr = 0, bool objectOwner = true);
					RefCountingPointer(const RefCountingPointer<T>& rhs, bool objectOwner = true);
        /*SDO_API*/ RefCountingPointer(T& rhs, bool objectOwner = true);
        /*SDO_API*/ virtual ~RefCountingPointer(void);
        /*SDO_API*/ RefCountingPointer& operator=(const RefCountingPointer<T>& rhs);
				
        /*SDO_API*/ bool operator==(RefCountingPointer<T>& test) const;
        /*SDO_API*/ T* operator->() const;
        /*SDO_API*/ T& operator*() const;
        /*SDO_API*/ bool operator!() const;
					bool isObjectOwner(void) const;

#ifdef MFT
        // MFT == member function templates
        // Notes on the items below.
        // In our code, we use subclasses to expose the API, and super
        // classes to implement. E,g DataObject and DataObjectImpl.
        // In some cases, we know that the DataObject given to us is a 
        // DataObjectImpl, and cast it. With RefCountingPointers, however,
        // the cast cannot work, as the RefCountingPointer to the superclass
        // is not related to the RCP to the subclass. Recent changes in the
        // C++ language allow this to work by defining an operator which 
        // causes a pointer of the other type to be returned, as long as pointee
        // is acceptable as a parameter to the cosntructor of the other type 
        // of pointer. This works in C++.NET, but not in C++6:
        operator RefCountingPointer<otherType>()
        { 
             return RefCountingPointer<otherType>(pointee);
        }

        // Since we are using C6, a possible workround is to provide a method
        // which returns the dumb pointer, then construct a pointer to the 
        // base class from the pointer returned. This is that the operator T* does.
        // The code in DataObject could be simpler if we used C7,and we should
        // discusss changing.
#else 
        operator T*() {return pointee;}
#endif

        template <class otherType>
        operator RefCountingPointer<otherType>()
        {
            return RefCountingPointer<otherType>(pointee);
        }

        friend std::ostream& operator<< (std::ostream &os, const RefCountingPointer<T>& ptr)
        {
            if (!ptr)
            {
                os << "NULL" << std::endl;
            }
            else
            {
                ptr->printSelf(os);
            }

            return os;
        }

    private:
        T *pointee;
		bool objectOwner;
        void init();

};

	};
};

#endif //REF_COUNTING_POINTER_H