~ubuntu-branches/ubuntu/wily/ginkgocadx/wily-proposed

« back to all changes in this revision

Viewing changes to src/cadxcore/api/icontrato.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-05-02 08:09:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110502080926-bql5wep49c7hg91t
Tags: upstream-2.4.1.1
Import upstream version 2.4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  
 
3
 *  $Id: icontrato.h 3700 2011-04-15 07:34:44Z tovar $
 
4
 *  Ginkgo CADx Project
 
5
 *
 
6
 *  Copyright 2008-10 MetaEmotion S.L. All rights reserved.
 
7
 *  http://ginkgo-cadx.com
 
8
 *
 
9
 *  This file is licensed under LGPL v3 license.
 
10
 *  See License.txt for details
 
11
 *
 
12
 */
 
13
#pragma once
 
14
#include "ivista.h"
 
15
#include <map>
 
16
#include <list>
 
17
 
 
18
namespace GNC {
 
19
        namespace GCS {
 
20
 
 
21
//region "Helpers"
 
22
                // <editor-fold defaultstate="collapsed" desc="lista prioritaria">
 
23
                template<class T>
 
24
                class priority_list {
 
25
                        std::list<T> lst;
 
26
                public:
 
27
                        class iterator;
 
28
 
 
29
                        friend class iterator;
 
30
 
 
31
                        class iterator : public std::iterator< std::bidirectional_iterator_tag,T,ptrdiff_t> {
 
32
                                typename std::list<T>* r;
 
33
                                typename std::list<T>::iterator it;
 
34
                        public:
 
35
                                iterator(priority_list<T>& otro) { *this = otro; }
 
36
                                iterator(std::list<T>& lst, const typename std::list<T>::iterator& i) : r(&lst), it(i) {}
 
37
 
 
38
                                bool operator==(const iterator& x) const {
 
39
                                        return it == x.it;
 
40
                                }
 
41
                                bool operator!=(const iterator& x) const {
 
42
                                        return !(*this == x);
 
43
                                }
 
44
                                typename std::list<T>::reference operator*() const {
 
45
                                        return *it;
 
46
                                }
 
47
                                iterator& operator=(const iterator& otro) {
 
48
                                        r = otro.r;
 
49
                                        it = otro.it;
 
50
                                }
 
51
                                iterator& operator++() {
 
52
                                        ++it;
 
53
                                        return *this;
 
54
                                }
 
55
                                iterator operator++(int) {
 
56
                                        iterator tmp = *this;
 
57
                                        ++*this;
 
58
                                        return tmp;
 
59
                                }
 
60
                                iterator& operator--() {
 
61
                                        --it;
 
62
                                        return *this;
 
63
                                }
 
64
                                iterator operator--(int) {
 
65
                                        iterator tmp = *this;
 
66
                                        --*this;
 
67
                                        return tmp;
 
68
                                }
 
69
                                iterator insert(const T& x){
 
70
                                        return iterator(*r, r->insert(it));
 
71
                                }
 
72
                                iterator erase() {
 
73
                                        return iterator(*r, r->erase(it));
 
74
                                }
 
75
                        };
 
76
                        void insertar(const T& x) {
 
77
                                bool stop = false;
 
78
                                for (typename std::list<T>::iterator it1 = lst.begin(); !stop && it1 != lst.end(); it1++) {
 
79
                                        if ((*it1) > x) {
 
80
                                                lst.insert(it1, x);
 
81
                                                stop = true;
 
82
                                        }
 
83
                                }
 
84
                                if (!stop) {
 
85
                                        lst.push_back(x);
 
86
                                }
 
87
                        }
 
88
                        void eliminar(const T& x) {
 
89
                                bool stop = false;
 
90
                                for (typename std::list<T>::iterator it1 = lst.begin(); !stop && it1 != lst.end(); it1++) {
 
91
                                        if ((*it1) == x) {
 
92
                                                lst.erase(it1);
 
93
                                                stop = true;
 
94
                                        }
 
95
                                }
 
96
                        }
 
97
                        iterator begin() {
 
98
                                return iterator(lst, lst.begin());
 
99
                        }
 
100
                        iterator end() {
 
101
                                return iterator(lst, lst.end());
 
102
                        }
 
103
                        int size() { return lst.size(); }
 
104
                };
 
105
                // </editor-fold>
 
106
//endregion
 
107
 
 
108
                class EXTAPI ContratoException : public std::exception {
 
109
                public:
 
110
 
 
111
                        ContratoException(const char* msg)
 
112
                        {
 
113
                                m_Mensaje.assign(msg);
 
114
                        }
 
115
 
 
116
                        ContratoException(std::string& mensaje)
 
117
                        {
 
118
                                m_Mensaje = mensaje;
 
119
                        }
 
120
 
 
121
                        ContratoException(std::string mensaje)
 
122
                        {
 
123
                                m_Mensaje = mensaje;
 
124
                        }
 
125
 
 
126
                        ~ContratoException() throw ()
 
127
                        {
 
128
                        }
 
129
 
 
130
                        std::string& getCause()
 
131
                        {
 
132
                                return m_Mensaje;
 
133
                        }
 
134
 
 
135
                protected:
 
136
                        std::string m_Mensaje;
 
137
                };
 
138
 
 
139
                //------------------------------------------------------------------------
 
140
                /* Interfaz generica de contratos.
 
141
                 * Cuidado con la herencia multiple aqui
 
142
                 */
 
143
                class EXTAPI IContrato {
 
144
                public:
 
145
 
 
146
                        typedef enum {
 
147
                                TC_Obligatorio,
 
148
                                TC_Opcional
 
149
                        } TipoContrato;
 
150
 
 
151
                        IContrato(TipoContrato tipo = TC_Obligatorio, int prioridad = 0)
 
152
                        {
 
153
                                m_Tipo = tipo;
 
154
                                m_Prioridad = prioridad;
 
155
                        };
 
156
 
 
157
                        ~IContrato()
 
158
                        {
 
159
                                ;
 
160
                        };
 
161
 
 
162
                        void Ping()
 
163
                        {
 
164
                        };
 
165
 
 
166
                        bool operator<(const IContrato& otro) const {
 
167
                                if (m_Prioridad < otro.m_Prioridad) {
 
168
                                        return true;
 
169
                                }
 
170
                                return false;
 
171
                        };
 
172
                        bool operator<(const IContrato* otro) const {
 
173
                                if (m_Prioridad < otro->m_Prioridad) {
 
174
                                        return true;
 
175
                                }
 
176
                                return false;
 
177
                        };
 
178
 
 
179
 
 
180
                public:
 
181
                        TipoContrato m_Tipo;
 
182
                        int          m_Prioridad;
 
183
                };
 
184
 
 
185
                //------------------------------------------------------------------------
 
186
                /* Clase de gestion de contratos */
 
187
                template <class TContrato>
 
188
                class IContratable {
 
189
 
 
190
//region "Interfaz pública de gestion de contratos"
 
191
 
 
192
                public:
 
193
                        typedef TContrato TipoContrato;
 
194
                        typedef priority_list<TContrato*> ListaContratos;
 
195
                        typedef typename ListaContratos::iterator IteradorListaContratos;
 
196
 
 
197
                public:
 
198
 
 
199
                        /* Registra un contrato asociado a la lista. Los objetos apuntados por pContrato no son nunca liberados por esta clase */
 
200
                        void Subscribir(GNC::GCS::IVista* pVista, TContrato* pContrato)
 
201
                        {
 
202
                                /* solo para mostrar errores legibles en tiempo de compilación */
 
203
                                GTRACE("IContrato::Subscribir( Herramienta = " << this << ", Vista = " << pVista << ", Contrato = " << pContrato << ")");
 
204
                                GNC::GCS::IContrato* c = static_cast<GNC::GCS::IContrato*> (pContrato);
 
205
                                if (c != NULL) {
 
206
                                        c->Ping();
 
207
                                        IteradorMapaContratos it = m_Contratos.find(pVista);
 
208
                                        if (it == m_Contratos.end()) { // Insertamos una nueva lista
 
209
                                                ListaContratos* pLista = new ListaContratos();
 
210
                                                pLista->insertar(pContrato);
 
211
                                                m_Contratos[pVista] = pLista;
 
212
 
 
213
                                        }
 
214
                                        else {
 
215
                                                (*it).second->insertar(pContrato);
 
216
                                        }
 
217
                                }
 
218
                        };
 
219
                        /* DesRegistra un contrato asociado a la lista. El objeto apuntado por pContrato no es liberado nunca por esta clase. */
 
220
                        void DesSubscribir(GNC::GCS::IVista* pVista, TContrato* pContrato)
 
221
                        {
 
222
                                GTRACE("IContrato::DesSubscribir( Herramienta = " << this << ", Vista = " << pVista << ", Contrato = " << pContrato << ")");
 
223
                                IteradorMapaContratos it = m_Contratos.find(pVista);
 
224
                                if (it != m_Contratos.end()) {
 
225
                                        ListaContratos* pLista = (*it).second;
 
226
                                        if (pLista != NULL) {
 
227
                                                pLista->eliminar(pContrato);
 
228
                                        }
 
229
                                }
 
230
                                if (pVista == m_pVistaActiva) {
 
231
                                        m_pVistaActiva = NULL;
 
232
                                        m_pListaActiva = NULL;
 
233
                                }
 
234
 
 
235
                        };
 
236
 
 
237
                        void DesSubscribirsLosDeLaVista(GNC::GCS::IVista* pVista)
 
238
                        {
 
239
                                GTRACE("IContratable::DesSubscribirLosDeLaVista ( this = " << this << ", Vista = " << pVista << ")");
 
240
                                if (pVista != NULL) {
 
241
                                        IteradorMapaContratos it = m_Contratos.find(pVista);
 
242
                                        if (it != m_Contratos.end()) {
 
243
                                                delete (*it).second;
 
244
                                                m_Contratos.erase(it);
 
245
                                        }
 
246
                                        if (pVista == m_pVistaActiva) {
 
247
                                                m_pVistaActiva = NULL;
 
248
                                                m_pListaActiva = NULL;
 
249
                                        }
 
250
                                }
 
251
                        };
 
252
 
 
253
                        bool ValidaContrato(GNC::GCS::IVista* pVista) {
 
254
                                ListaContratos* lista = NULL;
 
255
                                if (pVista != NULL) {
 
256
                                        lista = ObtenerContratos(pVista);
 
257
                                }
 
258
                                return (lista != NULL && lista->size() > 0);
 
259
                        }
 
260
 
 
261
                        bool ValidaContratoLaVistaActiva() {
 
262
                                return m_pListaActiva != NULL && m_pListaActiva->size() > 0;
 
263
                        }
 
264
//endregion
 
265
 
 
266
 
 
267
//region "Interfaz privada de gestion de contratos"
 
268
                protected:
 
269
                        typedef std::map<IVista*, ListaContratos* > MapaContratos;
 
270
                        typedef typename MapaContratos::iterator IteradorMapaContratos;
 
271
 
 
272
                        void EstablecerVistaActiva(GNC::GCS::IVista* pVista)
 
273
                        {
 
274
                                m_pVistaActiva = pVista;
 
275
                                if (pVista != NULL) {
 
276
                                        IteradorMapaContratos it = m_Contratos.find(pVista);
 
277
                                        if (it != m_Contratos.end()) {
 
278
                                                m_pListaActiva = (*it).second;
 
279
                                        }
 
280
                                        else {
 
281
                                                m_pListaActiva = NULL;
 
282
                                        }
 
283
                                } else {
 
284
                                        m_pListaActiva = NULL;
 
285
                                }
 
286
                        };
 
287
 
 
288
                        ListaContratos* ObtenerContratos(GNC::GCS::IVista* pVista)
 
289
                        {
 
290
                                ListaContratos* lista = NULL;
 
291
                                if (pVista != NULL) {
 
292
                                        IteradorMapaContratos it = m_Contratos.find(pVista);
 
293
                                        if (it != m_Contratos.end()) {
 
294
                                                lista = (*it).second;
 
295
                                        }
 
296
                                }
 
297
                                return lista;
 
298
                        };
 
299
 
 
300
                        ListaContratos* ObtenerContratosDeActiva()
 
301
                        {
 
302
                                return m_pListaActiva;
 
303
                        };
 
304
 
 
305
                        void PurgarTodo()
 
306
                        {
 
307
                                for (IteradorMapaContratos it = m_Contratos.begin(); it != m_Contratos.end(); it++) {
 
308
                                        delete (*it).second;
 
309
                                        m_Contratos.erase(it);
 
310
                                }
 
311
                        }
 
312
 
 
313
                        IContratable() {
 
314
                                m_pListaActiva = NULL;
 
315
                                m_pVistaActiva = NULL;
 
316
                        }
 
317
 
 
318
                        ~IContratable() {
 
319
                                m_Contratos.clear();
 
320
                                m_pListaActiva = NULL;
 
321
                        }
 
322
 
 
323
//endregion
 
324
 
 
325
//region "Atributos"
 
326
 
 
327
                protected:
 
328
                        MapaContratos m_Contratos;
 
329
                        GNC::GCS::IVista* m_pVistaActiva;
 
330
                        ListaContratos* m_pListaActiva;
 
331
 
 
332
//endregion
 
333
                };
 
334
                #if defined(_WINDOWS)
 
335
                EXTAPI_TEMPLATE template IContratable<IContrato>;
 
336
                #endif
 
337
        };
 
338
 
 
339
};