00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef CLIPPER_MEMORY
00046 #define CLIPPER_MEMORY
00047
00048
00049 #include "clipper_message.h"
00050 #include <vector>
00051
00052
00053 namespace clipper
00054 {
00055
00057 class Property_base
00058 {
00059 public:
00060 virtual Property_base* clone() const = 0;
00061 virtual ~Property_base() {};
00062 };
00063
00065 template<class T> class Property : public Property_base
00066 {
00067 public:
00069 explicit Property( const T& val ) { val_ = val; }
00070 ~Property() {}
00071 Property_base* clone() const { return new Property<T>( *this ); }
00072 const T& value() const { return val_; }
00073 private:
00074 T val_;
00075 };
00076
00078
00095 class PropertyManager
00096 {
00097 public:
00098 PropertyManager() {}
00099 PropertyManager( const PropertyManager& mgr );
00100 PropertyManager& operator =( const PropertyManager& mgr );
00101 ~PropertyManager();
00102 PropertyManager& copy( const PropertyManager& mgr );
00103
00104 bool set_property(const std::string& label, const Property_base& property);
00106 const Property_base& get_property( const std::string& label ) const;
00107 bool exists_property(const std::string& label) const;
00108 bool delete_property(const std::string& label);
00109 private:
00110 std::vector<std::pair<std::string,Property_base*> > property_;
00111 };
00112
00113
00115
00141 template<class T> class ObjectCache
00142 {
00143 public:
00145 class Reference
00146 {
00147 public:
00148 Reference() : obj_(NULL) {}
00149 Reference( std::pair<int,T>* obj ) : obj_(NULL) { ref( obj ); }
00150 Reference( const Reference& other ) : obj_(NULL) { ref( other.obj_ ); }
00151 ~Reference() { ref( NULL ); }
00152 Reference& operator =( const Reference& other )
00153 { ref( other.obj_ ); return *this; }
00154 bool is_null() const { return obj_ == NULL; }
00155 const T& data() const { return obj_->second; }
00156 private:
00157 std::pair<int,T>* obj_;
00159 void ref( std::pair<int,T>* obj )
00160 { if ( !is_null() ) obj_->first--;
00161 obj_ = obj;
00162 if ( !is_null() ) obj_->first++; }
00163 };
00164
00165 enum MODE { NORMAL, MINMEM, MAXMEM };
00166 ObjectCache();
00167 ~ObjectCache();
00168 void set_mode( const MODE& mode );
00169 void purge();
00170 void debug() const;
00172 template<class K> Reference cache( const K& key )
00173 {
00174 for ( int i = 0; i < cache_.size(); i++ )
00175 if ( cache_[i]->second.matches(key) )
00176 return Reference( cache_[i] );
00177
00178 if ( mode_ == MINMEM )
00179 purge();
00180 else if ( mode_ == NORMAL )
00181 for ( int i = 0; i < cache_.size(); i++ )
00182 if ( cache_[i]->first == 0 ) {
00183 cache_[i]->second = T(key);
00184 return Reference( cache_[i] );
00185 }
00186
00187 cache_.push_back( new std::pair<int,T>( 0, T(key) ) );
00188 return Reference( cache_.back() );
00189 }
00190 private:
00191 std::vector<std::pair<int,T>*> cache_;
00192 MODE mode_;
00193
00194 friend class ObjectCache<T>::Reference;
00195 };
00196
00197
00198
00199
00200 template<class T> ObjectCache<T>::ObjectCache()
00201 { mode_ = NORMAL; }
00202
00203 template<class T> ObjectCache<T>::~ObjectCache()
00204 {
00205 for ( int i = 0; i < cache_.size(); i++ ) {
00206 if ( cache_[i]->first != 0 ) {
00207 std::string num( "0000" );
00208 num[3] += cache_[i]->first % 10;
00209 num[2] += cache_[i]->first / 10 % 10;
00210 num[1] += cache_[i]->first / 100 % 10;
00211 num[0] += cache_[i]->first / 1000;
00212 Message::message( Message_warn( "ObjectCache: Leaked "+num+" refs to <"+cache_[i]->second.format()+">" ) );
00213 }
00214 }
00215 }
00216
00225 template<class T> void ObjectCache<T>::set_mode( const MODE& mode )
00226 { mode_ = mode; }
00227
00231 template<class T> void ObjectCache<T>::purge()
00232 {
00233 for ( int i = cache_.size() - 1; i >= 0; i-- )
00234 if ( cache_[i]->first == 0 ) {
00235 delete cache_[i];
00236 cache_.erase( cache_.begin() + i );
00237 }
00238 }
00239
00240
00241
00242
00243
00244
00245 template<class T> void ObjectCache<T>::debug() const
00246 {
00247 for ( int i = 0; i < cache_.size(); i++ )
00248 std::cout << "Cache pos: " << i << "\t Refs: " << cache_[i]->first
00249 << "\t" << cache_[i]->second.format() << "\n";
00250 }
00251
00252
00253 }
00254
00255 #endif