1 // Module:  Log4CPLUS
2 // File:    objectregistry.cxx
3 // Created: 3/2003
4 // Author:  Tad E. Smith
5 //
6 //
7 // Copyright 2003-2013 Tad E. Smith
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 //     http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include <log4cplus/spi/objectregistry.h>
22 #include <log4cplus/thread/syncprims-pub-impl.h>
23 #include <log4cplus/thread/threads.h>
24 
25 
26 namespace log4cplus { namespace spi {
27 
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // ObjectRegistryBase ctor and dtor
31 ///////////////////////////////////////////////////////////////////////////////
32 
ObjectRegistryBase()33 ObjectRegistryBase::ObjectRegistryBase()
34 { }
35 
36 
~ObjectRegistryBase()37 ObjectRegistryBase::~ObjectRegistryBase()
38 { }
39 
40 
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // ObjectRegistryBase public methods
44 ///////////////////////////////////////////////////////////////////////////////
45 
46 bool
exists(const tstring & name) const47 ObjectRegistryBase::exists(const tstring& name) const
48 {
49     thread::MutexGuard guard (mutex);
50 
51     return data.find(name) != data.end();
52 }
53 
54 
55 std::vector<tstring>
getAllNames() const56 ObjectRegistryBase::getAllNames() const
57 {
58     std::vector<tstring> tmp;
59 
60     {
61         thread::MutexGuard guard (mutex);
62         for(ObjectMap::const_iterator it=data.begin(); it!=data.end(); ++it)
63             tmp.push_back( (*it).first );
64     }
65 
66     return tmp;
67 }
68 
69 
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 // ObjectRegistryBase protected methods
73 ///////////////////////////////////////////////////////////////////////////////
74 
75 bool
putVal(const tstring & name,void * object)76 ObjectRegistryBase::putVal(const tstring& name, void* object)
77 {
78     ObjectMap::value_type value(name, object);
79     std::pair<ObjectMap::iterator, bool> ret;
80 
81     {
82         thread::MutexGuard guard (mutex);
83         ret = data.insert(value);
84     }
85 
86     if (! ret.second)
87         deleteObject( value.second );
88     return ret.second;
89 }
90 
91 
92 void*
getVal(const tstring & name) const93 ObjectRegistryBase::getVal(const tstring& name) const
94 {
95     thread::MutexGuard guard (mutex);
96 
97     ObjectMap::const_iterator it (data.find (name));
98     if (it != data.end ())
99         return it->second;
100     else
101         return 0;
102 }
103 
104 
105 
106 
107 void
clear()108 ObjectRegistryBase::clear()
109 {
110     thread::MutexGuard guard (mutex);
111 
112     for(ObjectMap::iterator it=data.begin(); it!=data.end(); ++it)
113         deleteObject( it->second );
114 }
115 
116 
117 } } // namespace log4cplus { namespace spi {
118