agmemory.hh
00001 #ifndef DUNE_AGMEMORY_HH
00002 #define DUNE_AGMEMORY_HH
00003
00004 #if HAVE_ALBERTA
00005
00006 #include <stack>
00007
00008 #include <dune/grid/albertagrid/albertaheader.hh>
00009
00010 namespace Dune
00011 {
00012
00014 template <class Object>
00015 class AGMemoryProvider
00016 {
00017 std::stack < Object * > objStack_;
00018
00019 typedef AGMemoryProvider < Object > MyType;
00020 public:
00021 typedef Object ObjectType;
00022
00024 AGMemoryProvider() {};
00025
00027 ~AGMemoryProvider ();
00028
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00047 template <class GridType, class ObjectImp>
00048 ObjectType * getNewObjectEntity(const GridType &grid,
00049 const ObjectImp * fakePointer );
00050
00052 template <class GridType>
00053 ObjectType * getObject(const GridType &grid, int level);
00054
00056 ObjectType * getObjectCopy(const ObjectType & org);
00057
00059 void freeObjectEntity (ObjectType * obj);
00060
00062 void freeObject (ObjectType * obj) { freeObjectEntity(obj); }
00063
00064 private:
00066 AGMemoryProvider(const AGMemoryProvider<Object> & org);
00067 };
00068
00069
00070
00071
00072
00073
00074
00075 template <class Object> template <class GridType, class ObjectImp>
00076 inline typename AGMemoryProvider<Object>::ObjectType *
00077 AGMemoryProvider< Object >
00078 ::getNewObjectEntity ( const GridType &grid, const ObjectImp *fakePointer )
00079 {
00080 if( objStack_.empty() )
00081 {
00082 return new Object( ObjectImp( grid ) );
00083 }
00084 else
00085 {
00086 ObjectType * obj = objStack_.top();
00087 objStack_.pop();
00088 return obj;
00089 }
00090 }
00091
00092 template <class Object> template <class GridType>
00093 inline typename AGMemoryProvider<Object>::ObjectType *
00094 AGMemoryProvider<Object>::getObject
00095 (const GridType &grid, int level )
00096 {
00097 if( objStack_.empty() )
00098 {
00099 return ( new Object (grid,level) );
00100 }
00101 else
00102 {
00103 ObjectType * obj = objStack_.top();
00104 objStack_.pop();
00105 return obj;
00106 }
00107 }
00108
00109 template <class Object>
00110 inline typename AGMemoryProvider<Object>::ObjectType *
00111 AGMemoryProvider<Object>::getObjectCopy
00112 (const ObjectType & org)
00113 {
00114 if( objStack_.empty() )
00115 {
00116 return ( new Object (org));
00117 }
00118 else
00119 {
00120 ObjectType * obj = objStack_.top();
00121 objStack_.pop();
00122 return obj;
00123 }
00124 }
00125
00126 template <class Object>
00127 inline AGMemoryProvider<Object>::~AGMemoryProvider()
00128 {
00129 while ( !objStack_.empty() )
00130 {
00131 ObjectType * obj = objStack_.top();
00132 objStack_.pop();
00133 if( obj ) delete obj;
00134 }
00135 }
00136
00137 template <class Object>
00138 inline void AGMemoryProvider<Object>::freeObjectEntity(Object * obj)
00139 {
00140 objStack_.push( obj );
00141 }
00142
00143 }
00144
00145 #endif // HAVE_ALBERTA
00146
00147 #endif