glucat 0.12.0
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 ***************************************************************************/
34#include "test/try_catch.h"
35
36namespace glucat
37{
40 {
41 private:
43 bool m_valid;
44 bool valid() const
45 { return m_valid; }
46
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() = default;
60 ~control_t() = default;
61 control_t(const control_t&) = delete;
62 control_t& operator= (const control_t&) = delete;
63
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
Parameters to control tests.
Definition: control.h:40
int call(intfn f) const
Call a function that returns int.
Definition: control.h:137
~control_t()=default
friend class friend_for_private_destructor
Definition: control.h:67
static const control_t & control(int argc, char **argv)
Definition: control.h:71
bool m_valid
Test parameters are valid.
Definition: control.h:43
control_t()=default
control_t(const control_t &)=delete
static bool verbose()
Produce more detailed output from tests.
Definition: control.h:80
bool m_catch_exceptions
Catch exceptions.
Definition: control.h:48
static bool m_verbose_output
Produce more detailed output from tests.
Definition: control.h:53
control_t & operator=(const control_t &)=delete
bool valid() const
Definition: control.h:44
bool catch_exceptions() const
Definition: control.h:49
#define GLUCAT_PACKAGE_NAME
Definition: glucat_config.h:89
#define GLUCAT_VERSION
int(* intintfn)(int)
For exception catching: pointer to function of int returning int.
Definition: try_catch.h:40
int(* intfn)()
For exception catching: pointer to function returning int.
Definition: try_catch.h:37
int try_catch(intfn f)
Exception catching for functions returning int.
Definition: try_catch.h:49