~karsten.klagges/openwns-allinone/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * This file is part of openWNS (open Wireless Network Simulator)
 * _____________________________________________________________________________
 *
 * Copyright (C) 2004-2007
 * Chair of Communication Networks (ComNets)
 * Kopernikusstr. 16, D-52074 Aachen, Germany
 * phone: ++49-241-80-27910,
 * fax: ++49-241-80-22242
 * email: info@openwns.org
 * www: http://www.openwns.org
 * _____________________________________________________________________________
 *
 * openWNS is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 2 as published by the
 * Free Software Foundation;
 *
 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

#pragma once
#include <memory>
#include <openwns/container/Registry.hpp>
#include <openwns/Chamaeleon.hpp>

namespace wns { namespace container {

/**
 * @brief Stores references to instances of any type with a KEY
 * @author Marc Schinnenburg <marc@schinnenburg.net>
 */
template <typename KEY,
          typename SORTINGPOLICY = std::less<KEY> >
class WNS_API UntypedRegistry
{
    // ATTENTION: CLEANUPPOLICY here refers to cleanup of
    // ChamaeleonBase type, which must be deleted in any case
    typedef wns::container::Registry<KEY,
                                     ChamaeleonBase*,
                                     wns::container::registry::DeleteOnErase,
                                     SORTINGPOLICY> InstanceContainer;
public:
    typedef KEY KeyType;
    typedef typename InstanceContainer::UnknownKeyValue UnknownKeyValue;
    typedef typename InstanceContainer::DuplicateKeyValue DuplicateKeyValue;
    typedef typename InstanceContainer::KeyList KeyList;
    typedef ChamaeleonBase::BadCast BadCast;

    /**
     * @brief Empty registry
     */
    UntypedRegistry() :
        instances_()
    {
    }

    /**
     * @brief All entries in the registry will be erased
     */
    virtual
        ~UntypedRegistry()
    {
    }

    /**
     * @brief add an instance with a unique name
     */
    template<typename ELEMENT>
        void
        insert(const KEY& key, ELEMENT element)
    {
        // to avoid memory leak in case of exception we use an unique_ptr
        std::unique_ptr<ChamaeleonBase>
            chamaeleonPtr(new Chamaeleon<ELEMENT>(element));
        instances_.insert(key, chamaeleonPtr.get());
        // release the unique_ptr, everything went fine, registry takes
        // control
        chamaeleonPtr.release();
    }

    /**
     * @brief Is an instance with this name known?
     */
    virtual bool
        knows(const KEY& key) const
    {
        return instances_.knows(key);
    }

    /**
     * @brief return an instance of type T according to the name it
     * has been registerd with
     */
    template <typename T>
        T
        find(const KEY& key) const
    {

        ChamaeleonBase* cb = instances_.find(key);
        return cb->downCast<T>()->unHide();
    }

    /**
     * @brief Update the element found by key
     *
     * @pre An element with this key MUST NOT have been registered
     * before
     *
     * @exception UnkownKeyValue Thrown if element to key is not
     * found
     *
     * @todo Marc Schinnenburg: If the need arises we can also offer
     * a method "updateTypeSafe", which alows updating only, if the
     * type to be updated is the same.
     */
    template <typename ELEMENT>
        void
        update(const KeyType& key, ELEMENT element)
    {
        // to avoid memory leak in case of exception we use an auto_ptr
        std::unique_ptr<ChamaeleonBase>
            chamaeleonPtr(new Chamaeleon<ELEMENT>(element));
        instances_.update(key, chamaeleonPtr.get());
        // release the auto_ptr, everything went fine, registry takes
        // control
        chamaeleonPtr.release();
    }

    /**
     * @brief remove the instance registered under the given name,
     */
    virtual void
        erase(const KEY& key)
    {
        instances_.erase(key);
    }


    /**
     * @brief Clears the container
     */
    virtual void
        clear()
    {
        instances_.clear();
    }

    /**
     * @brief True if size of Registry is 0
     */
    virtual bool
        empty() const
        throw()
    {
        return instances_.empty();
    }

    /**
     * @brief Return the size of the registry
     */
    virtual size_t
        size() const
        throw()
    {
        return instances_.size();
    }

    virtual KeyList
        keys() const
        throw()
    {
        return instances_.keys();
    }

private:
    // Disallow Copy Constructor
    UntypedRegistry(const UntypedRegistry&);

    InstanceContainer instances_;
};

}
}