Fluid structure interaction suite
patterns_unsigned_int.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2022 by Luca Heltai
4 //
5 // This file is part of the FSI-suite platform, based on the deal.II library.
6 //
7 // The FSI-suite platform is free software; you can use it, redistribute it,
8 // and/or modify it under the terms of the GNU Lesser General Public License as
9 // published by the Free Software Foundation; either version 3.0 of the License,
10 // or (at your option) any later version. The full text of the license can be
11 // found in the file LICENSE at the top level of the FSI-suite platform
12 // distribution.
13 //
14 // ---------------------------------------------------------------------
15 
17 
18 namespace dealii
19 {
20  namespace Patterns
21  {
22  const unsigned int UnsignedInteger::min_int_value =
23  std::numeric_limits<unsigned int>::min();
24  const unsigned int UnsignedInteger::max_int_value =
25  std::numeric_limits<unsigned int>::max();
26 
27  const char *UnsignedInteger::description_init = "[UnsignedInteger";
28 
29 
30  UnsignedInteger::UnsignedInteger(const unsigned int lower_bound,
31  const unsigned int upper_bound)
32  : lower_bound(lower_bound)
33  , upper_bound(upper_bound)
34  {}
35 
36 
37  bool
38  UnsignedInteger::match(const std::string &test_string) const
39  {
40  std::istringstream str(test_string);
41 
42  unsigned int i;
43  if (!(str >> i))
44  return false;
45 
46  // if (!has_only_whitespace(str))
47  // return false;
48  // check whether valid bounds
49  // were specified, and if so
50  // enforce their values
51  if (lower_bound <= upper_bound)
52  return ((lower_bound <= i) && (upper_bound >= i));
53  else
54  return true;
55  }
56 
57 
58 
59  std::string
60  UnsignedInteger::description(const OutputStyle style) const
61  {
62  switch (style)
63  {
64  case Machine:
65  {
66  // check whether valid bounds
67  // were specified, and if so
68  // output their values
69  if (lower_bound <= upper_bound)
70  {
71  std::ostringstream description;
72 
73  description << description_init << " range " << lower_bound
74  << "..." << upper_bound << " (inclusive)]";
75  return description.str();
76  }
77  else
78  // if no bounds were given, then
79  // return generic string
80  return "[ UnsignedInteger]";
81  }
82  case Text:
83  {
84  if (lower_bound <= upper_bound)
85  {
86  std::ostringstream description;
87 
88  description << "An unsigned integer n such that "
89  << lower_bound << " <= n <= " << upper_bound;
90 
91  return description.str();
92  }
93  else
94  return "An unsigned integer";
95  }
96  case LaTeX:
97  {
98  if (lower_bound <= upper_bound)
99  {
100  std::ostringstream description;
101 
102  description << "An unsigned integer @f$n@f$ such that @f$"
103  << lower_bound << "\\leq n \\leq " << upper_bound
104  << "@f$";
105 
106  return description.str();
107  }
108  else
109  return "An unsigned integer";
110  }
111  default:
112  AssertThrow(false, ExcNotImplemented());
113  }
114  // Should never occur without an exception, but prevent compiler from
115  // complaining
116  return "";
117  }
118 
119 
120 
121  std::unique_ptr<PatternBase>
123  {
124  return std::unique_ptr<PatternBase>(
126  }
127 
128 
129 
130  std::unique_ptr<UnsignedInteger>
131  UnsignedInteger::create(const std::string &description)
132  {
133  if (description.compare(0,
134  std::strlen(description_init),
135  description_init) == 0)
136  {
137  std::istringstream is(description);
138 
139  if (is.str().size() > strlen(description_init) + 1)
140  {
141  // TODO: verify that description matches the pattern "^\[
142  // UnsignedInteger range \d+\.\.\.\d+\]@f$"
144 
145  is.ignore(strlen(description_init) + strlen(" range "));
146 
147  if (!(is >> lower_bound))
148  return std::make_unique<UnsignedInteger>();
149 
150  is.ignore(strlen("..."));
151 
152  if (!(is >> upper_bound))
153  return std::make_unique<UnsignedInteger>();
154 
155  return std::make_unique<UnsignedInteger>(lower_bound,
156  upper_bound);
157  }
158  else
159  return std::make_unique<UnsignedInteger>();
160  }
161  else
162  return std::unique_ptr<UnsignedInteger>();
163  }
164  } // namespace Patterns
165 } // namespace dealii
virtual std::string description(const OutputStyle style=Machine) const override
virtual bool match(const std::string &test_string) const override
static const unsigned int min_int_value
virtual std::unique_ptr< PatternBase > clone() const override
static const unsigned int max_int_value
static std::unique_ptr< UnsignedInteger > create(const std::string &description)
UnsignedInteger(const unsigned int lower_bound=min_int_value, const unsigned int upper_bound=max_int_value)
static ::ExceptionBase & ExcNotImplemented()
#define AssertThrow(cond, exc)