Commit 32299b97 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

the pair of handle classes appears to work well

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: 1798
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent c67ed963
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.18 2005-07-01 07:40:40 tforb Exp $
# $Id: Makefile,v 1.19 2005-07-04 13:06:09 tforb Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -41,7 +41,7 @@ SRC=misc/fortranio.cc misc/commandline.cc misc/ioswap.cc misc/error.cc \
misc/xcmdline.cc strings/stringfunc.cc
#TESTS=$(filter-out tests/blitztest,$(wildcard tests/*.cc))
TESTS=tests/commandlinetest.cc tests/fortraniotest.cc \
tests/stringtest.cc
tests/stringtest.cc tests/handletest.cc
#tests/blitztest.cc
FTESTS=$(wildcard tests/*.f)
README=$(shell find . -name README) README.xtra
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: handle.h,v 1.2 2005-06-30 13:59:34 tforb Exp $
* $Id: handle.h,v 1.3 2005-07-04 13:06:10 tforb Exp $
* \author Thomas Forbriger
* \date 30/06/2005
*
......@@ -23,7 +23,7 @@
#define TF_HANDLE_H_VERSION \
"TF_HANDLE_H V1.0 "
#define TF_HANDLE_H_CVSID \
"$Id: handle.h,v 1.2 2005-06-30 13:59:34 tforb Exp $"
"$Id: handle.h,v 1.3 2005-07-04 13:06:10 tforb Exp $"
namespace tfxx {
......@@ -51,17 +51,23 @@ namespace tfxx {
* sense. You always have to provide another handle to copy from or an element
* to be handled.
*
* \sa tfxx::Handle
*
* @author Bjarne Stroustrup,
* @author Thomas Forbriger
* @version V1.0 (#$Revision: 1.2 $ $Date: 2005-06-30 13:59:34 $#)
* @version V1.0 (#$Revision: 1.3 $ $Date: 2005-07-04 13:06:10 $#)
* @memo template providing a handle to an object of any type
*/
template<class X>
class Handle {
class ConstHandle {
public:
typedef X Tobject;
typedef X* Tpointer;
typedef X& Treference;
typedef X Tmutableobject;
typedef const X Tobject;
typedef ConstHandle<Tobject> Tcontainer;
typedef Tcontainer Tcoc;
typedef Tcoc Tcontainer_of_const;
typedef const Tobject* Tpointer;
typedef const Tobject& Treference;
/** constructor #tfxx::THandle<class X>::THandle#:
*
......@@ -70,8 +76,19 @@ class Handle {
* #THandle<X> handle(new X);#
*
* @memo init constructor
ConstHandle(Tobject* p): Mrep(p), Mpcount(new int(1)) { }
*/
Handle(X* p): Mrep(p), Mpcount(new int(1)) { }
/** constructor #tfxx::THandle<class X>::THandle#:
*
* This constructor is used in the following way:
*
* #THandle<X> handle(X);#
*
* @memo init constructor
*/
ConstHandle(Treference p):
Mrep(new Tmutableobject(p)), Mpcount(new int(1)) { }
/** constructor #tfxx::THandle<class X>::THandle#:
*
......@@ -82,13 +99,13 @@ class Handle {
*
* @memo copy constructor
*/
Handle(const THandle& h): Mrep(h.Mrep), Mpcount(h.Mpcount)
{ (*pcount)++; }
ConstHandle(const ConstHandle& h): Mrep(h.Mrep), Mpcount(h.Mpcount)
{ (*Mpcount)++; }
/** desctructor #tfxx::THandle<class X>::~THandle#
* @memo book-keeping destructor
*/
~Handle() { if (--(*Mpcount)==0) { delete Mrep; delete Mpcount; } }
~ConstHandle() { if (--(*Mpcount)==0) { delete Mrep; delete Mpcount; } }
/** operator #tfxx::THandle<class X>::operator->()#
* @return returns pointer to object of type #class X#
......@@ -105,7 +122,7 @@ class Handle {
/** operator #tfxx::THandle<class X>::operator=()#
* @memo book-keeping asignment operator
*/
Handle& operator=(const Handle& h)
ConstHandle& operator=(const ConstHandle& h)
{
if (Mrep == h.Mrep) return *this;
if (--(*Mpcount) == 0) { delete Mrep; delete Mpcount; }
......@@ -115,11 +132,80 @@ class Handle {
return *this;
}
protected:
//! expose the pointer to derived classes
Tmutableobject* pointer() const { return Mrep;}
//! expose the object to derived classes
Tmutableobject& reference() const { return *Mrep;}
private:
/// internal pointer to the handled object
X* Mrep;
Tmutableobject* Mrep;
/// usage counter
int* Mpcount;
}; // template class ConstHandle
/*! The handle class
*/
template<class X>
class Handle: public ConstHandle<X> {
public:
typedef X Tobject;
typedef ConstHandle<Tobject> Tbase;
typedef Handle<Tobject> Tcontainer;
typedef Tbase Tcoc;
typedef Tcoc Tcontainer_of_const;
typedef Tobject* Tpointer;
typedef Tobject& Treference;
/** constructor #tfxx::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)) { }
*/
Handle(const Treference p): Tbase(p) { }
/** constructor #tfxx::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 Handle& h): Tbase(h) { }
/** desctructor #tfxx::THandle<class X>::~THandle#
* @memo book-keeping destructor
~Handle() { }
*/
/** operator #tfxx::THandle<class X>::operator->()#
* @return returns pointer to object of type #class X#
* @memo dereferencing operator
*/
Tpointer operator->() const { return Tbase::pointer(); }
/** operator #tfxx::THandle<class X>::operator*()#
* @return returns reference to object of type #class X#
* @memo dereferencing operator
*/
Treference operator*() const { return Tbase::reference(); }
/** operator #tfxx::THandle<class X>::operator=()#
* @memo book-keeping asignment operator
*/
Handle& operator=(const Handle& h)
{
Tbase::operator=(h);
return *this;
}
}; // template class Handle
} // namespace tfxx
......
This diff is collapsed.
This diff is collapsed.
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.9 2005-07-01 07:40:42 tforb Exp $
# $Id: Makefile,v 1.10 2005-07-04 13:06:10 tforb Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -46,7 +46,8 @@ clean: ;
# all that only need templates from the library
STANDARDTEST=
# all that need the binary library
BINARYTEST=commandlinetest fortraniotest blitztest stringtest
BINARYTEST=commandlinetest fortraniotest blitztest stringtest \
handletest
# F77 part of the I/O test
FORTRANTEST=fortranF77
......
/*! \file handletest.cc
* \brief test handle containers
*
* ----------------------------------------------------------------------------
*
* $Id: handletest.cc,v 1.1 2005-07-04 13:06:11 tforb Exp $
* \author Thomas Forbriger
* \date 04/07/2005
*
* test handle containers
*
* Copyright (c) 2005 by Thomas Forbriger (BFO Schiltach)
*
* REVISIONS and CHANGES
* - 04/07/2005 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define HANDLETEST_VERSION \
"HANDLETEST V1.0 test handle containers"
#define HANDLETEST_CVSID \
"$Id: handletest.cc,v 1.1 2005-07-04 13:06:11 tforb Exp $"
#include <iostream>
#include <tfxx/commandline.h>
#include <tfxx/handle.h>
#include <tfxx/range.h>
using std::cout;
using std::cerr;
using std::endl;
//! Show code along with output of executed code.
#define CODE(line) cout.width(50); cout << endl << #line << "; --> "; line
//! Show code that does not compile.
#define ILLEGAL(line) cout.width(50); cout << #line << \
"; !!! does not compile!" << endl;
typedef tfxx::Range<int> Tirange;
std::ostream& operator << (std::ostream& os, const Tirange& r)
{ os << r.first() << "-" << r.last(); }
std::ostream& operator << (std::ostream& os,
const tfxx::ConstHandle<Tirange>& r)
{ os << r->first() << "-" << r->last(); }
int main(int iargc, char* argv[])
{
// define usage information
char usage_text[]=
{
HANDLETEST_VERSION "\n"
"usage: handletest" "\n"
" or: handletest --help|-h" "\n"
};
// define full help text
char help_text[]=
{
HANDLETEST_CVSID
};
// define commandline options
using namespace tfxx::cmdline;
static Declare options[]=
{
// 0: print help
{"help",arg_no,"-"},
// 1: verbose mode
{"v",arg_no,"-"},
{NULL}
};
// no arguments? print usage...
if (iargc<1)
{
cerr << usage_text << endl;
exit(0);
}
// collect options from commandline
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
CODE( Tirange r1(4,8) );
CODE( cout << r1 << endl );
CODE( typedef tfxx::Handle<Tirange> Tirhandle );
CODE( Tirhandle h1(r1) );
CODE( cout << *h1 << endl );
CODE( Tirhandle h2(h1) );
CODE( cout << *h2 << endl );
CODE( *h1=Tirange(12,34) );
CODE( cout << *h2 << endl );
CODE( h2->shift(-12) );
CODE( cout << *h1 << endl );
CODE( Tirhandle::Tcoc ch1(h1) );
CODE( cout << *ch1 << endl );
CODE( h2->shift(10) );
CODE( cout << *ch1 << endl );
ILLEGAL( ch1->shift(10) );
CODE( cout << *h1 << ", " << h1 << endl );
CODE( cout << *ch1 << ", " << ch1 << endl );
}
/* ----- END OF handletest.cc ----- */
Markdown is supported
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