TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTKMacros.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_MACRO
24 #define COM_DAFER45_TBTK_MACRO
25 
26 #include "TBTK/Streams.h"
27 
28 #include <chrono>
29 #include <cstring>
30 #include <ctime>
31 #include <sstream>
32 #include <string>
33 
34 #define TBTK_VERSION_STRING std::to_string(TBTK_VERSION_MAJOR) + "." \
35  + std::to_string(TBTK_VERSION_MINOR) + "." \
36  + std::to_string(TBTK_VERSION_PATCH)
37 
38 #define TBTK_ABOUT_STRING \
39  "TBTK\n" \
40  "Version:\t" + TBTK_VERSION_STRING + "\n" \
41  "Git hash:\t" TBTK_VERSION_GIT_HASH
42 
43 inline std::string TBTK_GET_CURRENT_TIME_STRING(){
44  std::chrono::time_point<std::chrono::system_clock> timePoint
45  = std::chrono::system_clock::now();
46  std::time_t now = std::chrono::system_clock::to_time_t(timePoint);
47 
48  return std::ctime(&now);
49 }
50 
51 #define TBTK_RUNTIME_CONTEXT_STRING \
52  TBTK_ABOUT_STRING + "\n" \
53  + "Date:\t" + TBTK_GET_CURRENT_TIME_STRING()
54 
55 #ifdef TBTKOptimize
56  #define TBTKAssert(expression, function, message, hint) ;
57  #define TBTKExceptionAssert(expression, exception);
58  #define TBTKExit(function, message, hint) exit(1);
59 #else
60  #define TBTKAssert(expression, function, message, hint) \
61  if(!(expression)){ \
62  TBTK::Streams::err << "Error in " << function << "\n"; \
63  TBTK::Streams::err << "\t" << message << "\n"; \
64  std::stringstream hintStream; \
65  hintStream << hint; \
66  if(std::strcmp(hintStream.str().c_str(), "") != 0) \
67  TBTK::Streams::err << "\tHint: " << hint << "\n"; \
68  TBTK::Streams::err << "\tWhere: " << __FILE__ << ", " << __LINE__ << "\n"; \
69  if(TBTK::Streams::logIsOpen()) \
70  TBTK::Streams::closeLog(); \
71  exit(1); \
72  }
73 
74  #define TBTKExceptionAssert(expression, exception) \
75  if(!(expression)) \
76  throw exception;
77 
78  #define TBTKExit(function, message, hint) \
79  TBTK::Streams::err << "Error in " << function << "\n"; \
80  TBTK::Streams::err << "\t" << message << "\n"; \
81  std::stringstream hintStream; \
82  hintStream << hint; \
83  if(std::strcmp(hintStream.str().c_str(), "") != 0) \
84  TBTK::Streams::err << "\tHint: " << hint << "\n"; \
85  TBTK::Streams::err << "\tWhere: " << __FILE__ << ", " << __LINE__ << "\n"; \
86  if(TBTK::Streams::logIsOpen()) \
87  TBTK::Streams::closeLog(); \
88  exit(1);
89 #endif
90 
91 #define TBTKNotYetImplemented(function) \
92  TBTK::Streams::err << "Error in " << function << "\n"; \
93  TBTK::Streams::err << "\tNot yet implemented.\n"; \
94  TBTK::Streams::err << "\tWhere: " << __FILE__ << ", " << __LINE__ << "\n"; \
95  if(TBTK::Streams::logIsOpen()) \
96  TBTK::Streams::closeLog(); \
97  exit(1);
98 
99 #define TBTKReadableCodeBlock(code) ;
100 
101 #define TBTKWhere std::string(__FILE__) + ", " + std::to_string(__LINE__)
102 
103 #endif
Streams for TBTK output.