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 an error message ( the current instance error messsage will also be updated).
126  \param message Message to be logged.
127  */
128  #define TERP_INSTANCE_LOGERR( message ) \
129  { \
130  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
131  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
132  };
133 
134  /*!
135  \brief Logs a warning message.
136  \param message Message to be logged.
137  */
138  #define TERP_LOGWARN( message ) \
139  { \
140  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
141  TE_LOG_WARN( boost::lexical_cast< std::string >( message ).c_str() ); \
142  TERP_LOGWARN_STDOUT( message ); \
143  };
144 
145  /*!
146  \brief Logs an warning message ( the current instance warning messsage will also be updated).
147  \param message Message to be logged.
148  */
149  #define TERP_INSTANCE_LOGWARN( message ) \
150  { \
151  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
152  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
153  };
154 
155  /*!
156  \brief Logs a error message and throws.
157  \param message Message to be logged.
158  */
159  #define TERP_LOG_AND_THROW( message ) \
160  { \
161  TERP_LOGERR( message ); \
162  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
163  };
164 
165  /*!
166  \brief Checks if value is true and throws an exception if not.
167  \param value Value to be checked.
168  \param message Message to be logged.
169  */
170  #define TERP_TRUE_OR_THROW( value , message ) \
171  if( ( value ) == 0 ) { \
172  TERP_LOGERR( boost::lexical_cast< std::string >( #value ) ); \
173  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
174  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
175  };
176 
177  /*!
178  \brief Checks if value is true and throws an exception if not ( the current instance warning messsage will also be updated).
179  \param value Value to be checked.
180  \param message Message to be logged.
181  */
182  #define TERP_INSTANCE_TRUE_OR_THROW( value, message ) \
183  if( ( value ) == 0 ) { \
184  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
185  TERP_LOGERR( boost::lexical_cast< std::string >( #value ) ); \
186  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
187  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
188  };
189 
190  /*!
191  \brief Variable watching.
192  \param variable Variable to be logged.
193  */
194  #define TERP_WATCH( variable ) \
195  { \
196  TERP_LOGMSG( "WATCH - " + boost::lexical_cast< std::string >( #variable ) + \
197  "=[" + boost::lexical_cast< std::string >( variable ) +"]" ); \
198  };
199 
200  /*!
201  \@brief Checks if value is true and logs an warning message if not.
202  \param value Value to be checked.
203  \@param message Message to be logged.
204  */
205  #define TERP_TRUE_OR_LOG( value , message ) \
206  if( ( value ) == 0 ) { \
207  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
208  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
209  };
210 
211  /*!
212  \brief Checks if value is true and logs a message if not ( the current instance warning messsage will also be updated).
213  \param value Value to be checked.
214  \param message Message to be logged.
215  */
216  #define TERP_INSTANCE_TRUE_OR_LOG( value, message ) \
217  if( ( value ) == 0 ) { \
218  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
219  TERP_LOGERR( boost::lexical_cast< std::string >( #value ) ); \
220  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
221  };
222 
223  /*!
224  \brief Checks if value is true. For false values a warning message will be logged
225  and a return of context with false value will be done.
226 
227  \param value Value to be checked.
228  \param message Message to be logged.
229  */
230  #define TERP_TRUE_OR_RETURN_FALSE( value , message ) \
231  if( ( value ) == 0 ) { \
232  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
233  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
234  return false; \
235  };
236 
237  /*!
238  \brief Checks if value is true. For false values a warning message will be logged,
239  the current instance error messsage will also be updated and
240  a return of context with false value will be done.
241 
242  \param value Value to be checked.
243  \param message Message to be logged.
244  */
245  #define TERP_INSTANCE_TRUE_OR_RETURN_FALSE( value , message ) \
246  if( ( value ) == 0 ) { \
247  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
248  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
249  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
250  return false; \
251  };
252 
253  /*!
254  \brief Checks if value is true. For false values a warning message will be logged
255  and a return of context will be done.
256 
257  \param value Value to be checked.
258  \param message Message to be logged.
259  */
260  #define TERP_TRUE_OR_RETURN( value , message ) \
261  if( ( value ) == 0 ) { \
262  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
263  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
264  return; \
265  };
266 
267  /*!
268  \brief Checks if value is false. For true values a warning message
269  will be logged and a return of context with false value will be done.
270  *
271  \param value Value to be checked.
272  \param message Message to be logged.
273  */
274  #define TERP_FALSE_OR_RETURN_FALSE( value , message ) \
275  if( ( value ) != 0 ) { \
276  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
277  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
278  return false; \
279  };
280 
281  /*!
282  \brief Checks if value is false. For true values a warning message
283  will be logged, the current instance error messsage will be updated and a return of context with false value will be done.
284  *
285  \param value Value to be checked.
286  \param message Message to be logged.
287  */
288  #define TERP_INSTANCE_FALSE_OR_RETURN_FALSE( value , message ) \
289  if( ( value ) != 0 ) { \
290  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
291  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
292  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
293  return false; \
294  };
295 
296  /*!
297  \brief Checks if value is false. For true values a warning message
298  will be logged and a return of context will be done.
299  *
300  \param value Value to be checked.
301  \param message Message to be logged.
302  */
303  #define TERP_FALSE_OR_RETURN( value , message ) \
304  if( ( value ) != 0 ) { \
305  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
306  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
307  return; \
308  };
309 
310  /*!
311  \brief Logs a warning message will and return false.
312  \param message Message to be logged.
313  */
314  #define TERP_LOG_AND_RETURN_FALSE( message ) \
315  { \
316  TERP_LOGWARN( message ); \
317  return false; \
318  };
319 
320  /*!
321  \brief Logs a warning message, update the current instance error messsage and return false.
322  \param message Message to be logged.
323  */
324  #define TERP_INSTANCE_LOG_AND_RETURN_FALSE( message ) \
325  { \
326  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
327  TERP_LOGWARN( message ); \
328  return false; \
329  };
330 
331  /*!
332  \brief Logs a warning message will and return.
333  \param message Message to be logged.
334  */
335  #define TERP_LOG_AND_RETURN( message ) \
336  { \
337  TERP_LOGWARN( message ); \
338  return; \
339  };
340 
341  /*!
342  \brief Checks if value is false and logs an warning message if not.
343  \param value Value to be checked.
344  \param message Message to be logged.
345  */
346  #define TERP_FALSE_OR_LOG( value , message ) \
347  if( ( value ) != 0 ) { \
348  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
349  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
350  };
351 
352  /*!
353  \brief Checks if two values are equal and throws an exception if not.
354  \param value1 Value to be checked.
355  \param value2 Value to be checked.
356  \param message Message to be logged.
357  */
358  #define TERP_CHECK_EQUAL( value1 , value2 , message ) \
359  if( value1 != value2 ) \
360  { \
361  TERP_LOGERR(std::string( "[" ) + \
362  boost::lexical_cast< std::string >( #value1 ) \
363  + std::string( "!=" ) + \
364  + boost::lexical_cast< std::string >( #value2 ) \
365  + "][" \
366  + boost::lexical_cast< std::string >( value1 ) \
367  + std::string( "!=" ) + \
368  + boost::lexical_cast< std::string >( value2 ) ); \
369  TERP_LOG_AND_THROW( message ); \
370  };
371 
372  /*!
373  \brief Checks if two values are diferent and throws an exception if not.
374  \param value1 Value to be checked.
375  \param value2 Value to be checked.
376  \param message Message to be logged.
377  */
378  #define TERP_CHECK_NOT_EQUAL( value1 , value2 , message ) \
379  if( value1 == value2 ) \
380  { \
381  TERP_LOGERR(std::string( "[" ) + \
382  boost::lexical_cast< std::string >( #value1 ) \
383  + std::string( "==" ) + \
384  + boost::lexical_cast< std::string >( #value2 ) \
385  + "][" \
386  + boost::lexical_cast< std::string >( value1 ) \
387  + std::string( "==" ) + \
388  + boost::lexical_cast< std::string >( value2 ) ); \
389  TERP_LOG_AND_THROW( message ); \
390  };
391 
392  /*!
393  \brief Checks if two values are equal ( within an EPS ) and throws an exception if not.
394  \param value1 Value to be checked.
395  \param value2 Value to be checked.
396  \param eps EPS ( threshold )
397  \param message Message to be logged.
398  */
399  #define TERP_CHECK_EPS( value1 , value2 , eps , message ) \
400  { \
401  TERP_TRUE_OR_THROW( ( (eps) >= 0), "Invalid eps" ); \
402  double TERP_CHECK_EPS_double_diff = 0; \
403  double TERP_CHECK_EPS_double_value1 = (double)(value1); \
404  double TERP_CHECK_EPS_double_value2 = (double)(value2); \
405  double TERP_CHECK_EPS_double_eps = (double)(eps); \
406  if( TERP_CHECK_EPS_double_value1 < TERP_CHECK_EPS_double_value2 ) { \
407  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value2 - \
408  TERP_CHECK_EPS_double_value1 ); \
409  } else { \
410  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value1 - \
411  TERP_CHECK_EPS_double_value2 ); \
412  }; \
413  if( TERP_CHECK_EPS_double_diff > TERP_CHECK_EPS_double_eps ) \
414  { \
415  TERP_LOGERR( \
416  std::string( "Values are not equal: " ) + \
417  boost::lexical_cast< std::string >( #value1 ) + \
418  std::string( "=[") + \
419  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value1 ) + \
420  std::string( "] " ) + \
421  boost::lexical_cast< std::string >( #value2 ) + \
422  std::string( "=[") + \
423  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value2 ) + \
424  std::string( "] eps=[") + \
425  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_eps ) + \
426  std::string( "] diff=[") + \
427  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_diff ) \
428  ); \
429  TERP_LOG_AND_THROW( message ); \
430  }; \
431  };
432 
433  /*!
434  \brief Throws an exception for not implemented source.
435  */
436  #define TERP_NOT_IMPLEMENTED TERP_LOG_AND_THROW( "Not Implemented." );
437 
438  /*!
439  \brief Checks if value is true and throws an exception if not.
440  \note This macro will be disabled for non debug mode.
441  \param value Value to be checked.
442  \param message Message to be logged.
443  */
444  #ifdef TERP_DEBUG_MODE
445  #define TERP_DEBUG_TRUE_OR_THROW( value , message ) \
446  TERP_TRUE_OR_THROW( value , message );
447  #else
448  #define TERP_DEBUG_TRUE_OR_THROW( value , message );
449  #endif
450 
451  /*!
452  \brief Checks if value is true. For false values a warning message will be logged
453  and a return of context with false value will be done.
454 
455  \note This macro will be disabled for non debug mode.
456  \param value Value to be checked.
457  \param message Message to be logged.
458  */
459  #ifdef TERP_DEBUG_MODE
460  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message ) \
461  TERP_TRUE_OR_RETURN_FALSE( value , message );
462  #else
463  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message );
464  #endif
465 
466  /*!
467  \brief Minimum of two values.
468  */
469  #ifndef MIN
470  #define MIN(x,y) (std::min)( x , y )
471  #endif
472 
473  /*!
474  \brief Maximum of two values.
475  */
476  #ifndef MAX
477  #define MAX(x,y) (std::max)( x , y )
478  #endif
479 
480  /*!
481  \brief Absolute value.
482  */
483  #ifndef ABS
484  #define ABS(x) std::abs( x )
485  #endif
486 
487 #endif // __TERRALIB_RP_INTERNAL_MACROS_H
488 
An exception class for the XML module.
This singleton defines the TerraLib XML module entry.