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