/* * 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 #include "apache/das/DASNullPointerException.h" namespace apache { namespace das { template class RefCountingPointer { public: /*SDO_API*/ RefCountingPointer(T* realPtr = 0, bool objectOwner = true); RefCountingPointer(const RefCountingPointer& rhs, bool objectOwner = true); /*SDO_API*/ RefCountingPointer(T& rhs, bool objectOwner = true); /*SDO_API*/ virtual ~RefCountingPointer(void); /*SDO_API*/ RefCountingPointer& operator=(const RefCountingPointer& rhs); /*SDO_API*/ bool operator==(RefCountingPointer& 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() { return RefCountingPointer(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 operator RefCountingPointer() { return RefCountingPointer(pointee); } friend std::ostream& operator<< (std::ostream &os, const RefCountingPointer& 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