Commit 9d7d05d0 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

new class template

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1787
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent afdf1697
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.16 2005-06-28 10:51:12 tforb Exp $
# $Id: Makefile,v 1.17 2005-06-30 13:55:12 tforb Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -28,7 +28,8 @@
#HEADERS=$(shell find . -name \*.h)
HEADERS=misc/range.h misc/xcmdline.h misc/error.h misc/commandline.h \
misc/complexio.h misc/misc.h misc/fortranio.h
misc/complexio.h misc/misc.h misc/fortranio.h \
containers/handle.h
#misc/blitzfortranio.h
#misc/blitzutil.h
#hacks/hack_blitztogrepg.h
......
This is a legacy version of the repository. It may be incomplete as well as
inconsistent. See README.history for details. For the old stock of the
repository copyright and licence conditions apply as specified for versions
commited after 2015-03-01. Use recent versions as a base for new development.
The legacy version is only stored to keep a record of history.
/*! \file handle.h
* \brief provide a pointer with reference counting (prototypes)
*
* ----------------------------------------------------------------------------
*
* $Id: handle.h,v 1.1 2005-06-30 13:55:12 tforb Exp $
* \author Thomas Forbriger
* \date 30/06/2005
*
* provide a pointer with reference counting (prototypes)
*
* Copyright (c) 2005 by Thomas Forbriger (BFO Schiltach)
*
* REVISIONS and CHANGES
* - 30/06/2005 V1.0 Thomas Forbriger
*
* ============================================================================
*/
// include guard
#ifndef TF_HANDLE_H_VERSION
#define TF_HANDLE_H_VERSION \
"TF_HANDLE_H V1.0 "
#define TF_HANDLE_H_CVSID \
"$Id: handle.h,v 1.1 2005-06-30 13:55:12 tforb Exp $"
namespace tf {
/** template #tf::THandle<class TObj>#:
*
* THandle is a class template published by B. Stroustrup in Chapter 25.7 of
* "The C++ programming Language" (3rd edition). This is a slightly modified
* version.
*
* Example:
* To create a handle #hs# to a string #s# and then output the string value
* you would code:
*
* #THandle<string> hs(new string(s)); cout << *hs << endl;#
*
* Thus the handle always represents a kind of pointer to your string. You may
* copy and pass this pointer as you like. And all these copies will allocated
* just memory for that pointer and any modification applied to the string
* will be accessible through every of these handles. However if the last
* handle is removed during program execution it will also remove the string
* representation from the memory. This is an convenient way to pass large
* objects without worrying about memory usage and management.
*
* It is not possible to create an empty handle - that wouldn't make any
* sense. You always have to provide another handle to copy from or an element
* to be handled.
*
* @author Bjarne Stroustrup,
* @author Thomas Forbriger
* @version V1.0 (#$Revision: 1.1 $ $Date: 2005-06-30 13:55:12 $#)
* @memo template providing a handle to an object of any type
*/
template<class X>
class Handle {
public:
typedef X Tobject;
typedef X* Tpointer;
typedef X& Treference;
/** constructor #tf::THandle<class X>::THandle#:
*
* This constructor is used in the following way:
*
* #THandle<X> handle(new X);#
*
* @memo init constructor
*/
Handle(X* p): Mrep(p), Mpcount(new int(1)) { }
/** constructor #tf::THandle<class X>::THandle#:
*
* This constructor is used in the following way together with another
* #handle2# of type #THandle<X>#:
*
* #THandle<X> handle2=handle1;#
*
* @memo copy constructor
*/
Handle(const THandle& h): Mrep(h.Mrep), Mpcount(h.Mpcount)
{ (*pcount)++; }
/** desctructor #tf::THandle<class X>::~THandle#
* @memo book-keeping destructor
*/
~Handle() { if (--(*Mpcount)==0) { delete Mrep; delete Mpcount; } }
/** operator #tf::THandle<class X>::operator->()#
* @return returns pointer to object of type #class X#
* @memo dereferencing operator
*/
Tpointer operator->() const { return Mrep; }
/** operator #tf::THandle<class X>::operator*()#
* @return returns reference to object of type #class X#
* @memo dereferencing operator
*/
Treference operator*() const { return *Mrep; }
/** operator #tf::THandle<class X>::operator=()#
* @memo book-keeping asignment operator
*/
Handle& operator=(const Handle& h)
{
if (Mrep == h.Mrep) return *this;
if (--(*Mpcount) == 0) { delete Mrep; delete Mpcount; }
Mrep=h.Mrep;
Mpcount=h.Mpcount;
(*Mpcount)++;
return *this;
}
private:
/// internal pointer to the handled object
X* Mrep;
/// usage counter
int* Mpcount;
}; // template class Handle
} // namespace tf
#endif // TF_HANDLE_H_VERSION (includeguard)
/* ----- END OF handle.h ----- */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment