blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
choices.h
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2013 Joakim Karlsson
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 
24 #ifndef CHOICES_145313413_H
25 #define CHOICES_145313413_H
26 
27 namespace choices {
28 
29  typedef std::map<std::string, std::string> options;
30 
31  inline std::ostream& operator<<(std::ostream& stm, const options& opt)
32  {
33  options::const_iterator it;
34  stm << "{ ";
35  for(it = opt.begin(); it != opt.end(); it++)
36  {
37  stm << (*it).first << ": " << (*it).second << " ";
38  }
39 
40  stm << "}" << std::endl;
41 
42  return stm;
43  }
44 
45  namespace details {
46 
47  inline void as_vect(int argc, const char *argv[], std::vector<std::string>& res)
48  {
49  res.assign(argv, argv + argc);
50  }
51 
52  inline std::string remove_prefix(const std::string s)
53  {
54  size_t pos = s.find("--");
55  return pos == std::string::npos ? s : s.substr(pos + 2);
56  }
57 
58  inline bool is_assignment(const std::string& s)
59  {
60  return s.find("=") != std::string::npos;
61  }
62 
63  inline bool is_flag(const std::string& s)
64  {
65  return s.find("--") == 0 && !is_assignment(s);
66  }
67 
68  inline std::string flag(const std::string& assignment)
69  {
70  std::string s = remove_prefix(assignment);
71  return s.substr(0, s.find("="));
72  }
73 
74  inline std::string value(const std::string& assignment)
75  {
76  return assignment.substr(assignment.find("=")+1);
77  }
78 
79  inline void get_options(const std::vector<std::string> v, options& opt)
80  {
81  std::vector<std::string>::const_iterator it;
82  for(it = v.begin(); it != v.end(); it++)
83  {
84  if(is_flag(*it))
85  {
86  opt[remove_prefix(*it)] = "";
87  }
88  else if(is_assignment(*it))
89  {
90  opt[flag(*it)] = value(*it);
91  }
92  }
93  }
94  }
95 
96  namespace d = details;
97 
98  inline options parse_cmd(int argc, const char *argv[])
99  {
100  options o;
101  if(argc == 0 || argv == 0)
102  {
103  return o;
104  }
105 
106  std::vector<std::string> v;
107  d::as_vect(argc, argv, v);
108 
109  v.erase(v.begin());
110 
111  d::get_options(v, o);
112 
113  return o;
114  }
115 
116  inline bool has_option(const std::string& option, const options& opt)
117  {
118  return opt.find(option) != opt.end();
119  }
120 
121  inline const std::string& option_value(const std::string& option, const options& opt)
122  {
123  options::const_iterator it = opt.find(option);
124  return (*it).second;
125  }
126 }
127 
128 #endif
Definition: choices.h:27