Clipper
clipper_memory.h
1 
4 //C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5 //L
6 //L This library is free software and is distributed under the terms
7 //L and conditions of version 2.1 of the GNU Lesser General Public
8 //L Licence (LGPL) with the following additional clause:
9 //L
10 //L `You may also combine or link a "work that uses the Library" to
11 //L produce a work containing portions of the Library, and distribute
12 //L that work under terms of your choice, provided that you give
13 //L prominent notice with each copy of the work that the specified
14 //L version of the Library is used in it, and that you include or
15 //L provide public access to the complete corresponding
16 //L machine-readable source code for the Library including whatever
17 //L changes were used in the work. (i.e. If you make changes to the
18 //L Library you must distribute those, but you do not need to
19 //L distribute source or object code to those portions of the work
20 //L not covered by this licence.)'
21 //L
22 //L Note that this clause grants an additional right and does not impose
23 //L any additional restriction, and so does not affect compatibility
24 //L with the GNU General Public Licence (GPL). If you wish to negotiate
25 //L other terms, please contact the maintainer.
26 //L
27 //L You can redistribute it and/or modify the library under the terms of
28 //L the GNU Lesser General Public License as published by the Free Software
29 //L Foundation; either version 2.1 of the License, or (at your option) any
30 //L later version.
31 //L
32 //L This library is distributed in the hope that it will be useful, but
33 //L WITHOUT ANY WARRANTY; without even the implied warranty of
34 //L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 //L Lesser General Public License for more details.
36 //L
37 //L You should have received a copy of the CCP4 licence and/or GNU
38 //L Lesser General Public License along with this library; if not, write
39 //L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40 //L The GNU Lesser General Public can also be obtained by writing to the
41 //L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 //L MA 02111-1307 USA
43 
44 
45 #ifndef CLIPPER_MEMORY
46 #define CLIPPER_MEMORY
47 
48 
49 #include "clipper_thread.h"
50 #include <vector>
51 
52 
53 namespace clipper
54 {
55 
58  {
59  public:
60  virtual Property_base* clone() const = 0;
61  virtual ~Property_base() {};
62  };
63 
65  template<class T> class Property : public Property_base
66  {
67  public:
69  explicit Property( const T& val ) { val_ = val; }
70  ~Property() {}
71  Property_base* clone() const { return new Property<T>( *this ); }
72  const T& value() const { return val_; }
73  private:
74  T val_;
75  };
76 
78 
96  {
97  public:
99  PropertyManager( const PropertyManager& mgr );
101  ~PropertyManager();
102  PropertyManager& copy( const PropertyManager& mgr );
103  bool set_property(const std::string& label, const Property_base& property);
106  const Property_base& get_property( const std::string& label ) const;
107  bool exists_property(const std::string& label) const;
108  bool delete_property(const std::string& label);
109  private:
110  std::vector<std::pair<std::string,Property_base*> > property_;
111  //static Mutex mutex; //!< thread safety
112  };
113 
114 
116 
150  template<class T> class ObjectCache
151  {
152  public:
154  class Reference
155  {
156  public:
157  Reference() : obj_(NULL) {}
158  Reference( const Reference& other );
159  ~Reference();
160  void operator =( const Reference& other );
161  bool is_null() const { return obj_ == NULL; }
162  const T& data() const { return obj_->second; }
163  private:
164  std::pair<int,T>* obj_;
166  Reference( std::pair<int,T>* obj ) { obj_ = obj; obj_->first++; }
167 
168  friend class ObjectCache<T>;
169  };
170 
171  enum MODE { NORMAL, MINMEM, MAXMEM };
172  ObjectCache();
173  ~ObjectCache();
174  void set_mode( const MODE& mode );
175  void purge();
176  void destroy();
177  void debug() const;
179  Reference cache( const typename T::Key& key );
180  private:
181  std::vector<std::pair<int,T>*> cache_;
182  MODE mode_;
183  };
184 
185 
186  // for template implementations, see clipper_instance.cpp
187 
188 
189 } // namespace clipper
190 
191 #endif
bool exists_property(const std::string &label) const
test for property
Definition: clipper_memory.cpp:112
~PropertyManager()
destructor
Definition: clipper_memory.cpp:60
PropertyManager & operator=(const PropertyManager &mgr)
assign op
Definition: clipper_memory.cpp:56
bool set_property(const std::string &label, const Property_base &property)
add a labelled property to the list
Definition: clipper_memory.cpp:85
Reference cache(const typename T::Key &key)
cache or return data by key
Definition: clipper_instance.cpp:91
Object Cache manager.
Definition: clipper_memory.h:150
const Property_base & get_property(const std::string &label) const
get a labelled property from the list
Definition: clipper_memory.cpp:95
bool delete_property(const std::string &label)
delete property
Definition: clipper_memory.cpp:127
ObjectCache reference class.
Definition: clipper_memory.h:154
void destroy()
VERY DANGEROUS, DO NOT USE.
Definition: clipper_instance.cpp:134
PropertyManager & copy(const PropertyManager &mgr)
copy manager
Definition: clipper_memory.cpp:70
Property(const T &val)
constructor: takes contents
Definition: clipper_memory.h:69
void purge()
purge unreferenced objects from cache
Definition: clipper_instance.cpp:125
Template for a property holding an arbitrary type.
Definition: clipper_memory.h:65
void set_mode(const MODE &mode)
set garbage collection mode
Definition: clipper_instance.cpp:122
~ObjectCache()
destructor, can message on contents
Definition: clipper_instance.cpp:77
Base class for properties of arbitrary types.
Definition: clipper_memory.h:57
Property_base * clone() const
factory copy method
Definition: clipper_memory.h:71
virtual Property_base * clone() const =0
factory copy method
const T & value() const
return value of contents
Definition: clipper_memory.h:72
Class for holding a list of labelled properties of arbitrary types.
Definition: clipper_memory.h:95
PropertyManager()
null constructor
Definition: clipper_memory.h:98
ObjectCache()
constructor
Definition: clipper_instance.cpp:74