Jpp
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 
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  * Store address for given identifier.
70  *
71  * \param id identifier
72  * \param address address
73  */
74  void put(const int id, argument_type address)
75  {
76  if (this->empty()) {
77 
78  this->push_back(address);
79 
80  this->first = id;
81 
82  } else {
83 
84  for ( ; id < this->first; --(this->first)) {
85  this->push_front(getDefaultAddress());
86  }
87 
88  const unsigned int index = (unsigned int) (id - this->first);
89 
90  while (index >= this->size()) {
91  this->push_back(getDefaultAddress());
92  }
93 
94  this->at(index) = address;
95  }
96  }
97 
98 
99  /**
100  * Check whether given identifier is in range of this router.
101  *
102  * \param id identifier
103  * \return true if in range; else false
104  */
105  bool in_range(const int id) const
106  {
107  return (id >= this->first && id < this->first + (int) this->size());
108  }
109 
110 
111  /**
112  * Get address of given identifier.
113  *
114  * \param id identifier
115  * \return address
116  */
117  const JAddress_t& get(const int id) const
118  {
119  return this->at(id - this->first);
120  }
121 
122  protected:
123  int first;
124  };
125 
126 
127  /**
128  * Template specialisation of JRouter with default address comparison.
129  */
130  template<class JAddress_t>
131  class JRouter<JAddress_t, true> :
132  public JRouter<JAddress_t, false>
133  {
134 
136 
137  protected:
138  /**
139  * Simple data structure for validation of address.
140  */
141  class JAddress
142  {
143  public:
144  /**
145  * Default constructor.
146  */
148  __is_valid(false),
149  __address ()
150  {}
151 
152 
153  /**
154  * Constructor.
155  *
156  * \param address default address
157  */
159  __is_valid(true),
160  __address (address)
161  {}
162 
163 
164  /**
165  * Check validity.
166  *
167  * \return true if valid; else false
168  */
169  bool is_valid() const
170  {
171  return __is_valid;
172  }
173 
174 
175  /**
176  * Get address.
177  *
178  * \return address
179  */
180  const JAddress_t& getAddress() const
181  {
182  return __address;
183  }
184 
185 
186  /**
187  * Compare to given address.
188  *
189  * \param address address
190  * \return true if valid and equal; else false
191  */
192  bool equals(argument_type address) const
193  {
194  return __is_valid && address == __address;
195  }
196 
197 
198  private:
200  JAddress_t __address;
201  };
202 
203 
204  public:
205  /**
206  * Default constructor.
207  */
209  JRouter<JAddress_t, false>(),
210  defaultAddress()
211  {}
212 
213 
214  /**
215  * Constructor.
216  *
217  * \param address default address
218  */
220  JRouter<JAddress_t, false>(),
221  defaultAddress(address)
222  {}
223 
224 
225  /**
226  * Check availability of default address.
227  *
228  * \return true if available; else false
229  */
230  bool hasDefaultAddress() const
231  {
232  return defaultAddress.is_valid();
233  }
234 
235 
236  /**
237  * Get default address.
238  *
239  * \return default address
240  */
241  virtual const JAddress_t& getDefaultAddress() const
242  {
243  return defaultAddress.getAddress();
244  }
245 
246 
247  /**
248  * Set default address.
249  *
250  * \param address default address
251  */
253  {
254  defaultAddress = JAddress(address);
255  }
256 
257 
258  /**
259  * Test whether given identifier has valid address (i.e identifier is in range and corresponding address is not equal to default address).
260  *
261  * \param id identifier
262  * \return true if identifier in range and address not equal to default; else false
263  */
264  bool has(const int id) const
265  {
266  return (this->in_range(id) && !this->defaultAddress.equals(this->get(id)));
267  }
268 
269 
270  private:
271  JAddress defaultAddress;
272  };
273 }
274 
275 #endif
JTOOLS::JRouter< JAddress_t, true >::setDefaultAddress
void setDefaultAddress(argument_type address)
Set default address.
Definition: JRouter.hh:252
JLANG::JLOCAL::JComparisonAvailable
Test availability of comparison operators of non-composite data types.
Definition: JComparisonAvailable.hh:37
JTOOLS::JRouter< JAddress_t, true >::has
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:264
JTOOLS::JRouter< JAddress_t, true >::JAddress::is_valid
bool is_valid() const
Check validity.
Definition: JRouter.hh:169
JTOOLS::JRouter< JAddress_t, true >::JAddress::__is_valid
bool __is_valid
Definition: JRouter.hh:199
JTOOLS::JRouter< JAddress_t, false >::put
void put(const int id, argument_type address)
Store address for given identifier.
Definition: JRouter.hh:74
JTOOLS::JRouter< JAddress_t, true >::JRouter
JRouter(argument_type address)
Constructor.
Definition: JRouter.hh:219
JTOOLS::JRouter< JAddress_t, false >::JRouter
JRouter()
Default constructor.
Definition: JRouter.hh:43
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
JTOOLS::JRouter< JAddress_t, false >::~JRouter
virtual ~JRouter()
Virtual destructor.
Definition: JRouter.hh:51
JTOOLS::JRouter< JAddress_t, true >::hasDefaultAddress
bool hasDefaultAddress() const
Check availability of default address.
Definition: JRouter.hh:230
JTOOLS::JRouter< JAddress_t, false >::getDefaultAddress
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:60
JTOOLS::JRouter< JAddress_t, true >::JAddress::__address
JAddress_t __address
Definition: JRouter.hh:200
JTOOLS::JRouter< JAddress_t, true >::JAddress::JAddress
JAddress()
Default constructor.
Definition: JRouter.hh:147
push_front
void push_front(std::vector< T > &vec, T &value)
Put value in front of data.
Definition: io_ascii.hh:384
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JRouter< JAddress_t, true >::getDefaultAddress
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:241
JTOOLS::JRouter< JAddress_t, true >::argument_type
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:135
JTOOLS::JRouter
Direct addressing of elements with unique identifiers.
Definition: JRouter.hh:26
JTOOLS::JRouter< JAddress_t, true >::JRouter
JRouter()
Default constructor.
Definition: JRouter.hh:208
JTOOLS::JRouter< JAddress_t, false >::first
int first
Definition: JRouter.hh:123
JTOOLS::JRouter< JAddress_t, true >::defaultAddress
JAddress defaultAddress
Definition: JRouter.hh:271
JTOOLS::JRouter< JAddress_t, true >::JAddress::equals
bool equals(argument_type address) const
Compare to given address.
Definition: JRouter.hh:192
std
Definition: jaanetDictionary.h:36
JTOOLS::JRouter< JAddress_t, false >::argument_type
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:37
JTOOLS::JRouter< JAddress_t, false >::in_range
bool in_range(const int id) const
Check whether given identifier is in range of this router.
Definition: JRouter.hh:105
JClass.hh
JTOOLS::JRouter< JAddress_t, true >::JAddress::getAddress
const JAddress_t & getAddress() const
Get address.
Definition: JRouter.hh:180
JComparisonAvailable.hh
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JRouter< JAddress_t, true >::JAddress::JAddress
JAddress(argument_type address)
Constructor.
Definition: JRouter.hh:158
JTOOLS::JRouter< JAddress_t, false >::get
const JAddress_t & get(const int id) const
Get address of given identifier.
Definition: JRouter.hh:117