]>
Dogcows Code - chaz/yoink/blob - src/moof/manager.hh
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
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.
8 *****************************************************************************/
10 #ifndef _MOOF_MANAGER_HH_
11 #define _MOOF_MANAGER_HH_
14 #include <boost/shared_ptr.hpp>
16 #include <moof/hash.hh>
21 * A manager is a collection of named objects of the same type. Libraries
22 * use reference counting to automagically delete objects which no longer
23 * have any interested code.
37 const std::string
& name() const
42 static boost::shared_ptr
<T
> instance(const std::string
& name
)
44 return boost::shared_ptr
<T
>(retain(name
), &release
);
49 typedef stlplus::hash
<std::string
,T
*,hash_function
> ptr_lookup
;
51 static T
* retain(const std::string
& name
)
53 typename
ptr_lookup::iterator it
= ptr_lookup_
.find(name
);
55 if (it
!= ptr_lookup_
.end())
57 ++((*it
).second
->retain_count_
);
65 new_object
->name_
= name
;
66 new_object
->init(name
);
67 ptr_lookup_
.insert(std::make_pair(name
,
74 static void release(T
* obj
)
76 if (--(obj
->retain_count_
) == 0)
78 ptr_lookup_
.erase(obj
->name_
);
84 static ptr_lookup ptr_lookup_
;
86 unsigned retain_count_
;
90 hash
<std::string
,T
*,hash_function
> manager
<T
>::ptr_lookup_
;
95 #endif // _MOOF_MANAGER_HH_
This page took 0.035391 seconds and 4 git commands to generate.