OTB  7.0.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) << "file " __FILE__ ", line " << __LINE__ << ", " x
48 
49 #define otbClassContext(x) << this->GetNameOfClass() << " (" << this << "): " x
50 
51 // Beware that to log to CRITICAL level, level should be passed as "Error"
52 #define otbLogMacro(level, msg) \
53  { \
54  std::ostringstream itkmsg; \
55  itkmsg msg << "\n"; \
56  otb::Logger::Instance()->level(itkmsg.str().c_str()); \
57  }
58 
59 
60 // Re-definition of old log macros to use the otbLogMacro
61 #define otbDebugMacro(x) otbLogMacro(Debug, otbFileContext(otbClassContext(x)))
62 #define otbMsgDebugMacro(x) otbLogMacro(Debug, otbFileContext(x))
63 #define otbGenericMsgDebugMacro(x) otbLogMacro(Debug, x)
64 #define otbMsgDevMacro(x) otbLogMacro(Debug, otbFileContext(x))
65 #define otbWarningMacro(x) otbLogMacro(Warning, otbFileContext(otbClassContext(x)))
66 #define otbGenericWarningMacro(x) otbLogMacro(Warning, otbFileContext(x))
67 #define otbGenericMsgTestingMAcro(x) otbLogMacro(Info, "[testing] " << x)
68 
72 #define otbControlConditionTestMacro(condition, message) \
73  { \
74  if ((condition)) \
75  itkGenericExceptionMacro(<< message); \
76  }
77 
79 #define otbSetObjectMemberMacro(object, name, type) \
80  virtual void Set##name(const type _arg) \
81  { \
82  itkDebugMacro("setting member " #name " to " << _arg); \
83  this->m_##object->Set##name(_arg); \
84  this->Modified(); \
85  }
86 
87 
89 #define otbGetObjectMemberMacro(object, name, type) \
90  virtual type Get##name() \
91  { \
92  itkDebugMacro("returning " << #name " of " << this->m_##object->Get##name()); \
93  return this->m_##object->Get##name(); \
94  }
95 
96 
100 #define otbGetObjectMemberConstMacro(object, name, type) \
101  virtual type Get##name() const \
102  { \
103  itkDebugMacro("returning " << #name " of " << this->m_##object->Get##name()); \
104  return this->m_##object->Get##name(); \
105  }
106 
107 
112 #define otbGetObjectMemberConstReferenceMacro(object, name, type) \
113  virtual const type& Get##name() const \
114  { \
115  itkDebugMacro("returning " << #name " of " << this->m_##object->Get##name()); \
116  return this->m_##object->Get##name(); \
117  }
118 
119 
123 #define otbTestingCheckValidCommand(command) \
124  { \
125  try \
126  { \
127  command; \
128  } \
129  catch (const std::exception&) \
130  { \
131  throw; \
132  } \
133  catch (...) \
134  { \
135  std::ostringstream message; \
136  message << "otb::ERROR Unknown error while running " << #command << " (catch(...) )"; \
137  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
138  throw e_; \
139  } \
140  std::cout << " Checking valid command " << #command " ok." << std::endl; \
141  }
142 
143 
144 #define otbGenericExceptionMacro(T, x) \
145  { \
146  std::ostringstream message; \
147  message << "otb::ERROR: " x; \
148  T e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
149  throw e_; \
150  }
151 
152 #define otbTestingCheckNotValidCommand(command) \
153  { \
154  int result(1); \
155  try \
156  { \
157  command; \
158  } \
159  catch (std::bad_alloc & err) \
160  { \
161  throw err; \
162  } \
163  catch (itk::ExceptionObject&) \
164  { \
165  std::cout << "Checking not valid Command " << #command " ok." << std::endl; \
166  result = 0; \
167  } \
168  catch (const std::exception& stde) \
169  { \
170  throw stde; \
171  } \
172  catch (...) \
173  { \
174  std::ostringstream message; \
175  message << "otb::ERROR Unknown error while running " << #command << " (catch(...) )"; \
176  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
177  throw e_; \
178  } \
179  if (result == 1) \
180  { \
181  std::ostringstream message; \
182  message << "otb::ERROR: " << #command << " should be throwing an exception."; \
183  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str(), ITK_LOCATION); \
184  throw e_; \
185  } \
186  }
187 
188 #define otbUnusedMacro(x) \
189  do \
190  { \
191  (void)sizeof(x); \
192  } while (0)
193 
194 //
195 // Unused variable warnings in Debug/Release management.
196 // see http://stackoverflow.com/questions/2290509/debug-vs-ndebug#2290616
197 #ifdef OTB_DEBUG
198 #define otbUseInDebug(name) name
199 #define otbUseInRelease(name)
200 #else // NDEBUG
201 #define otbUseInDebug(name)
202 #define otbUseInRelease(name) name
203 #endif // NDEBUG
204 
205 #endif // end of otbMacro.h
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.