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. For false values a warning message will be logged
213  and a return of context with false value will be done.
214 
215  \param value Value to be checked.
216  \param message Message to be logged.
217  */
218  #define TERP_TRUE_OR_RETURN_FALSE( value , message ) \
219  if( ( value ) == 0 ) { \
220  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
221  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
222  return false; \
223  };
224 
225  /*!
226  \brief Checks if value is true. For false values a warning message will be logged,
227  the current instance error messsage will also be updated and
228  a return of context with false value will be done.
229 
230  \param value Value to be checked.
231  \param message Message to be logged.
232  */
233  #define TERP_INSTANCE_TRUE_OR_RETURN_FALSE( value , message ) \
234  if( ( value ) == 0 ) { \
235  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
236  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
237  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
238  return false; \
239  };
240 
241  /*!
242  \brief Checks if value is true. For false values a warning message will be logged
243  and a return of context will be done.
244 
245  \param value Value to be checked.
246  \param message Message to be logged.
247  */
248  #define TERP_TRUE_OR_RETURN( value , message ) \
249  if( ( value ) == 0 ) { \
250  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
251  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
252  return; \
253  };
254 
255  /*!
256  \brief Checks if value is false. For true values a warning message
257  will be logged and a return of context with false value will be done.
258  *
259  \param value Value to be checked.
260  \param message Message to be logged.
261  */
262  #define TERP_FALSE_OR_RETURN_FALSE( value , message ) \
263  if( ( value ) != 0 ) { \
264  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
265  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
266  return false; \
267  };
268 
269  /*!
270  \brief Checks if value is false. For true values a warning message
271  will be logged, the current instance error messsage will be updated and a return of context with false value will be done.
272  *
273  \param value Value to be checked.
274  \param message Message to be logged.
275  */
276  #define TERP_INSTANCE_FALSE_OR_RETURN_FALSE( value , message ) \
277  if( ( value ) != 0 ) { \
278  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
279  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
280  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
281  return false; \
282  };
283 
284  /*!
285  \brief Checks if value is false. For true values a warning message
286  will be logged and a return of context will be done.
287  *
288  \param value Value to be checked.
289  \param message Message to be logged.
290  */
291  #define TERP_FALSE_OR_RETURN( value , message ) \
292  if( ( value ) != 0 ) { \
293  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
294  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
295  return; \
296  };
297 
298  /*!
299  \brief Logs a warning message will and return false.
300  \param message Message to be logged.
301  */
302  #define TERP_LOG_AND_RETURN_FALSE( message ) \
303  { \
304  TERP_LOGWARN( message ); \
305  return false; \
306  };
307 
308  /*!
309  \brief Logs a warning message, update the current instance error messsage and return false.
310  \param message Message to be logged.
311  */
312  #define TERP_INSTANCE_LOG_AND_RETURN_FALSE( message ) \
313  { \
314  setErrorMessage( boost::lexical_cast< std::string >( message ) ); \
315  TERP_LOGWARN( message ); \
316  return false; \
317  };
318 
319  /*!
320  \brief Logs a warning message will and return.
321  \param message Message to be logged.
322  */
323  #define TERP_LOG_AND_RETURN( message ) \
324  { \
325  TERP_LOGWARN( message ); \
326  return; \
327  };
328 
329  /*!
330  \brief Checks if value is false and logs an warning message if not.
331  \param value Value to be checked.
332  \param message Message to be logged.
333  */
334  #define TERP_FALSE_OR_LOG( value , message ) \
335  if( ( value ) != 0 ) { \
336  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
337  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
338  };
339 
340  /*!
341  \brief Checks if two values are equal and throws an exception if not.
342  \param value1 Value to be checked.
343  \param value2 Value to be checked.
344  \param message Message to be logged.
345  */
346  #define TERP_CHECK_EQUAL( value1 , value2 , message ) \
347  if( value1 != value2 ) \
348  { \
349  TERP_LOGERR(std::string( "[" ) + \
350  boost::lexical_cast< std::string >( #value1 ) \
351  + std::string( "!=" ) + \
352  + boost::lexical_cast< std::string >( #value2 ) \
353  + "][" \
354  + boost::lexical_cast< std::string >( value1 ) \
355  + std::string( "!=" ) + \
356  + boost::lexical_cast< std::string >( value2 ) ); \
357  TERP_LOG_AND_THROW( message ); \
358  };
359 
360  /*!
361  \brief Checks if two values are diferent and throws an exception if not.
362  \param value1 Value to be checked.
363  \param value2 Value to be checked.
364  \param message Message to be logged.
365  */
366  #define TERP_CHECK_NOT_EQUAL( value1 , value2 , message ) \
367  if( value1 == value2 ) \
368  { \
369  TERP_LOGERR(std::string( "[" ) + \
370  boost::lexical_cast< std::string >( #value1 ) \
371  + std::string( "==" ) + \
372  + boost::lexical_cast< std::string >( #value2 ) \
373  + "][" \
374  + boost::lexical_cast< std::string >( value1 ) \
375  + std::string( "==" ) + \
376  + boost::lexical_cast< std::string >( value2 ) ); \
377  TERP_LOG_AND_THROW( message ); \
378  };
379 
380  /*!
381  \brief Checks if two values are equal ( within an EPS ) and throws an exception if not.
382  \param value1 Value to be checked.
383  \param value2 Value to be checked.
384  \param eps EPS ( threshold )
385  \param message Message to be logged.
386  */
387  #define TERP_CHECK_EPS( value1 , value2 , eps , message ) \
388  { \
389  TERP_TRUE_OR_THROW( ( (eps) >= 0), "Invalid eps" ); \
390  double TERP_CHECK_EPS_double_diff = 0; \
391  double TERP_CHECK_EPS_double_value1 = (double)(value1); \
392  double TERP_CHECK_EPS_double_value2 = (double)(value2); \
393  double TERP_CHECK_EPS_double_eps = (double)(eps); \
394  if( TERP_CHECK_EPS_double_value1 < TERP_CHECK_EPS_double_value2 ) { \
395  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value2 - \
396  TERP_CHECK_EPS_double_value1 ); \
397  } else { \
398  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value1 - \
399  TERP_CHECK_EPS_double_value2 ); \
400  }; \
401  if( TERP_CHECK_EPS_double_diff > TERP_CHECK_EPS_double_eps ) \
402  { \
403  TERP_LOGERR( \
404  std::string( "Values are not equal: " ) + \
405  boost::lexical_cast< std::string >( #value1 ) + \
406  std::string( "=[") + \
407  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value1 ) + \
408  std::string( "] " ) + \
409  boost::lexical_cast< std::string >( #value2 ) + \
410  std::string( "=[") + \
411  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value2 ) + \
412  std::string( "] eps=[") + \
413  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_eps ) + \
414  std::string( "] diff=[") + \
415  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_diff ) \
416  ); \
417  TERP_LOG_AND_THROW( message ); \
418  }; \
419  };
420 
421  /*!
422  \brief Throws an exception for not implemented source.
423  */
424  #define TERP_NOT_IMPLEMENTED TERP_LOG_AND_THROW( "Not Implemented." );
425 
426  /*!
427  \brief Checks if value is true and throws an exception if not.
428  \note This macro will be disabled for non debug mode.
429  \param value Value to be checked.
430  \param message Message to be logged.
431  */
432  #ifdef TERP_DEBUG_MODE
433  #define TERP_DEBUG_TRUE_OR_THROW( value , message ) \
434  TERP_TRUE_OR_THROW( value , message );
435  #else
436  #define TERP_DEBUG_TRUE_OR_THROW( value , message );
437  #endif
438 
439  /*!
440  \brief Checks if value is true. For false values a warning message will be logged
441  and a return of context with false value will be done.
442 
443  \note This macro will be disabled for non debug mode.
444  \param value Value to be checked.
445  \param message Message to be logged.
446  */
447  #ifdef TERP_DEBUG_MODE
448  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message ) \
449  TERP_TRUE_OR_RETURN_FALSE( value , message );
450  #else
451  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message );
452  #endif
453 
454  /*!
455  \brief Minimum of two values.
456  */
457  #ifndef MIN
458  #define MIN(x,y) (std::min)( x , y )
459  #endif
460 
461  /*!
462  \brief Maximum of two values.
463  */
464  #ifndef MAX
465  #define MAX(x,y) (std::max)( x , y )
466  #endif
467 
468  /*!
469  \brief Absolute value.
470  */
471  #ifndef ABS
472  #define ABS(x) std::abs( x )
473  #endif
474 
475 #endif // __TERRALIB_RP_INTERNAL_MACROS_H
476 
Module.h
This singleton defines the TerraLib XML module entry.
Exception.h
An exception class for the XML module.