Macros.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/Marcos.h
22 
23  \brief Internal Raster Processing module macros.
24  */
25 
26 #ifndef __TERRALIB_RP_INTERNAL_MACROS_H
27 #define __TERRALIB_RP_INTERNAL_MACROS_H
28 
29  #include "../core/logger/Logger.h"
30  #include "../common/Config.h"
31  #include "../common/MathUtils.h"
32  #include "Module.h"
33  #include "Exception.h"
34 
35  #include <boost/lexical_cast.hpp>
36 
37  #include <sstream>
38  #include <string>
39  #include <iostream>
40 
41  /*!
42  \brief Debug mode selection based on NDEBUG define.
43  */
44  #ifndef TERP_DEBUG_MODE
45  #ifndef NDEBUG
46  /*! \brief Debug mode selection flag. */
47  #define TERP_DEBUG_MODE
48 
49  /*! \brief STDOUT logging selection flag. */
50  #ifndef TERP_ENABLE_STDOUT_LOG
51  #define TERP_ENABLE_STDOUT_LOG
52  #endif
53  #endif
54  #endif
55 
56  /*!
57  \brief Logs a message to stdout
58  \param message Message to be logged.
59  */
60  #ifdef TERP_ENABLE_STDOUT_LOG
61  #define TERP_LOGMSG_STDOUT( message ) \
62  std::cout << std::endl << "Message : " \
63  << __FILE__ \
64  << ":" << __LINE__ \
65  << " - " << boost::lexical_cast< std::string >( message ) \
66  << std::endl;
67  #else
68  #define TERP_LOGMSG_STDOUT( message )
69  #endif
70 
71  /*!
72  \brief Logs a error message to stderr
73  \param message Message to be logged.
74  */
75  #ifdef TERP_ENABLE_STDOUT_LOG
76  #define TERP_LOGERR_STDOUT( message ) \
77  std::cerr << std::endl << "Error : " \
78  << __FILE__ \
79  << ":" << __LINE__ \
80  << " - " << boost::lexical_cast< std::string >( message ) \
81  << std::endl;
82  #else
83  #define TERP_LOGERR_STDOUT( message )
84  #endif
85 
86  /*!
87  \brief Logs a warning message to stdout
88  \param message Message to be logged.
89  */
90  #ifdef TERP_ENABLE_STDOUT_LOG
91  #define TERP_LOGWARN_STDOUT( message ) \
92  std::cout << std::endl << "Warning : " \
93  << __FILE__ \
94  << ":" << __LINE__ \
95  << " - " << boost::lexical_cast< std::string >( message ) \
96  << std::endl;
97  #else
98  #define TERP_LOGWARN_STDOUT( message )
99  #endif
100 
101 
102  /*!
103  \brief Logs a message.
104  \param message Message to be logged.
105  */
106  #define TERP_LOGMSG( message ) \
107  { \
108  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
109  TE_LOG_INFO( boost::lexical_cast< std::string >( message ).c_str() ); \
110  TERP_LOGMSG_STDOUT( message ); \
111  };
112 
113  /*!
114  \brief Logs an error message.
115  \param message Message to be logged.
116  */
117  #define TERP_LOGERR( message ) \
118  { \
119  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
120  TE_LOG_ERROR( boost::lexical_cast< std::string >( message ).c_str() ); \
121  TERP_LOGERR_STDOUT( message ); \
122  };
123 
124  /*!
125  \brief Logs a warning message.
126  \param message Message to be logged.
127  */
128  #define TERP_LOGWARN( message ) \
129  { \
130  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
131  TE_LOG_WARN( boost::lexical_cast< std::string >( message ).c_str() ); \
132  TERP_LOGWARN_STDOUT( message ); \
133  };
134 
135  /*!
136  \brief Logs a error message and throws.
137  \param message Message to be logged.
138  */
139  #define TERP_LOG_AND_THROW( message ) \
140  { \
141  TERP_LOGERR( message ); \
142  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
143  };
144 
145  /*!
146  \brief Checks if value is true and throws an exception if not.
147  \param value Value to be checked.
148  \param message Message to be logged.
149  */
150  #define TERP_TRUE_OR_THROW( value , message ) \
151  if( ( value ) == 0 ) { \
152  TERP_LOGERR( boost::lexical_cast< std::string >( #value ) ); \
153  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
154  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
155  };
156 
157  /*!
158  \brief Variable watching.
159  \param variable Variable to be logged.
160  */
161  #define TERP_WATCH( variable ) \
162  { \
163  TERP_LOGMSG( "WATCH - " + boost::lexical_cast< std::string >( #variable ) + \
164  "=[" + boost::lexical_cast< std::string >( variable ) +"]" ); \
165  };
166 
167  /*!
168  \@brief Checks if value is true and logs an warning message if not.
169  \param value Value to be checked.
170  \@param message Message to be logged.
171  */
172  #define TERP_TRUE_OR_LOG( value , message ) \
173  if( ( value ) == 0 ) { \
174  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
175  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
176  };
177 
178  /*!
179  \brief Checks if value is true. For false values a warning message will be logged
180  and a return of context with false value will be done.
181 
182  \param value Value to be checked.
183  \param message Message to be logged.
184  */
185  #define TERP_TRUE_OR_RETURN_FALSE( value , message ) \
186  if( ( value ) == 0 ) { \
187  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
188  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
189  return false; \
190  };
191 
192  /*!
193  \brief Checks if value is true. For false values a warning message will be logged,
194  the current instance error messsage will also be updated and
195  a return of context with false value will be done.
196 
197  \param value Value to be checked.
198  \param message Message to be logged.
199  */
200  #define TERP_INSTANCE_TRUE_OR_RETURN_FALSE( value , message ) \
201  if( ( value ) == 0 ) { \
202  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
203  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
204  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
205  return false; \
206  };
207 
208  /*!
209  \brief Checks if value is true. For false values a warning message will be logged
210  and a return of context will be done.
211 
212  \param value Value to be checked.
213  \param message Message to be logged.
214  */
215  #define TERP_TRUE_OR_RETURN( value , message ) \
216  if( ( value ) == 0 ) { \
217  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
218  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
219  return; \
220  };
221 
222  /*!
223  \brief Checks if value is false. For true values a warning message
224  will be logged and a return of context with false value will be done.
225  *
226  \param value Value to be checked.
227  \param message Message to be logged.
228  */
229  #define TERP_FALSE_OR_RETURN_FALSE( value , message ) \
230  if( ( value ) != 0 ) { \
231  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
232  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
233  return false; \
234  };
235 
236  /*!
237  \brief Checks if value is false. For true values a warning message
238  will be logged, the current instance error messsage will be updated and a return of context with false value will be done.
239  *
240  \param value Value to be checked.
241  \param message Message to be logged.
242  */
243  #define TERP_INSTANCE_FALSE_OR_RETURN_FALSE( value , message ) \
244  if( ( value ) != 0 ) { \
245  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
246  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
247  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
248  return false; \
249  };
250 
251  /*!
252  \brief Checks if value is false. For true values a warning message
253  will be logged and a return of context will be done.
254  *
255  \param value Value to be checked.
256  \param message Message to be logged.
257  */
258  #define TERP_FALSE_OR_RETURN( value , message ) \
259  if( ( value ) != 0 ) { \
260  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
261  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
262  return; \
263  };
264 
265  /*!
266  \brief Logs a warning message will and return false.
267  \param message Message to be logged.
268  */
269  #define TERP_LOG_AND_RETURN_FALSE( message ) \
270  { \
271  TERP_LOGWARN( message ); \
272  return false; \
273  };
274 
275  /*!
276  \brief Logs a warning message, update the current instance error messsage and return false.
277  \param message Message to be logged.
278  */
279  #define TERP_INSTANCE_LOG_AND_RETURN_FALSE( message ) \
280  { \
281  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
282  TERP_LOGWARN( message ); \
283  return false; \
284  };
285 
286  /*!
287  \brief Logs a warning message will and return.
288  \param message Message to be logged.
289  */
290  #define TERP_LOG_AND_RETURN( message ) \
291  { \
292  TERP_LOGWARN( message ); \
293  return; \
294  };
295 
296  /*!
297  \brief Checks if value is false and logs an warning message if not.
298  \param value Value to be checked.
299  \param message Message to be logged.
300  */
301  #define TERP_FALSE_OR_LOG( value , message ) \
302  if( ( value ) != 0 ) { \
303  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
304  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
305  };
306 
307  /*!
308  \brief Checks if two values are equal and throws an exception if not.
309  \param value1 Value to be checked.
310  \param value2 Value to be checked.
311  \param message Message to be logged.
312  */
313  #define TERP_CHECK_EQUAL( value1 , value2 , message ) \
314  if( value1 != value2 ) \
315  { \
316  TERP_LOGERR(std::string( "[" ) + \
317  boost::lexical_cast< std::string >( #value1 ) \
318  + std::string( "!=" ) + \
319  + boost::lexical_cast< std::string >( #value2 ) \
320  + "][" \
321  + boost::lexical_cast< std::string >( value1 ) \
322  + std::string( "!=" ) + \
323  + boost::lexical_cast< std::string >( value2 ) ); \
324  TERP_LOG_AND_THROW( message ); \
325  };
326 
327  /*!
328  \brief Checks if two values are diferent and throws an exception if not.
329  \param value1 Value to be checked.
330  \param value2 Value to be checked.
331  \param message Message to be logged.
332  */
333  #define TERP_CHECK_NOT_EQUAL( value1 , value2 , message ) \
334  if( value1 == value2 ) \
335  { \
336  TERP_LOGERR(std::string( "[" ) + \
337  boost::lexical_cast< std::string >( #value1 ) \
338  + std::string( "==" ) + \
339  + boost::lexical_cast< std::string >( #value2 ) \
340  + "][" \
341  + boost::lexical_cast< std::string >( value1 ) \
342  + std::string( "==" ) + \
343  + boost::lexical_cast< std::string >( value2 ) ); \
344  TERP_LOG_AND_THROW( message ); \
345  };
346 
347  /*!
348  \brief Checks if two values are equal ( within an EPS ) and throws an exception if not.
349  \param value1 Value to be checked.
350  \param value2 Value to be checked.
351  \param eps EPS ( threshold )
352  \param message Message to be logged.
353  */
354  #define TERP_CHECK_EPS( value1 , value2 , eps , message ) \
355  { \
356  TERP_TRUE_OR_THROW( ( (eps) >= 0), "Invalid eps" ); \
357  double TERP_CHECK_EPS_double_diff = 0; \
358  double TERP_CHECK_EPS_double_value1 = (double)(value1); \
359  double TERP_CHECK_EPS_double_value2 = (double)(value2); \
360  double TERP_CHECK_EPS_double_eps = (double)(eps); \
361  if( TERP_CHECK_EPS_double_value1 < TERP_CHECK_EPS_double_value2 ) { \
362  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value2 - \
363  TERP_CHECK_EPS_double_value1 ); \
364  } else { \
365  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value1 - \
366  TERP_CHECK_EPS_double_value2 ); \
367  }; \
368  if( TERP_CHECK_EPS_double_diff > TERP_CHECK_EPS_double_eps ) \
369  { \
370  TERP_LOGERR( \
371  std::string( "Values are not equal: " ) + \
372  boost::lexical_cast< std::string >( #value1 ) + \
373  std::string( "=[") + \
374  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value1 ) + \
375  std::string( "] " ) + \
376  boost::lexical_cast< std::string >( #value2 ) + \
377  std::string( "=[") + \
378  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value2 ) + \
379  std::string( "] eps=[") + \
380  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_eps ) + \
381  std::string( "] diff=[") + \
382  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_diff ) \
383  ); \
384  TERP_LOG_AND_THROW( message ); \
385  }; \
386  };
387 
388  /*!
389  \brief Throws an exception for not implemented source.
390  */
391  #define TERP_NOT_IMPLEMENTED TERP_LOG_AND_THROW( "Not Implemented." );
392 
393  /*!
394  \brief Checks if value is true and throws an exception if not.
395  \note This macro will be disabled for non debug mode.
396  \param value Value to be checked.
397  \param message Message to be logged.
398  */
399  #ifdef TERP_DEBUG_MODE
400  #define TERP_DEBUG_TRUE_OR_THROW( value , message ) \
401  TERP_TRUE_OR_THROW( value , message );
402  #else
403  #define TERP_DEBUG_TRUE_OR_THROW( value , message );
404  #endif
405 
406  /*!
407  \brief Checks if value is true. For false values a warning message will be logged
408  and a return of context with false value will be done.
409 
410  \note This macro will be disabled for non debug mode.
411  \param value Value to be checked.
412  \param message Message to be logged.
413  */
414  #ifdef TERP_DEBUG_MODE
415  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message ) \
416  TERP_TRUE_OR_RETURN_FALSE( value , message );
417  #else
418  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message );
419  #endif
420 
421  /*!
422  \brief Minimum of two values.
423  */
424  #ifndef MIN
425  #define MIN(x,y) (std::min)( x , y )
426  #endif
427 
428  /*!
429  \brief Maximum of two values.
430  */
431  #ifndef MAX
432  #define MAX(x,y) (std::max)( x , y )
433  #endif
434 
435  /*!
436  \brief Absolute value.
437  */
438  #ifndef ABS
439  #define ABS(x) std::abs( x )
440  #endif
441 
442 #endif // __TERRALIB_RP_INTERNAL_MACROS_H
443 
Exception class.
This singleton defines the TerraLib Raster Processing module entry.