BWAPI
|
00001 // Copyright (c) 1997-2000 Max-Planck-Institute Saarbruecken (Germany). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under 00005 // the terms of the Q Public License version 1.0. 00006 // See the file LICENSE.QPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Nef_2/include/CGAL/Nef_2/geninfo.h $ 00015 // $Id: geninfo.h 41714 2008-01-20 20:24:20Z spion $ 00016 // 00017 // 00018 // Author(s) : Michael Seel <seel@mpi-sb.mpg.de> 00019 00020 #ifndef CGAL_NEF_2_GENINFO_H 00021 #define CGAL_NEF_2_GENINFO_H 00022 00023 #include <memory> 00024 00025 /*{\Moptions outfile=geninfo.man}*/ 00026 /*{\Moptions constref=yes}*/ 00027 /*{\Manpage {geninfo} {T} {Information association via GenPtr} {}}*/ 00028 00029 template <typename T> 00030 struct geninfo { 00031 typedef void* GenPtr; 00032 00033 /*{\Mdefinition |\Mname| encapsulates information association via 00034 generic pointers of type |GenPtr (=void*)|. An object |t| of type |T| 00035 is stored directly in a variable |p| of type |GenPtr| if |sizeof(T)| 00036 is not larger than |sizeof(GenPtr)| (also called word size). Otherwise 00037 |t| is allocated on the heap and referenced via |p|. This class 00038 encapsulates the technicalities, however the user always has to obey 00039 the order of its usage: |create|-|access/const_access|-|clear|. On 00040 misuse memory problems occur.}*/ 00041 00042 /*{\Moperations 2 1}*/ 00043 00044 static void create(GenPtr& p) 00045 /*{\Mstatic create a slot for an object of type |T| referenced 00046 via |p|.}*/ 00047 { if (sizeof(T) <= sizeof(GenPtr)) new((void*)(&p)) T; 00048 if (sizeof(T) > sizeof(GenPtr)) p = (GenPtr) new T; 00049 } 00050 00051 static T& access(GenPtr& p) 00052 /*{\Mstatic access an object of type |T| via |p|. 00053 \precond |p| was initialized via |create| and was not cleared 00054 via |clear|.}*/ 00055 { if (sizeof(T) <= sizeof(GenPtr)) return *(T*)(&p); 00056 else return *(T*)p; 00057 } 00058 00059 static const T& const_access(const GenPtr& p) 00060 /*{\Mstatic read-only access of an object of type |T| via |p|. 00061 \precond |p| was initialized via |create| and was not cleared 00062 via |clear|.}*/ 00063 { if (sizeof(T) <= sizeof(GenPtr)) return *(const T*)(&p); 00064 else return *(const T*)p; 00065 } 00066 00067 static void clear(GenPtr& p) 00068 /*{\Mstatic clear the memory used for the object of type |T| via 00069 |p|. \precond |p| was initialized via |create|.}*/ 00070 { if (sizeof(T) <= sizeof(GenPtr)) ((T*)(&p))->~T(); 00071 if (sizeof(T) > sizeof(GenPtr)) delete (T*) p; 00072 p=0; 00073 } 00074 00075 }; 00076 00077 /*{\Mexample In the first example we store a pair of boolean values 00078 which normally fit into one word. Thus there will no heap allocation 00079 take place. 00080 \begin{Mverb} 00081 struct A { bool a,b }; 00082 GenPtr a; 00083 geninfo<A>::create(a); 00084 A& a_access = geninfo<A>::access(a); 00085 geninfo<A>::clear(a); 00086 \end{Mverb} 00087 The second example uses the heap scheme as two longs do not fit into 00088 one word. 00089 \begin{Mverb} 00090 struct B { long a,b }; 00091 GenPtr b; 00092 geninfo<B>::create(b); 00093 B& b_access = geninfo<B>::access(b); 00094 geninfo<B>::clear(b); 00095 \end{Mverb} 00096 Note that usage of the scheme takes away with the actual check for the 00097 type size. Even more important this size might depend on the platform 00098 which is used to compile the code and thus the scheme enables platform 00099 independent programming.}*/ 00100 00101 #endif //GENINFO_H