CHROMA
enum_type_map.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Enum std::map
4  *
5  * Enum std::map
6  */
7 
8 #ifndef enum_type_map_h
9 #define enum_type_map_h
10 
11 #include "chromabase.h"
12 #include <iostream>
13 #include <map>
14 
15 namespace Chroma
16 {
17 
18  //! Main enum std::map holder
19  /*!
20  * \ingroup io
21  *
22  * Enums used throughout the code and their initialization
23  * via std::maps. This is used for IO.
24  */
25  template<typename EnumType>
26  class EnumTypeMap
27  {
28  public:
29  typedef typename std::map<std::string, EnumType> Str2Enum;
30  typedef typename std::map<EnumType, std::string> Enum2Str;
31  typedef typename std::map<std::string, EnumType>::iterator Str2EnumIterator;
32  typedef typename std::map<EnumType, std::string>::iterator Enum2StrIterator;
33  private:
34  Enum2Str enum_map; // Map one way (Enum->String)
35  Str2Enum str_map; // Map other way (String->Enum)
37 
41  };
42 
44  StringLookupException(const EnumType& t) : val_enum(t) {};
45  const EnumType val_enum;
46  };
47 
49  QDPIO::cout << "Allowed enum " << typeIDString << " std::string-value pairs are: " << std::endl;
50  QDPIO::cout << "==================== " << std::endl;
52  for( it=enum_map.begin(); it!=enum_map.end(); it++) {
53  QDPIO::cout << " String: " << it->second << ", Int value of enum: " << it->first << std::endl;
54  }
55  }
56 
57  public:
58 
60 
61  //! Function registers a std::string/enum pair
62  /*!
63  * Enum-String pairs are initialised when the relevant
64  * env's "registered" boolean is set, and this may well be
65  * before QDP is initialised. So it is not safe to
66  * call QDPIO::cerr in this pair registration function....
67  * Printing to std::cerr works tho. So I add it. Message
68  * should only be generated if registration fails which
69  * is most likely to be due to a duplicate key. This is
70  * a useful check that I haven't left duplicates in the
71  * registerAll() functions.
72  */
73  bool registerPair(const std::string& s, const EnumType t) {
74  bool success;
75  success = enum_map.insert(make_pair(t,s)).second;
76  if( ! success ) {
77  std::cerr << "Enum-String Insertion Failure. Enum="<<t<< " String=" << s << std::endl;
78  std::cerr << "Most Likely pair already inserted. Check relevant files for duplicates" << std::endl << std::flush ;
79 
80  exit(1);
81  }
82 
83  success &= str_map.insert(make_pair(s,t)).second;
84  if( ! success ) {
85  std::cerr << "String-Enum Insertion Failure. Enum="<<t<< " String=" << s << std::endl;
86  std::cerr << "Most Likely pair already inserted. Check relevant files for duplicates" << std::endl << std::flush ;
87 
88  exit(1);
89  }
90 
91  return true;
92  }
93 
94 
95  //! Look up an enum based on a std::string
96  EnumType lookUpEnum(const std::string& s) {
97 
98  // Do the lookup
99  Str2EnumIterator it = str_map.find(s);
100  EnumType t;
101  if( it != str_map.end() ) {
102  t= it->second;
103  }
104  else {
106  throw e;
107  }
108  return t;
109  }
110 
111 
112  //! Look up a std::string from an enum
113  std::string lookUpString(const EnumType& t) {
114  // Do the lookup
115  Enum2StrIterator it = enum_map.find(t);
116  std::string s;
117  if( it != enum_map.end() ) {
118  s= it->second;
119  }
120  else {
122  throw e;
123  }
124  return s;
125  }
126 
127 
128  //! "Reader"
130  XMLReader& xml_in,
131  const std::string& path,
132  EnumType& t) {
133  // Try to read a std::string for the enum
134  std::string string_in;
135  try {
136  QDP::read(xml_in, path, string_in);
137  }
138  catch( const std::string& xml_e ) {
139  QDPIO::cerr << "Caught Exception parsing XML: " << xml_e << std::endl;
140  QDP_abort(1);
141  }
142 
143  // Try to look up the enum for the std::string
144  try {
145  t = lookUpEnum(string_in);
146  }
147  catch( EnumLookupException& e ) {
148  QDPIO::cerr << "No enum " << typeIDString << " registered for std::string " << e.val_str << " while parsing XML Query " << path << std::endl;
150  QDP_abort(1);
151  }
152  }
153 
154 
155  //! Writer
157  XMLWriter& xml_out,
158  const std::string& path,
159  const EnumType& t) {
160  std::string string_out;
161  // Try to look up the std::string for the enum
162  try {
163  string_out = lookUpString(t);
164  }
165  catch( StringLookupException& e) {
166  QDPIO::cerr << "No std::string found for enum " << typeIDString << " with value " << t << " while attempting to write XML " << std::endl;
168  QDP_abort(1);
169  }
170 
171  // Try writing the std::string
172  try {
173  QDP::write(xml_out, path, string_out);
174  }
175  catch( const std::string& xml_e ) {
176  QDPIO::cerr << "Caught Exception writing XML: " << xml_e << std::endl;
177  QDP_abort(1);
178  }
179  }
180 
181  //! "Reader"
183  BinaryReader& bin_in,
184  EnumType& t) {
185  // Try to read a std::string for the enum
186  std::string string_in;
187  try {
188  QDP::readDesc(bin_in, string_in);
189  }
190  catch( const std::string& bin_e ) {
191  QDPIO::cerr << "Caught Exception parsing BIN: " << bin_e << std::endl;
192  QDP_abort(1);
193  }
194 
195  // Try to look up the enum for the std::string
196  try {
197  t = lookUpEnum(string_in);
198  }
199  catch( EnumLookupException& e ) {
200  QDPIO::cerr << "No enum " << typeIDString << " registered for std::string " << e.val_str << " while parsing Binary lookup" << std::endl;
202  QDP_abort(1);
203  }
204  }
205 
206 
207  //! Writer
209  BinaryWriter& bin_out,
210  const EnumType& t) {
211  std::string string_out;
212  // Try to look up the std::string for the enum
213  try {
214  string_out = lookUpString(t);
215  }
216  catch( StringLookupException& e) {
217  QDPIO::cerr << "No std::string found for enum " << typeIDString << " with value " << t << " while attempting to write BIN " << std::endl;
219  QDP_abort(1);
220  }
221 
222  // Try writing the std::string
223  try {
224  QDP::writeDesc(bin_out, string_out);
225  }
226  catch( const std::string& bin_e ) {
227  QDPIO::cerr << "Caught Exception writing BIN: " << bin_e << std::endl;
228  QDP_abort(1);
229  }
230  }
231 
232  };
233 
234 
235 }
236 
237 
238 #endif
Primary include file for CHROMA library code.
Main enum std::map holder.
Definition: enum_type_map.h:27
void write(const std::string &typeIDString, XMLWriter &xml_out, const std::string &path, const EnumType &t)
Writer.
EnumTypeMap(const EnumTypeMap< EnumType > &t)
Definition: enum_type_map.h:36
void read(const std::string &typeIDString, BinaryReader &bin_in, EnumType &t)
"Reader"
bool registerPair(const std::string &s, const EnumType t)
Function registers a std::string/enum pair.
Definition: enum_type_map.h:73
void dumpMapStrings(const std::string &typeIDString)
Definition: enum_type_map.h:48
void write(const std::string &typeIDString, BinaryWriter &bin_out, const EnumType &t)
Writer.
std::string lookUpString(const EnumType &t)
Look up a std::string from an enum.
EnumType lookUpEnum(const std::string &s)
Look up an enum based on a std::string.
Definition: enum_type_map.h:96
std::map< EnumType, std::string > Enum2Str
Definition: enum_type_map.h:30
std::map< EnumType, std::string >::iterator Enum2StrIterator
Definition: enum_type_map.h:32
std::map< std::string, EnumType > Str2Enum
Definition: enum_type_map.h:29
std::map< std::string, EnumType >::iterator Str2EnumIterator
Definition: enum_type_map.h:31
void read(const std::string &typeIDString, XMLReader &xml_in, const std::string &path, EnumType &t)
"Reader"
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
int it
Definition: meslate.cc:33
int t
Definition: meslate.cc:37
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
multi1d< LatticeFermion > s(Ncb)
::std::string string
Definition: gtest.h:1979
EnumLookupException(const std::string &s)
Definition: enum_type_map.h:39