]>
Dogcows Code - chaz/yoink/blob - src/moof/manager.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_MANAGER_HH_
13 #define _MOOF_MANAGER_HH_
17 * A manager is a collection of named objects of the same type. Libraries
18 * use reference counting to automagically delete objects which no longer
19 * have any interested code.
24 #include <boost/shared_ptr.hpp>
26 #include <moof/hash.hh>
40 const std::string
& name() const
45 static boost::shared_ptr
<T
> instance(const std::string
& name
)
47 return boost::shared_ptr
<T
>(retain(name
), &release
);
53 typedef stlplus::hash
<std::string
,T
*,hash_function
> ptr_lookup
;
55 static T
* retain(const std::string
& name
)
57 typename
ptr_lookup::iterator it
= ptr_lookup_
.find(name
);
59 if (it
!= ptr_lookup_
.end())
61 ++((*it
).second
->retain_count_
);
69 new_object
->name_
= name
;
70 new_object
->init(name
);
71 ptr_lookup_
.insert(std::make_pair(name
, new_object
));
77 static void release(T
* obj
)
79 if (--(obj
->retain_count_
) == 0)
81 ptr_lookup_
.erase(obj
->name_
);
87 static ptr_lookup ptr_lookup_
;
89 unsigned retain_count_
;
93 hash
<std::string
,T
*,hash_function
> manager
<T
>::ptr_lookup_
;
98 #endif // _MOOF_MANAGER_HH_
This page took 0.043987 seconds and 4 git commands to generate.