flags.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 /* --------------------------------------------------------------------- */
11 /* --- INCLUDE --------------------------------------------------------- */
12 /* --------------------------------------------------------------------- */
13 
15 #include <stdlib.h>
16 
17 #include <list>
18 
20 #include <sot/core/debug.hh>
21 #include <sot/core/flags.hh>
22 
23 using namespace std;
24 using namespace dynamicgraph::sot;
25 
26 /* --------------------------------------------------------------------- */
27 /* --- CLASS ----------------------------------------------------------- */
28 /* --------------------------------------------------------------------- */
29 
30 Flags::Flags(const bool &b) : flags(), outOfRangeFlag(b) {}
31 
32 Flags::Flags(const char *_flags)
33  : flags(strlen(_flags)), outOfRangeFlag(false) {
34  for (unsigned int i = 0; i < flags.size(); ++i) {
35  switch (_flags[i]) {
36  case '0':
37  flags[i] = false;
38  break;
39  case '1':
40  flags[i] = true;
41  break;
42  case ' ':
43  break;
44  default:
45  throw std::invalid_argument("Could not parse input string " +
46  std::string(_flags) + ". Expected 0 or 1.");
47  }
48  }
49 }
50 
51 Flags::Flags(const std::vector<bool> &_flags)
52  : flags(_flags), outOfRangeFlag(false) {}
53 
54 Flags::operator bool(void) const {
55  if (outOfRangeFlag) return true;
56  for (unsigned int i = 0; i < flags.size(); ++i)
57  if (flags[i]) return true;
58  return false;
59 }
60 
61 /* --------------------------------------------------------------------- */
62 
63 bool Flags::operator()(const int &i) const {
64  if (i < (int)flags.size()) return flags[i];
65  return outOfRangeFlag;
66 }
67 
68 /* --------------------------------------------------------------------- */
69 void Flags::add(const bool &b) { flags.push_back(b); }
70 
71 /* --------------------------------------------------------------------- */
72 void Flags::set(const unsigned int &idx) {
73  if (idx < flags.size()) flags[idx] = true;
74 }
75 
76 void Flags::unset(const unsigned int &idx) {
77  if (idx < flags.size()) flags[idx] = false;
78 }
79 
80 namespace dynamicgraph {
81 namespace sot {
82 
83 /* --------------------------------------------------------------------- */
84 Flags Flags::operator!(void) const {
85  Flags res = *this;
86  res.flags.flip();
88  return res;
89 }
90 
91 Flags operator&(const Flags &f1, const Flags &f2) {
92  Flags res = f1;
93  res &= f2;
94  return res;
95 }
96 
97 Flags operator|(const Flags &f1, const Flags &f2) {
98  Flags res = f1;
99  res |= f2;
100  return res;
101 }
102 
103 Flags &Flags::operator&=(const Flags &f2) {
104  if (f2.flags.size() > flags.size())
105  flags.resize(f2.flags.size(), outOfRangeFlag);
106  for (unsigned int i = 0; i < f2.flags.size(); ++i)
107  flags[i] = flags[i] & f2.flags[i];
108  for (auto i = f2.flags.size(); i < flags.size(); ++i)
109  flags[i] = flags[i] & f2.outOfRangeFlag;
111  return *this;
112 }
113 
115  if (f2.flags.size() > flags.size())
116  flags.resize(f2.flags.size(), outOfRangeFlag);
117  for (unsigned int i = 0; i < f2.flags.size(); ++i)
118  flags[i] = flags[i] | f2.flags[i];
119  for (auto i = f2.flags.size(); i < flags.size(); ++i)
120  flags[i] = flags[i] | f2.outOfRangeFlag;
122  return *this;
123 }
124 
125 /* --------------------------------------------------------------------- */
126 std::ostream &operator<<(std::ostream &os, const Flags &fl) {
127  for (auto f : fl.flags) os << (f ? '1' : '0');
128  return os;
129 }
130 
131 std::istream &operator>>(std::istream &is, Flags &fl) {
132  char c;
133  fl.flags.clear();
134  while (is.get(c).good()) {
135  switch (c) {
136  case '0':
137  fl.flags.push_back(false);
138  break;
139  case '1':
140  fl.flags.push_back(true);
141  break;
142  case ' ':
143  break;
144  default:
145  throw std::invalid_argument("Could not parse input character " +
146  std::string(1, c) + ". Expected 0 or 1.");
147  }
148  }
149  return is;
150 }
151 
152 } /* namespace sot */
153 } /* namespace dynamicgraph */
Flags operator!(void) const
Definition: flags.cpp:84
Flags(const bool &b=false)
Definition: flags.cpp:30
SOT_CORE_EXPORT friend std::istream & operator>>(std::istream &is, Flags &fl)
Definition: flags.cpp:131
int i
Vec3f b
SOT_CORE_EXPORT friend Flags operator|(const Flags &f1, const Flags &f2)
Definition: flags.cpp:97
Vec3f c
void set(const unsigned int &i)
Definition: flags.cpp:72
void add(const bool &b)
Definition: flags.cpp:69
std::vector< bool > flags
Definition: flags.hh:35
SOT_CORE_EXPORT friend Flags operator &(const Flags &f1, const Flags &f2)
Definition: flags.cpp:91
void f(void)
Flags & operator|=(const Flags &f2)
Definition: flags.cpp:114
res
bool operator()(const int &i) const
Definition: flags.cpp:63
SOT_CORE_EXPORT friend std::ostream & operator<<(std::ostream &os, const Flags &fl)
Definition: flags.cpp:126
Flags & operator &=(const Flags &f2)
void unset(const unsigned int &i)
Definition: flags.cpp:76


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26