OTB  6.7.0
Orfeo Toolbox
otbMacro.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
28 #ifndef otbMacro_h
29 #define otbMacro_h
30 
31 #include "itkMacro.h"
32 #include "itkObject.h"
33 #include "otbConfigure.h"
34 #include "otbLogger.h"
35 
42 namespace otb
43 {
44 } // end namespace otb - this is here for documentation purposes
45 
46 
47 #define otbFileContext(x) \
48  << "file " __FILE__ ", line " << __LINE__<<", " x
49 
50 #define otbClassContext(x) \
51  << this->GetNameOfClass() << " (" << this << "): " x
52 
53 // Beware that to log to CRITICAL level, level should be passed as "Error"
54 #define otbLogMacro(level,msg) \
55  { \
56  std::ostringstream itkmsg; \
57  itkmsg msg << "\n"; \
58  otb::Logger::Instance()->level(itkmsg.str().c_str()); \
59  }
60 
61 
62 // Re-definition of old log macros to use the otbLogMacro
63 #define otbDebugMacro(x) otbLogMacro(Debug,otbFileContext(otbClassContext(x)))
64 #define otbMsgDebugMacro(x) otbLogMacro(Debug,otbFileContext(x))
65 #define otbGenericMsgDebugMacro(x) otbLogMacro(Debug,x)
66 #define otbMsgDevMacro(x) otbLogMacro(Debug,otbFileContext(x))
67 #define otbWarningMacro(x) otbLogMacro(Warning,otbFileContext(otbClassContext(x)))
68 #define otbGenericWarningMacro(x) otbLogMacro(Warning,otbFileContext(x))
69 #define otbGenericMsgTestingMAcro(x) otbLogMacro(Info,"[testing] "<<x)
70 
74 #define otbControlConditionTestMacro(condition, message) \
75  { \
76  if ((condition)) itkGenericExceptionMacro(<< message); \
77  }
78 
80 #define otbSetObjectMemberMacro(object, name, type) \
81  virtual void Set ## name (const type _arg) \
82  { \
83  itkDebugMacro("setting member " # name " to " << _arg); \
84  this->m_ ## object->Set ## name(_arg); \
85  this->Modified(); \
86  }
87 
88 
90 #define otbGetObjectMemberMacro(object, name, type) \
91  virtual type Get ## name () \
92  { \
93  itkDebugMacro("returning " << # name " of " << this->m_ ## object->Get ## name()); \
94  return this->m_ ## object->Get ## name(); \
95  }
96 
97 
101 #define otbGetObjectMemberConstMacro(object, name, type) \
102  virtual type Get ## name () const \
103  { \
104  itkDebugMacro("returning " << # name " of " << this->m_ ## object->Get ## name()); \
105  return this->m_ ## object->Get ## name(); \
106  }
107 
108 
113 #define otbGetObjectMemberConstReferenceMacro(object, name, type) \
114  virtual const type &Get ## name () const \
115  { \
116  itkDebugMacro("returning " << # name " of " << this->m_ ## object->Get ## name()); \
117  return this->m_ ## object->Get ## name(); \
118  }
119 
120 
124 #define otbTestingCheckValidCommand(command) \
125  { \
126  try \
127  { \
128  command; \
129  } \
130  catch (const std::exception&) { \
131  throw; } \
132  catch (...) \
133  { \
134  std::ostringstream message; \
135  message << "otb::ERROR Unknown error while running " << # command << " (catch(...) )"; \
136  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
137  throw e_; \
138  } \
139  std::cout << " Checking valid command " << # command " ok." << std::endl; \
140  }
141 
142 
143 #define otbGenericExceptionMacro(T, x) \
144 { \
145  std::ostringstream message; \
146  message << "otb::ERROR: " x; \
147  T e_(__FILE__, __LINE__, message.str(),ITK_LOCATION); \
148  throw e_;\
149 }
150 
151 #define otbTestingCheckNotValidCommand(command) \
152  { \
153  int result(1); \
154  try \
155  { \
156  command; \
157  } \
158  catch (std::bad_alloc& err) { \
159  throw err; } \
160  catch (itk::ExceptionObject&) { std::cout << "Checking not valid Command " << # command " ok." << std::endl; \
161  result = 0; } \
162  catch (const std::exception& stde) { \
163  throw stde; } \
164  catch (...) \
165  { \
166  std::ostringstream message; \
167  message << "otb::ERROR Unknown error while running " << # command << " (catch(...) )"; \
168  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
169  throw e_; \
170  } \
171  if (result == 1) \
172  { \
173  std::ostringstream message; \
174  message << "otb::ERROR: " << # command << " should be throwing an exception."; \
175  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
176  throw e_; \
177  } \
178  }
179 
180 # define otbUnusedMacro(x) do { (void)sizeof(x); } while(0)
181 
182 //
183 // Unused variable warnings in Debug/Release management.
184 // see http://stackoverflow.com/questions/2290509/debug-vs-ndebug#2290616
185 #ifdef OTB_DEBUG
186 # define otbUseInDebug( name ) name
187 # define otbUseInRelease( name )
188 #else // NDEBUG
189 # define otbUseInDebug( name )
190 # define otbUseInRelease( name ) name
191 #endif // NDEBUG
192 
193 #endif //end of otbMacro.h