glucat  0.8.4
control.h
Go to the documentation of this file.
1 #ifndef _GLUCAT_CONTROL_H
2 #define _GLUCAT_CONTROL_H
3 /***************************************************************************
4  GluCat : Generic library of universal Clifford algebra templates
5  control.h : Define and set parameters to control tests
6  -------------------
7  begin : 2010-04-21
8  copyright : (C) 2010-2016 by Paul C. Leopardi
9  ***************************************************************************
10 
11  This library is free software: you can redistribute it and/or modify
12  it under the terms of the GNU Lesser General Public License as published
13  by the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public License
22  along with this library. If not, see <http://www.gnu.org/licenses/>.
23 
24  ***************************************************************************
25  This library is based on a prototype written by Arvind Raja and was
26  licensed under the LGPL with permission of the author. See Arvind Raja,
27  "Object-oriented implementations of Clifford algebras in C++: a prototype",
28  in Ablamowicz, Lounesto and Parra (eds.)
29  "Clifford algebras with numeric and symbolic computations", Birkhauser, 1996.
30  ***************************************************************************
31  See also Arvind Raja's original header comments in glucat.h
32  ***************************************************************************/
33 #include "glucat/glucat_config.h"
34 #include "test/try_catch.h"
35 
36 namespace glucat
37 {
39  class control_t
40  {
41  private:
43  bool m_valid;
44  bool valid() const
45  { return m_valid; }
46 
48  bool m_catch_exceptions;
49  bool catch_exceptions() const
50  { return m_catch_exceptions; }
51 
53  static bool m_verbose_output;
54 
56  control_t(int argc, char ** argv);
57  // Enforce singleton
58  // Reference: A. Alexandrescu, "Modern C++ Design", Chapter 6
59  control_t() {}
60  ~control_t() {}
61  control_t(const control_t&);
63 
67  friend class friend_for_private_destructor;
68  public:
71  static const control_t& control(int argc, char ** argv)
72  { static const control_t c(argc, argv); return c; }
73 
75  int call(intfn f) const;
77  int call(intintfn f, int arg) const;
78 
80  static bool verbose()
81  { return m_verbose_output; }
82  };
83 
85  bool control_t::m_verbose_output = false;
86 
89  control_t(int argc, char ** argv)
90  : m_valid(true), m_catch_exceptions(true)
91  {
92  bool print_help = false;
93  const std::string& arg_0_str = argv[0];
94  const std::string program_name = arg_0_str.substr(arg_0_str.find_last_of('/')+1);
95  for (int arg_ndx = 1; arg_ndx < argc; ++arg_ndx)
96  {
97  const std::string& arg_str = argv[arg_ndx];
98  bool valid = false;
99  if (arg_str.substr(0,2) == "--")
100  {
101  valid = true;
102  const std::string& arg_name = arg_str.substr(2);
103  if (arg_name == "help")
104  {
105  this->m_valid = false;
106  print_help = true;
107  }
108  else if (arg_name == "verbose")
109  this->m_verbose_output = true;
110  else if (arg_name == "no-catch")
111  this->m_catch_exceptions = false;
112  else
113  valid = false;
114  }
115  if (!valid)
116  {
117  std::cout << "Invalid argument: " << arg_str << std::endl;
118  this->m_valid = false;
119  print_help = true;
120  }
121  }
122  if (print_help)
123  {
124  std::cout << program_name << " for " << GLUCAT_PACKAGE_NAME << " version " << GLUCAT_VERSION << ":" << std::endl;
125  std::cout << "Usage: " << program_name << " [option ...]" << std::endl;
126  std::cout << "Options:" << std::endl;
127  std::cout << " --help : Print this summary." << std::endl;
128  std::cout << " --no-catch : Do not catch exceptions." << std::endl;
129  std::cout << " --verbose : Produce more detailed test output." << std::endl;
130  }
131  }
132 
134  inline
135  int
137  call(intfn f) const
138  {
139  if (valid())
140  return (catch_exceptions())
141  ? try_catch(f)
142  : (*f)();
143  else
144  return 1;
145  }
146 
148  inline
149  int
151  call(intintfn f, int arg) const
152  {
153  if (valid())
154  return (catch_exceptions())
155  ? try_catch(f, arg)
156  : (*f)(arg);
157  else
158  return 1;
159  }
160 }
161 #endif // _GLUCAT_CONTROL_H
glucat::control_t::m_valid
bool m_valid
Test parameters are valid.
Definition: control.h:101
glucat::control_t::friend_for_private_destructor
friend class friend_for_private_destructor
Definition: control.h:125
try_catch.h
glucat::control_t::m_verbose_output
static bool m_verbose_output
Produce more detailed output from tests.
Definition: control.h:111
glucat::try_catch
int try_catch(intfn f)
Exception catching for functions returning int.
Definition: try_catch.h:78
glucat::control_t::call
int call(intfn f) const
Call a function that returns int.
Definition: control.h:166
glucat::control_t::catch_exceptions
bool catch_exceptions() const
Definition: control.h:107
glucat::control_t
Parameters to control tests.
Definition: control.h:69
GLUCAT_VERSION
#define GLUCAT_VERSION
Definition: glucat_config.h:98
glucat::control_t::verbose
static bool verbose()
Produce more detailed output from tests.
Definition: control.h:138
glucat::control_t::control_t
control_t()
Definition: control.h:117
glucat::control_t::~control_t
~control_t()
Definition: control.h:118
glucat::control_t::m_catch_exceptions
bool m_catch_exceptions
Catch exceptions.
Definition: control.h:106
GLUCAT_PACKAGE_NAME
#define GLUCAT_PACKAGE_NAME
Definition: glucat_config.h:68
glucat::intintfn
int(* intintfn)(int)
For exception catching: pointer to function of int returning int.
Definition: try_catch.h:69
glucat::control_t::control
static const control_t & control(int argc, char **argv)
Definition: control.h:129
glucat
Definition: clifford_algebra.h:39
glucat_config.h
glucat::control_t::valid
bool valid() const
Definition: control.h:102
glucat::control_t::operator=
control_t & operator=(const control_t &)
glucat::intfn
int(* intfn)()
For exception catching: pointer to function returning int.
Definition: try_catch.h:66