Simpatico  v1.10
FlagSet.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include <util/global.h>
9 #include <util/misc/FlagSet.h>
10 
11 namespace Util
12 {
13 
14  /*
15  * Default constructor.
16  */
18  {}
19 
20  /*
21  * Constructor, sets allowed string.
22  */
24  { setAllowed(allowed); }
25 
26  /*
27  * Set allowed string, clear actual string.
28  */
29  void FlagSet::setAllowed(std::string allowed)
30  {
31  allowed_ = allowed;
32  actual_.clear();
33 
34  // Create map, initializing isActive to false for all.
35  map_.clear();
36  char c;
37  int n = allowed_.size();
38  for (int i=0; i < n; ++i) {
39  c = allowed_[i];
40  map_.insert(std::pair<char, bool>(c, false));
41  }
42  }
43 
44  /*
45  * Set string of actual character flags.
46  */
48  {
49  actual_ = actual;
50 
51  int n = allowed_.size();
52  if (n > 0) {
53 
54  // Set isActive to false for all allowed characters
55  MapType::iterator iter = map_.begin();
56  for ( ; iter != map_.end(); ++iter) {
57  iter->second = false;
58  }
59 
60  // Set isActive true for actual characters
61  char c, m;
62  int j = 0;
63  m = allowed_[j];
64  for (unsigned int i = 0; i < actual.size(); ++i) {
65  c = actual[i];
66  while (c != m) {
67  ++j;
68  if (j == n) {
69  std::string msg = "Unknown character ";
70  msg += c;
71  UTIL_THROW(msg.c_str());
72  }
73  m = allowed_[j];
74  assert(map_.count(m));
75  }
76  iter = map_.find(m);
77  assert(iter != map_.end());
78  iter->second = true;
79  }
80 
81  }
82  }
83 
84 }
const std::string & actual() const
Return the string of character for which flags are set.
Definition: FlagSet.h:130
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
FlagSet()
Default constructor.
Definition: FlagSet.cpp:17
Utility classes for scientific computation.
Definition: accumulators.mod:1
void setActualOrdered(std::string actual)
Set the string of actual flag characters.
Definition: FlagSet.cpp:47
void setAllowed(std::string allowed)
Set or reset the string of allowed flags.
Definition: FlagSet.cpp:29
const std::string & allowed() const
Return the string of allowed characters.
Definition: FlagSet.h:123