Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Utils.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 
36 #include <map>
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 std::istream& operator>>(std::istream& is, TokenIgnorer token);
53 
54 template<typename E>
55 static inline E toEnum(const std::string& str, // A string we want to convert.
56  std::string toString(const E&), const E first, const E last, const E def) // Enum informations.
57 {
58  static std::map<std::string, E> map; // A map to be lazily evaluated.
59  if (map.empty()) {
60  // Iterating over enums is potentially unsafe... (fixable in C++11).
61  for (int it = static_cast<int>(last); it >= static_cast<int>(first); it--) {
62  const E e = static_cast<E>(it);
63  map[toString(e)] = e;
64  }
65  }
66 
67  return map.find(str) == map.end() ? def : map[str];
68 }
69 
70 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::TokenIgnorer::TokenIgnorer
TokenIgnorer(const char c)
Definition: Utils.h:47
ogdf::TokenIgnorer::m_c
char m_c
Definition: Utils.h:44
ogdf::toEnum
static E toEnum(const std::string &str, std::string toString(const E &), const E first, const E last, const E def)
Definition: Utils.h:55
ogdf::TokenIgnorer
Definition: Utils.h:42
ogdf::TokenIgnorer::operator>>
friend std::istream & operator>>(std::istream &is, TokenIgnorer c)
ogdf::toString
string toString(FromClass key)
Definition: graphics.h:557
basic.h
Basic declarations, included by all source files.
ogdf::operator>>
std::istream & operator>>(std::istream &is, TokenIgnorer token)