]>
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_
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.
22 #include <boost/shared_ptr.hpp>
24 #include <moof/hash.hh>
38 const std::string
& name() const
43 static boost::shared_ptr
<T
> instance(const std::string
& name
)
45 return boost::shared_ptr
<T
>(retain(name
), &release
);
50 typedef stlplus::hash
<std::string
,T
*,hash_function
> ptr_lookup
;
52 static T
* retain(const std::string
& name
)
54 typename
ptr_lookup::iterator it
= ptr_lookup_
.find(name
);
56 if (it
!= ptr_lookup_
.end())
58 ++((*it
).second
->retain_count_
);
66 new_object
->name_
= name
;
67 new_object
->init(name
);
68 ptr_lookup_
.insert(std::make_pair(name
,
75 static void release(T
* obj
)
77 if (--(obj
->retain_count_
) == 0)
79 ptr_lookup_
.erase(obj
->name_
);
85 static ptr_lookup ptr_lookup_
;
87 unsigned retain_count_
;
91 hash
<std::string
,T
*,hash_function
> manager
<T
>::ptr_lookup_
;
96 #endif // _MOOF_MANAGER_HH_
This page took 0.041196 seconds and 4 git commands to generate.