KSS Contract
C++ program by contract utilities
 All Namespaces Files Functions Macros
contract.hpp
Go to the documentation of this file.
1 //
2 // contract.hpp
3 // ksscontract
4 //
5 // Created by Steven W. Klassen on 2018-11-17.
6 // Copyright © 2018 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
10 #ifndef ksscontract_contract_hpp
11 #define ksscontract_contract_hpp
12 
13 #include <initializer_list>
14 #include <string>
15 
16 namespace kss { namespace contract {
17 
18  namespace _private {
19  struct Expression {
20  bool result = false;
21  const char* expr = nullptr;
22  const char* functionName = nullptr;
23  const char* fileName = nullptr;
24  unsigned lineNo = 0;
25  };
26 
27  void performThrowingCheck(const char* conditionType, const Expression& exp);
28  void performTerminatingCheck(const char* conditionType, const Expression& exp);
29 
30  inline void parameter(const Expression& exp) {
31  performThrowingCheck("Parameter", exp);
32  }
33 
34  inline void precondition(const Expression& exp) {
35  performTerminatingCheck("Precondition", exp);
36  }
37 
38  inline void condition(const Expression& exp) {
39  performTerminatingCheck("Condition", exp);
40  }
41 
42  inline void postcondition(const Expression& exp) {
43  performTerminatingCheck("Postcondition", exp);
44  }
45  }
46 
47 
52 # define KSS_EXPR(expr) kss::contract::_private::Expression {(expr), #expr, __PRETTY_FUNCTION__, __FILE__, __LINE__}
53 
54 
78  inline void parameters(std::initializer_list<_private::Expression> exps) {
79  for (const auto& exp : exps) {
80  _private::parameter(exp);
81  }
82  }
83 
84 
105  inline void preconditions(std::initializer_list<_private::Expression> exps) {
106  for (const auto& exp : exps) {
107  _private::precondition(exp);
108  }
109  }
110 
111 
135  inline void conditions(std::initializer_list<_private::Expression> exps) {
136  for (const auto& exp : exps) {
137  _private::condition(exp);
138  }
139  }
140 
141 
162  inline void postconditions(std::initializer_list<_private::Expression> exps) {
163  for (const auto& exp : exps) {
164  _private::postcondition(exp);
165  }
166  }
167 
168 }}
169 
170 #endif
void parameters(std::initializer_list< _private::Expression > exps)
Definition: contract.hpp:78
void preconditions(std::initializer_list< _private::Expression > exps)
Definition: contract.hpp:105
void postconditions(std::initializer_list< _private::Expression > exps)
Definition: contract.hpp:162
void conditions(std::initializer_list< _private::Expression > exps)
Definition: contract.hpp:135