Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRouter.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JROUTER__
2 #define __JTOOLS__JROUTER__
3 
4 #include <deque>
5 
7 #include "JLang/JClass.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JTOOLS {}
15 namespace JPP { using namespace JTOOLS; }
16 
17 namespace JTOOLS {
18 
19  using JLANG::JComparisonAvailable;
20 
21 
22  /**
23  * Direct addressing of elements with unique identifiers.
24  */
25  template<class JAddress_t, bool has_eq = JComparisonAvailable<JAddress_t>::has_eq>
26  class JRouter;
27 
28 
29  /**
30  * Template specialisation of JRouter without default address comparison.
31  */
32  template<class JAddress_t>
33  class JRouter<JAddress_t, false> :
34  protected std::deque<JAddress_t>
35  {
36 
38 
39  public:
40  /**
41  * Default constructor.
42  */
43  JRouter() :
44  std::deque<JAddress_t>()
45  {}
46 
47 
48  /**
49  * Virtual destructor.
50  */
51  virtual ~JRouter()
52  {}
53 
54 
55  /**
56  * Get default address.
57  *
58  * \return default address
59  */
60  virtual const JAddress_t& getDefaultAddress() const
61  {
62  static JAddress_t address;
63 
64  return address;
65  }
66 
67 
68  /**
69  * Clear.
70  */
71  void clear()
72  {
73  static_cast<std::deque<JAddress_t>&>(*this).clear();
74 
75  this->first = 0;
76  }
77 
78 
79  /**
80  * Copy router.
81  *
82  * \param router router
83  * \param option copy addresses
84  */
85  void copy(const JRouter& router, const bool option = true)
86  {
87  if (option) {
88 
89  *this = router;
90 
91  } else {
92 
93  this->resize(router.size());
94  this->first = router.first;
95  }
96  }
97 
98 
99  /**
100  * Store address for given identifier.
101  *
102  * \param id identifier
103  * \param address address
104  */
105  void put(const int id, argument_type address)
106  {
107  if (this->empty()) {
108 
109  this->push_back(address);
110 
111  this->first = id;
112 
113  } else if (id < this->first) {
114 
115  this->insert(this->begin(), this->first - id, getDefaultAddress());
116 
117  this->first = id;
118 
119  this->at(0) = address;
120 
121  } else {
122 
123  const size_t index = (size_t) (id - this->first);
124 
125  if (index >= this->size()) {
126  this->resize(index + 1, getDefaultAddress());
127  }
128 
129  this->at(index) = address;
130  }
131  }
132 
133 
134  /**
135  * Check whether given identifier is in range of this router.
136  *
137  * \param id identifier
138  * \return true if in range; else false
139  */
140  bool in_range(const int id) const
141  {
142  return (id >= this->first && id < this->first + (int) this->size());
143  }
144 
145 
146  /**
147  * Get address of given identifier.
148  *
149  * \param id identifier
150  * \return address
151  */
152  const JAddress_t& get(const int id) const
153  {
154  return this->at(id - this->first);
155  }
156 
157  protected:
158  int first;
159  };
160 
161 
162  /**
163  * Template specialisation of JRouter with default address comparison.
164  */
165  template<class JAddress_t>
166  class JRouter<JAddress_t, true> :
167  public JRouter<JAddress_t, false>
168  {
169 
171 
172  protected:
173  /**
174  * Simple data structure for validation of address.
175  */
176  class JAddress
177  {
178  public:
179  /**
180  * Default constructor.
181  */
183  __is_valid(false),
184  __address ()
185  {}
186 
187 
188  /**
189  * Constructor.
190  *
191  * \param address default address
192  */
194  __is_valid(true),
195  __address (address)
196  {}
197 
198 
199  /**
200  * Check validity.
201  *
202  * \return true if valid; else false
203  */
204  bool is_valid() const
205  {
206  return __is_valid;
207  }
208 
209 
210  /**
211  * Get address.
212  *
213  * \return address
214  */
215  const JAddress_t& getAddress() const
216  {
217  return __address;
218  }
219 
220 
221  /**
222  * Compare to given address.
223  *
224  * \param address address
225  * \return true if valid and equal; else false
226  */
227  bool equals(argument_type address) const
228  {
229  return __is_valid && address == __address;
230  }
231 
232 
233  private:
235  JAddress_t __address;
236  };
237 
238 
239  public:
240  /**
241  * Default constructor.
242  */
244  JRouter<JAddress_t, false>(),
245  defaultAddress()
246  {}
247 
248 
249  /**
250  * Constructor.
251  *
252  * \param address default address
253  */
255  JRouter<JAddress_t, false>(),
256  defaultAddress(address)
257  {}
258 
259 
260  /**
261  * Check availability of default address.
262  *
263  * \return true if available; else false
264  */
265  bool hasDefaultAddress() const
266  {
267  return defaultAddress.is_valid();
268  }
269 
270 
271  /**
272  * Get default address.
273  *
274  * \return default address
275  */
276  virtual const JAddress_t& getDefaultAddress() const
277  {
278  return defaultAddress.getAddress();
279  }
280 
281 
282  /**
283  * Set default address.
284  *
285  * \param address default address
286  */
288  {
289  defaultAddress = JAddress(address);
290  }
291 
292 
293  /**
294  * Test whether given identifier has valid address (i.e identifier is in range and corresponding address is not equal to default address).
295  *
296  * \param id identifier
297  * \return true if identifier in range and address not equal to default; else false
298  */
299  bool has(const int id) const
300  {
301  return (this->in_range(id) && !this->defaultAddress.equals(this->get(id)));
302  }
303 
304 
305  private:
306  JAddress defaultAddress;
307  };
308 }
309 
310 #endif
const JAddress_t & getAddress() const
Get address.
Definition: JRouter.hh:215
bool in_range(const int id) const
Check whether given identifier is in range of this router.
Definition: JRouter.hh:140
void copy(const JRouter &router, const bool option=true)
Copy router.
Definition: JRouter.hh:85
Direct addressing of elements with unique identifiers.
Definition: JRouter.hh:26
JRouter()
Default constructor.
Definition: JRouter.hh:243
bool hasDefaultAddress() const
Check availability of default address.
Definition: JRouter.hh:265
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:170
JRouter()
Default constructor.
Definition: JRouter.hh:43
JRouter(argument_type address)
Constructor.
Definition: JRouter.hh:254
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
void setDefaultAddress(argument_type address)
Set default address.
Definition: JRouter.hh:287
JAddress(argument_type address)
Constructor.
Definition: JRouter.hh:193
virtual ~JRouter()
Virtual destructor.
Definition: JRouter.hh:51
bool has(const int id) const
Test whether given identifier has valid address (i.e identifier is in range and corresponding address...
Definition: JRouter.hh:299
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:37
bool is_valid() const
Check validity.
Definition: JRouter.hh:204
void put(const int id, argument_type address)
Store address for given identifier.
Definition: JRouter.hh:105
bool equals(argument_type address) const
Compare to given address.
Definition: JRouter.hh:227
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:276
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:60