DiaDes  0.1
DIAgnosis of Discrete-Event System
Identifier.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__IDENTIFIER__HH
2 #define __DIADES__UTILS__IDENTIFIER__HH
3 
13 #include<string>
14 #include<vector>
15 #include<unordered_map>
16 #include<map>
17 #include<istream>
18 //#include"Label.hh"
19 #include"Assertion.hh"
20 #include"Exceptions.hh"
21 
22 
23 using std::string;
24 using std::vector;
25 using std::unordered_map;
26 using std::istream;
27 using std::map;
28 
29 namespace Diades
30 {
31  namespace Utils
32  {
33 
34 
35  class IdentifierFactory;
36 
47  istream & getIdentifierString(istream & is, std::string & identifier);
48 
49 
60  class IdentifierData: public std::string
61  {
62  private:
63 
67  unsigned _id;
68 
72  double _rank;
73 
74 
75  public:
76  static string typeName() { return "Utils::IdentifierData"; }
78 
79 
89  IdentifierData(const std::string & idLabel, unsigned id, double rank):std::string(idLabel),_id(id),_rank(rank){}
90 
91 
92 
97  IdentifierData(const IdentifierData & data):std::string(data),_id(data._id),_rank(data._rank){}
98 
99 
100 
101 
102 
109  unsigned id() const { return _id; }
110 
117  double rank() const { return _rank; }
118  };
119 
120 
121 
122 
123  class IdentifierFactory;
124 
125 
134  {
135  private:
140 
141 
142  public:
143  static string typeName() { return "Utils::Identifier"; }
145  typedef string::const_iterator ConstIterator;
146  typedef string::const_reverse_iterator ConstReverseIterator;
147 
148  public:
153  Identifier():_data(0){}
154 
155 
164  Identifier(IdentifierData * data):_data(data){}
165 
166 
179  Identifier(const char * cString);
180 
181 
182 
195  Identifier(const string & str);
196 
202  Identifier(const Identifier & identifier):_data(identifier._data){}
203 
204 
211  bool valid() const
212  {
213  return _data != 0;
214  }
215 
216 
224  Identifier & operator=(const Identifier & identifier)
225  {
226  if(this != &identifier)
227  {
228  _data = identifier._data;
229  }
230  return *this;
231  }
232 
233 
234 
242  unsigned id() const
243  {
244  always_require(Exception,valid(),"id: Invalid identifier");
245  return _data->id();
246  }
247 
248 
256  ConstIterator begin() const
257  {
258  always_require(Exception,valid(),"begin: Invalid identifier");
259  return _data->begin();
260  }
261 
262 
269  double rank() const
270  {
271  always_require(Exception,valid(),"rank: invalid identifier");
272  return _data->rank();
273  }
274 
275 
283  ConstIterator end()
284  {
285  always_require(Exception,valid(),"end: Invalid identifier");
286  return _data->end();
287  }
288 
289 
297  ConstReverseIterator rbegin()
298  {
299  always_require(Exception,valid(),"rbegin: Invalid identifier");
300  return _data->rbegin();
301  }
302 
310  ConstReverseIterator rend()
311  {
312  always_require(Exception,valid(),"rend: Invalid identifier");
313  return _data->rend();
314  }
315 
316 
324  size_t size() const
325  {
326  always_require(Exception,valid(),"size: Invalid identifier");
327  return _data->size();
328  }
329 
330 
338  size_t length() const
339  {
340  return size();
341  }
342 
351  const char & operator[](size_t pos) const
352  {
353  always_require(Exception,valid(),"operator[]: Invalid identifier");
354  return _data->operator[](pos);
355  }
356 
365  Identifier & operator+= ( const Identifier& str );
366 
367 
368 
377  Identifier & operator+= ( const string& str );
378 
379 
380 
381 
382 
391  Identifier & operator+= ( const char* s );
392 
393 
394 
395 
396 
405  Identifier & operator+= ( char c );
406 
407 
408 
409 
410 
411 
412 
421  const string & str() const
422  {
423  always_require(Exception,valid(),"str: Invalid identifier");
424  return (*_data);
425  }
426 
427 
428 
437  const char * c_str() const
438  {
439  always_require(Exception,valid(),"c_str: Invalid identifier");
440  return _data->c_str();
441  }
442 
443 
451  bool operator== (const Identifier & identifier) const
452  {
453  return _data == identifier._data;
454  }
455 
463  bool operator!= (const Identifier & identifier) const
464  {
465  return !operator==(identifier);
466  }
467 
468 
469 
477  bool operator<(const Identifier & identifier) const;
478 
488  friend istream & operator >> (istream & is, Identifier & identifier);
489  friend class IdentifierFactory;
490  };
491 
492 
501  inline std::ostream& operator << (std::ostream & os, const Identifier & identifier)
502  {
503  if(identifier.valid())
504  {
505  return os << identifier.str();
506  }
507  return os << "__DIADES__UTILS__INVALID__IDENTIFIER";
508  }
509 
510 
523  {
524  private:
525  vector<Identifier> _identifiers;
526  map<double,Identifier> _rankings;
527  unordered_map<string,unsigned> _dictionary;
529 
530 
531 
532  public:
533 
541  static void destroy();
542 
543 
544 
545  protected:
546 
551  IdentifierFactory():_identifiers(),_rankings(),_dictionary(){}
552 
553  public:
554  static string typeName() { return "Utils::IdentifierFactory"; }
556 
562 
567  static IdentifierFactory * factory();
568 
577  Identifier getIdentifier(unsigned id);
578 
588  Identifier getIdentifier(const std::string & str);
589 
590  };
591 
592 
593 
594  };
595 };
596 
597 
598 namespace std
599 {
600  template<> struct hash<Diades::Utils::Identifier>
601  {
602  size_t operator()(const Diades::Utils::Identifier & identifier) const
603  {
604  if(identifier.valid())
605  {
606  return (size_t) (identifier.rank() * 100000);
607  }
608  return 0;
609  }
610  };
611 };
612 
613 #endif
const char * c_str() const
Definition: Identifier.hh:437
IdentifierData * _data
Definition: Identifier.hh:139
An Identifier is a reference to a string (IdentifierData) that only contains alpha-numeric characters...
Definition: Identifier.hh:133
#define always_require(Exception, expr, message)
Definition: Assertion.hh:122
size_t operator()(const Diades::Utils::Identifier &identifier) const
Definition: Identifier.hh:602
A IdentifierFactory is a factory in charge of creating IdentifierData.
Definition: Identifier.hh:522
vector< Identifier > _identifiers
Definition: Identifier.hh:525
const char & operator[](size_t pos) const
Definition: Identifier.hh:351
STL namespace.
const string & str() const
Definition: Identifier.hh:421
istream & getIdentifierString(istream &is, std::string &identifier)
Utils::Exception< Identifier > Exception
Definition: Identifier.hh:144
static string typeName()
Definition: Identifier.hh:143
static IdentifierFactory * _instance
Definition: Identifier.hh:528
unordered_map< string, unsigned > _dictionary
Definition: Identifier.hh:527
This is the internal representation of an Identifier.
Definition: Identifier.hh:60
map< double, Identifier > _rankings
Definition: Identifier.hh:526
Namespace of the Diades project.
string::const_reverse_iterator ConstReverseIterator
Definition: Identifier.hh:146
std::ostream & operator<<(std::ostream &os, const Identifier &identifier)
Definition: Identifier.hh:501
Identifier & operator=(const Identifier &identifier)
Definition: Identifier.hh:224
ConstReverseIterator rend()
Definition: Identifier.hh:310
unsigned id() const
Definition: Identifier.hh:242
Utils::Exception< IdentifierFactory > Exception
Definition: Identifier.hh:555
Diades::Utils::Identifier Identifier
Definition: Topology.hh:19
Identifier(IdentifierData *data)
Definition: Identifier.hh:164
Utils::Exception< IdentifierData > Exception
Definition: Identifier.hh:77
ConstIterator begin() const
Definition: Identifier.hh:256
Identifier(const Identifier &identifier)
Definition: Identifier.hh:202
IdentifierData(const IdentifierData &data)
Definition: Identifier.hh:97
IdentifierData(const std::string &idLabel, unsigned id, double rank)
Definition: Identifier.hh:89
ConstReverseIterator rbegin()
Definition: Identifier.hh:297
string::const_iterator ConstIterator
Definition: Identifier.hh:145