/src/log4cplus/src/objectregistry.cxx
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: objectregistry.cxx |
3 | | // Created: 3/2003 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2003-2017 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 | | |
33 | | ObjectRegistryBase::ObjectRegistryBase() |
34 | 272 | : locking (true) |
35 | 272 | { } |
36 | | |
37 | | |
38 | | ObjectRegistryBase::~ObjectRegistryBase() |
39 | 0 | { } |
40 | | |
41 | | |
42 | | |
43 | | /////////////////////////////////////////////////////////////////////////////// |
44 | | // ObjectRegistryBase public methods |
45 | | /////////////////////////////////////////////////////////////////////////////// |
46 | | |
47 | | bool |
48 | | ObjectRegistryBase::exists(const tstring& name) const |
49 | 0 | { |
50 | 0 | thread::MutexGuard guard (mutex); |
51 | |
|
52 | 0 | return data.find(name) != data.end(); |
53 | 0 | } |
54 | | |
55 | | |
56 | | std::vector<tstring> |
57 | | ObjectRegistryBase::getAllNames() const |
58 | 0 | { |
59 | 0 | std::vector<tstring> tmp; |
60 | |
|
61 | 0 | { |
62 | 0 | thread::MutexGuard guard (mutex); |
63 | 0 | tmp.reserve (data.size ()); |
64 | 0 | for (auto const & kv : data) |
65 | 0 | tmp.emplace_back(kv.first); |
66 | 0 | } |
67 | |
|
68 | 0 | return tmp; |
69 | 0 | } |
70 | | |
71 | | |
72 | | |
73 | | /////////////////////////////////////////////////////////////////////////////// |
74 | | // ObjectRegistryBase protected methods |
75 | | /////////////////////////////////////////////////////////////////////////////// |
76 | | |
77 | | bool |
78 | | ObjectRegistryBase::putVal(const tstring& name, void* object) |
79 | 1.56k | { |
80 | 1.56k | ObjectMap::value_type value(name, object); |
81 | 1.56k | std::pair<ObjectMap::iterator, bool> ret; |
82 | | |
83 | 1.56k | { |
84 | 1.56k | thread::MutexGuard guard; |
85 | 1.56k | if (locking) |
86 | 0 | guard.attach_and_lock (mutex); |
87 | | |
88 | 1.56k | ret = data.insert(std::move (value)); |
89 | 1.56k | } |
90 | | |
91 | 1.56k | if (! ret.second) |
92 | 0 | deleteObject( value.second ); |
93 | | |
94 | 1.56k | return ret.second; |
95 | 1.56k | } |
96 | | |
97 | | |
98 | | void* |
99 | | ObjectRegistryBase::getVal(const tstring& name) const |
100 | 234k | { |
101 | 234k | thread::MutexGuard guard (mutex); |
102 | | |
103 | 234k | ObjectMap::const_iterator it (data.find (name)); |
104 | 234k | if (it != data.end ()) |
105 | 234k | return it->second; |
106 | 0 | else |
107 | 0 | return nullptr; |
108 | 234k | } |
109 | | |
110 | | |
111 | | |
112 | | |
113 | | void |
114 | | ObjectRegistryBase::clear() |
115 | 0 | { |
116 | 0 | thread::MutexGuard guard (mutex); |
117 | |
|
118 | 0 | for (auto const & kv : data) |
119 | 0 | deleteObject (kv.second); |
120 | 0 | } |
121 | | |
122 | | |
123 | | void |
124 | | ObjectRegistryBase::_enableLocking (bool enable) |
125 | 544 | { |
126 | 544 | locking = enable; |
127 | 544 | } |
128 | | |
129 | | |
130 | | } } // namespace log4cplus { namespace spi { |