Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Utils.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <map>
35 
36 #include <ogdf/basic/basic.h>
37 
38 namespace ogdf {
39 
40 // Provides a nicer syntax for reading formatted input through streams, e.g.
41 // `stream >> a >> ';' >> y`.
42 class TokenIgnorer {
43 private:
44  char m_c;
45 
46 public:
47  explicit TokenIgnorer(const char c): m_c(c) {};
48 
49  friend std::istream &operator >>(std::istream &is, TokenIgnorer c);
50 };
51 
52 
53 std::istream &operator >>(std::istream &is, TokenIgnorer token);
54 
55 template <typename E>
56 static inline E toEnum(
57  const std::string &str, // A string we want to convert.
58  std::map<std::string, E> &map, // A map to be lazily evaluated.
59  std::string toString(const E&),
60  const E first, const E last, const E def) // Enum informations.
61 {
62  if(map.empty()) {
63 #if 0
64  map = new Hashing<std::string, E>();
65 #endif
66 
67  // Iterating over enums is potentially unsafe... (fixable in C++11).
68  for(int it = static_cast<int>(last); it >= static_cast<int>(first); it--) {
69  const E e = static_cast<E>(it);
70  map[toString(e)] = e;
71  }
72  }
73 
74  return map.find(str) == map.end() ? def : map[str];
75 }
76 
77 
78 } // end namespace ogdf
TokenIgnorer(const char c)
Definition: Utils.h:47
static E toEnum(const std::string &str, std::map< std::string, E > &map, std::string toString(const E &), const E first, const E last, const E def)
Definition: Utils.h:56
std::string toString(const Attribute &attr)
friend std::istream & operator>>(std::istream &is, TokenIgnorer c)
Basic declarations, included by all source files.
Hashing with chaining and table doubling.
Definition: Hashing.h:264
The namespace for all OGDF objects.