source: XMLIO_V2/external/include/Poco/FIFOStrategy.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1//
2// FIFOStrategy.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/FIFOStrategy.h#5 $
5//
6// Library: Foundation
7// Package: Events
8// Module:  FIFOStragegy
9//
10// Implementation of the FIFOStrategy template.
11//
12// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38#ifndef  Foundation_FIFOStrategy_INCLUDED
39#define  Foundation_FIFOStrategy_INCLUDED
40
41
42#include "Poco/NotificationStrategy.h"
43#include <map>
44#include <list>
45#include <memory>
46
47
48namespace Poco {
49
50
51template <class TArgs, class TDelegate, class TCompare> 
52class FIFOStrategy: public NotificationStrategy<TArgs, TDelegate>
53{
54public:
55        typedef std::list<TDelegate*>                    Delegates;
56        typedef typename Delegates::iterator             Iterator;
57        typedef typename Delegates::const_iterator       ConstIterator;
58        typedef std::map<TDelegate*, Iterator, TCompare> DelegateIndex;
59        typedef typename DelegateIndex::iterator         IndexIterator;
60        typedef typename DelegateIndex::const_iterator   ConstIndexIterator;
61
62        FIFOStrategy()
63        {
64        }
65
66        FIFOStrategy(const FIFOStrategy& s)
67        {
68                operator = (s);
69        }
70
71        ~FIFOStrategy()
72        {
73                clear();
74        }
75
76        void notify(const void* sender, TArgs& arguments)
77        {
78                std::vector<Iterator> delMe;
79                Iterator it    = _observers.begin();
80                Iterator itEnd = _observers.end();
81
82                for (; it != itEnd; ++it)
83                {
84                        if (!(*it)->notify(sender, arguments))
85                        {
86                                // schedule for deletion
87                                delMe.push_back(it);
88                        }
89                }
90               
91                while (!delMe.empty())
92                {
93                        typename std::vector<Iterator>::iterator vit = delMe.end();
94                        --vit;
95                        delete **vit;
96                        _observers.erase(*vit);
97                        delMe.pop_back();
98                }
99        }
100
101        void add(const TDelegate& delegate)
102        {
103                IndexIterator it = _observerIndex.find(const_cast<TDelegate*>(&delegate));
104                if (it != _observerIndex.end())
105                {
106                        delete *it->second;
107                        _observers.erase(it->second);
108                        _observerIndex.erase(it);
109                }
110                std::auto_ptr<TDelegate> pDelegate(delegate.clone());
111                _observers.push_back(pDelegate.get());
112                bool tmp = _observerIndex.insert(std::make_pair(pDelegate.get(), --_observers.end())).second;
113                poco_assert (tmp);
114                pDelegate.release();
115        }
116
117        void remove(const TDelegate& delegate)
118        {
119                IndexIterator it = _observerIndex.find(const_cast<TDelegate*>(&delegate));
120
121                if (it != _observerIndex.end())
122                {
123                        delete *it->second;
124                        _observers.erase(it->second);
125                        _observerIndex.erase(it);
126                }
127        }
128
129        FIFOStrategy& operator = (const FIFOStrategy& s)
130        {
131                if (this != &s)
132                {
133                        for (ConstIterator it = s._observers.begin(); it != s._observers.end(); ++it)
134                        {
135                                add(**it);
136                        }
137                }
138                return *this;
139        }
140
141        void clear()
142        {
143                for (Iterator it = _observers.begin(); it != _observers.end(); ++it)
144                {
145                        delete *it;
146                }
147
148                _observers.clear();
149                _observerIndex.clear();
150        }
151
152        bool empty() const
153        {
154                return _observers.empty();
155        }
156
157protected:
158        Delegates     _observers;     /// Stores the delegates in the order they were added.
159        DelegateIndex _observerIndex; /// For faster lookup when add/remove is used.
160};
161
162
163} // namespace Poco
164
165
166#endif
Note: See TracBrowser for help on using the repository browser.