]> Dogcows Code - chaz/yoink/blob - src/moof/manager.hh
remove some unused stlplus modules
[chaz/yoink] / src / moof / manager.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_MANAGER_HH_
11 #define _MOOF_MANAGER_HH_
12
13 /**
14 * \file manager.hh
15 * A manager is a collection of named objects of the same type. Libraries
16 * use reference counting to automagically delete objects which no longer
17 * have any interested code.
18 */
19
20 #include <string>
21
22 #include <boost/shared_ptr.hpp>
23
24 #include <moof/hash.hh>
25
26
27 namespace moof {
28
29
30 template <class T>
31 class manager
32 {
33 public:
34
35 manager() :
36 retain_count_(1) {}
37
38 const std::string& name() const
39 {
40 return name_;
41 }
42
43 static boost::shared_ptr<T> instance(const std::string& name)
44 {
45 return boost::shared_ptr<T>(retain(name), &release);
46 }
47
48 private:
49
50 typedef stlplus::hash<std::string,T*,hash_function> ptr_lookup;
51
52 static T* retain(const std::string& name)
53 {
54 typename ptr_lookup::iterator it = ptr_lookup_.find(name);
55
56 if (it != ptr_lookup_.end())
57 {
58 ++((*it).second->retain_count_);
59 return (*it).second;
60 }
61 else
62 {
63 T* new_object(new T);
64 if (new_object)
65 {
66 new_object->name_ = name;
67 new_object->init(name);
68 ptr_lookup_.insert(std::make_pair(name,
69 new_object));
70 }
71 return new_object;
72 }
73 }
74
75 static void release(T* obj)
76 {
77 if (--(obj->retain_count_) == 0)
78 {
79 ptr_lookup_.erase(obj->name_);
80 delete obj;
81 }
82 }
83
84
85 static ptr_lookup ptr_lookup_;
86 std::string name_;
87 unsigned retain_count_;
88 };
89
90 template <class T>
91 hash<std::string,T*,hash_function> manager<T>::ptr_lookup_;
92
93
94 } // namespace moof
95
96 #endif // _MOOF_MANAGER_HH_
97
This page took 0.033387 seconds and 5 git commands to generate.