Code_TYMPAN  4.2.0
Industrial site acoustic simulation
SelectorManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14 */
15 
16 #ifndef SELECTOR_MANAGER_H
17 #define SELECTOR_MANAGER_H
18 
19 #include "Selector.h"
20 #include <vector>
21 
22 
23 
27 template<typename T>
29 {
30 
31 public:
33  SelectorManager() : deletable(true) { }
36  {
37  deletable = manager.deletable;
38  for (unsigned int i = 0; i < manager.selectors.size(); i++)
39  {
40  selectors.push_back(manager.selectors.at(i)->Copy());
41  }
42 
43  }
45  virtual ~SelectorManager() { }
46 
48  void setDeletable(bool _isDeletable) { deletable = _isDeletable; }
50  bool isDeletable() { return deletable; }
52  void addSelector(Selector<T> *selector) { selectors.push_back(selector); }
54  std::vector<Selector<T>*>& getSelectors() const { return selectors; }
56  void reset()
57  {
58  for (unsigned int i = 0; i < selectors.size(); i++)
59  {
60  selectors.at(i)->reset();
61  }
62  selectors.clear();
63 
64  if ( !isDeletable() )
65  {
66  selectedData.clear();
67  }
68  else
69  {
70  // std::map<unsigned long long, T*>::iterator it = selectedData.begin();
71  //while ( it != selectedData.end() )
72  // {
73  // T* data = (*it).second;
74  // delete data;
75  //
76  // it = selectedData.erase(it);
77  // }
78  selectedData.clear();
79  }
80  }
82  bool appendData(T* data)
83  {
84  vector<unsigned long long> dataToReplace;
85  unsigned long long oldData;
86  for (unsigned int i = 0; i < selectors.size(); i++)
87  {
88  switch (selectors.at(i)->canBeInserted(data, oldData))
89  {
90  case SELECTOR_REJECT:
91  if (deletable)
92  {
93  delete data;
94  data = NULL;
95  }
96  else
97  {
98  rejectedData.insert(pair<unsigned long long, T*>(data->getConstructId(), data));
99  }
100  return false;
101  break;
102  case SELECTOR_REPLACE:
103  dataToReplace.push_back(oldData);
104  break;
105  case SELECTOR_ACCEPT:
106  break;
107  }
108  }
109 
110  //cout << "Tous les selecteurs (" << selectors.size() << ") ont ete passe avec succes." << endl;
111  //Tous les filtres sont passes, le rayon est valide
112  //On commence par deplacer/supprimer les rayons a remplacer
113  for (unsigned int i = 0; i < dataToReplace.size(); i++)
114  {
115  typename std::map<unsigned long long, T*>::iterator it = selectedData.find(dataToReplace.at(i));
116  it = selectedData.find(dataToReplace.at(i));
117  if (it != selectedData.end())
118  {
119  T* previousData = it->second;
120  selectedData.erase(it);
121  //cout << "Le rayon " << previousData->constructId << " est supprime ou deplace." << endl;
122  if (deletable)
123  {
124  delete previousData;
125  }
126  else
127  {
128  rejectedData.insert(pair<unsigned long long, T*>(previousData->getConstructId(), previousData));
129  }
130  }
131  }
132 
133  //Dans chaque filtre, on rajoute le rayon necessaire si un filtre a besoin des rayons precedemments acceptes.
134  for (unsigned int i = 0; i < selectors.size(); i++)
135  {
136  selectors.at(i)->insert(data);
137  }
138 
139  //Enfin, on rajoute le rayon dans la liste des rayons valides par le filtre
140  //std::cout<<"Insertion de l'element "<<data->constructId<<std::endl;
141  selectedData.insert(pair<unsigned long long, T*>(data->getConstructId(), data));
142  //cout << "Insertion du rayon dans chaque solver passe avec succes." << endl;
143  return true;
144  }
146  std::map<unsigned long long, T*>& getSelectedData() { return selectedData; }
147 
148 protected:
149  bool deletable;
150  std::vector<Selector<T>*> selectors;
151 
152  std::map<unsigned long long, T*> selectedData;
153  std::map<unsigned long long, T*> rejectedData;
154 
155 };
156 
157 #endif
bool deletable
Flag to know if a data may be deleted if rejected (by default, yes)
std::map< unsigned long long, T * > selectedData
Contains accepted data (rays)
std::vector< Selector< T > * > & getSelectors() const
Return the Selector&#39;s list.
void addSelector(Selector< T > *selector)
Add a Selector to the list.
bool appendData(T *data)
Append data (typically a ray) and loop on Selectors to filter.
std::map< unsigned long long, T * > & getSelectedData()
Get the selected data.
void setDeletable(bool _isDeletable)
Set deletable flag.
Base class for Selector (used to keep or disable rays according different criterias) ...
Definition: Selector.h:77
bool isDeletable()
Return true if this may be deleted.
void reset()
Reset all the Selector and clear the local data.
Selector manager.
SelectorManager()
Constructor.
SelectorManager(const SelectorManager< T > &manager)
Copy constructor.
virtual ~SelectorManager()
Destructor.
std::vector< Selector< T > * > selectors
Pointers list of Selector.
std::map< unsigned long long, T * > rejectedData
Contains rejected data (rays) if deletable set to false.