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 "../common/Logger.h"
30  #include "../common/Config.h"
31  #include "Module.h"
32  #include "Exception.h"
33 
34  #include <boost/lexical_cast.hpp>
35 
36  #include <sstream>
37  #include <string>
38  #include <iostream>
39 
40  /*!
41  \brief Debug mode selection based on NDEBUG define.
42  */
43  #ifndef TERP_DEBUG_MODE
44  #ifndef NDEBUG
45  /*! \brief Debug mode selection flag. */
46  #define TERP_DEBUG_MODE
47 
48  /*! \brief STDOUT logging selection flag. */
49  #ifndef TERP_ENABLE_STDOUT_LOG
50  #define TERP_ENABLE_STDOUT_LOG
51  #endif
52  #endif
53  #endif
54 
55  /*!
56  \brief Logs a message to stdout
57  \param message Message to be logged.
58  */
59  #ifdef TERP_ENABLE_STDOUT_LOG
60  #define TERP_LOGMSG_STDOUT( message ) \
61  std::cout << std::endl << "Message : " \
62  << __FILE__ \
63  << ":" << __LINE__ \
64  << " - " << boost::lexical_cast< std::string >( message ) \
65  << std::endl;
66  #else
67  #define TERP_LOGMSG_STDOUT( message )
68  #endif
69 
70  /*!
71  \brief Logs a error message to stderr
72  \param message Message to be logged.
73  */
74  #ifdef TERP_ENABLE_STDOUT_LOG
75  #define TERP_LOGERR_STDOUT( message ) \
76  std::cerr << std::endl << "Error : " \
77  << __FILE__ \
78  << ":" << __LINE__ \
79  << " - " << boost::lexical_cast< std::string >( message ) \
80  << std::endl;
81  #else
82  #define TERP_LOGERR_STDOUT( message )
83  #endif
84 
85  /*!
86  \brief Logs a warning message to stdout
87  \param message Message to be logged.
88  */
89  #ifdef TERP_ENABLE_STDOUT_LOG
90  #define TERP_LOGWARN_STDOUT( message ) \
91  std::cout << std::endl << "Warning : " \
92  << __FILE__ \
93  << ":" << __LINE__ \
94  << " - " << boost::lexical_cast< std::string >( message ) \
95  << std::endl;
96  #else
97  #define TERP_LOGWARN_STDOUT( message )
98  #endif
99 
100 
101  /*!
102  \brief Logs a message.
103  \param message Message to be logged.
104  */
105  #define TERP_LOGMSG( message ) \
106  { \
107  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
108  TE_LOG_INFO( boost::lexical_cast< std::string >( message ).c_str() ); \
109  TERP_LOGMSG_STDOUT( message ); \
110  };
111 
112  /*!
113  \brief Logs an error message.
114  \param message Message to be logged.
115  */
116  #define TERP_LOGERR( message ) \
117  { \
118  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
119  TE_LOG_ERROR( boost::lexical_cast< std::string >( message ).c_str() ); \
120  TERP_LOGERR_STDOUT( message ); \
121  };
122 
123  /*!
124  \brief Logs a warning message.
125  \param message Message to be logged.
126  */
127  #define TERP_LOGWARN( message ) \
128  { \
129  te::rp::Module::setLastLogStr( boost::lexical_cast< std::string >( message ) ); \
130  TE_LOG_WARN( boost::lexical_cast< std::string >( message ).c_str() ); \
131  TERP_LOGWARN_STDOUT( message ); \
132  };
133 
134  /*!
135  \brief Logs a error message and throws.
136  \param message Message to be logged.
137  */
138  #define TERP_LOG_AND_THROW( message ) \
139  { \
140  TERP_LOGERR( message ); \
141  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
142  };
143 
144  /*!
145  \brief Checks if value is true and throws an exception if not.
146  \param value Value to be checked.
147  \param message Message to be logged.
148  */
149  #define TERP_TRUE_OR_THROW( value , message ) \
150  if( ( value ) == 0 ) { \
151  TERP_LOGERR( boost::lexical_cast< std::string >( #value ) ); \
152  TERP_LOGERR( boost::lexical_cast< std::string >( message ) ); \
153  throw te::rp::Exception( boost::lexical_cast< std::string >( message ) ); \
154  };
155 
156  /*!
157  \brief Variable watching.
158  \param variable Variable to be logged.
159  */
160  #define TERP_WATCH( variable ) \
161  { \
162  TERP_LOGMSG( "WATCH - " + boost::lexical_cast< std::string >( #variable ) + \
163  "=[" + boost::lexical_cast< std::string >( variable ) +"]" ); \
164  };
165 
166  /**! * @brief Checks if value is true and logs an warning message if not.
167  \param value Value to be checked.
168  \@param message Message to be logged.
169  */
170  #define TERP_TRUE_OR_LOG( value , message ) \
171  if( ( value ) == 0 ) { \
172  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
173  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
174  };
175 
176  /*!
177  \brief Checks if value is true. For false values a warning message will be logged
178  and a return of context with false value will be done.
179 
180  \param value Value to be checked.
181  \param message Message to be logged.
182  */
183  #define TERP_TRUE_OR_RETURN_FALSE( value , message ) \
184  if( ( value ) == 0 ) { \
185  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
186  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
187  return false; \
188  };
189 
190  /*!
191  \brief Checks if value is true. For false values a warning message will be logged
192  and a return of context will be done.
193 
194  \param value Value to be checked.
195  \param message Message to be logged.
196  */
197  #define TERP_TRUE_OR_RETURN( value , message ) \
198  if( ( value ) == 0 ) { \
199  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
200  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
201  return; \
202  };
203 
204  /*!
205  \brief Checks if value is false. For true values a warning message
206  will be logged and a return of context with false value will be done.
207  *
208  \param value Value to be checked.
209  \param message Message to be logged.
210  */
211  #define TERP_FALSE_OR_RETURN_FALSE( value , message ) \
212  if( ( value ) != 0 ) { \
213  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
214  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
215  return false; \
216  };
217 
218  /*!
219  \brief Checks if value is false. For true values a warning message
220  will be logged and a return of context will be done.
221  *
222  \param value Value to be checked.
223  \param message Message to be logged.
224  */
225  #define TERP_FALSE_OR_RETURN( value , message ) \
226  if( ( value ) != 0 ) { \
227  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
228  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
229  return; \
230  };
231 
232  /*!
233  \brief Logs a warning message will and return false.
234  \param message Message to be logged.
235  */
236  #define TERP_LOG_AND_RETURN_FALSE( message ) \
237  { \
238  TERP_LOGWARN( message ); \
239  return false; \
240  };
241 
242  /*!
243  \brief Logs a warning message will and return.
244  \param message Message to be logged.
245  */
246  #define TERP_LOG_AND_RETURN( message ) \
247  { \
248  TERP_LOGWARN( message ); \
249  return; \
250  };
251 
252  /*!
253  \brief Checks if value is false and logs an warning message if not.
254  \param value Value to be checked.
255  \param message Message to be logged.
256  */
257  #define TERP_FALSE_OR_LOG( value , message ) \
258  if( ( value ) != 0 ) { \
259  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
260  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
261  };
262 
263  /*!
264  \brief Checks if two values are equal and throws an exception if not.
265  \param value1 Value to be checked.
266  \param value2 Value to be checked.
267  \param message Message to be logged.
268  */
269  #define TERP_CHECK_EQUAL( value1 , value2 , message ) \
270  if( value1 != value2 ) \
271  { \
272  TERP_LOGERR(std::string( "[" ) + \
273  boost::lexical_cast< std::string >( #value1 ) \
274  + std::string( "!=" ) + \
275  + boost::lexical_cast< std::string >( #value2 ) \
276  + "][" \
277  + boost::lexical_cast< std::string >( value1 ) \
278  + std::string( "!=" ) + \
279  + boost::lexical_cast< std::string >( value2 ) ); \
280  TERP_LOG_AND_THROW( message ); \
281  };
282 
283  /*!
284  \brief Checks if two values are diferent and throws an exception if not.
285  \param value1 Value to be checked.
286  \param value2 Value to be checked.
287  \param message Message to be logged.
288  */
289  #define TERP_CHECK_NOT_EQUAL( value1 , value2 , message ) \
290  if( value1 == value2 ) \
291  { \
292  TERP_LOGERR(std::string( "[" ) + \
293  boost::lexical_cast< std::string >( #value1 ) \
294  + std::string( "==" ) + \
295  + boost::lexical_cast< std::string >( #value2 ) \
296  + "][" \
297  + boost::lexical_cast< std::string >( value1 ) \
298  + std::string( "==" ) + \
299  + boost::lexical_cast< std::string >( value2 ) ); \
300  TERP_LOG_AND_THROW( message ); \
301  };
302 
303  /*!
304  \brief Checks if two values are equal ( within an EPS ) and throws an exception if not.
305  \param value1 Value to be checked.
306  \param value2 Value to be checked.
307  \param eps EPS ( threshold )
308  \param message Message to be logged.
309  */
310  #define TERP_CHECK_EPS( value1 , value2 , eps , message ) \
311  { \
312  TERP_TRUE_OR_THROW( ( (eps) >= 0), "Invalid eps" ); \
313  double TERP_CHECK_EPS_double_diff = 0; \
314  double TERP_CHECK_EPS_double_value1 = (double)(value1); \
315  double TERP_CHECK_EPS_double_value2 = (double)(value2); \
316  double TERP_CHECK_EPS_double_eps = (double)(eps); \
317  if( TERP_CHECK_EPS_double_value1 < TERP_CHECK_EPS_double_value2 ) { \
318  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value2 - \
319  TERP_CHECK_EPS_double_value1 ); \
320  } else { \
321  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value1 - \
322  TERP_CHECK_EPS_double_value2 ); \
323  }; \
324  if( TERP_CHECK_EPS_double_diff > TERP_CHECK_EPS_double_eps ) \
325  { \
326  TERP_LOGERR( \
327  std::string( "Values are not equal: " ) + \
328  boost::lexical_cast< std::string >( #value1 ) + \
329  std::string( "=[") + \
330  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value1 ) + \
331  std::string( "] " ) + \
332  boost::lexical_cast< std::string >( #value2 ) + \
333  std::string( "=[") + \
334  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value2 ) + \
335  std::string( "] eps=[") + \
336  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_eps ) + \
337  std::string( "] diff=[") + \
338  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_diff ) \
339  ); \
340  TERP_LOG_AND_THROW( message ); \
341  }; \
342  };
343 
344  /*!
345  \brief Throws an exception for not implemented source.
346  */
347  #define TERP_NOT_IMPLEMENTED TERP_LOG_AND_THROW( "Not Implemented." );
348 
349  /*!
350  \brief Checks if value is true and throws an exception if not.
351  \note This macro will be disabled for non debug mode.
352  \param value Value to be checked.
353  \param message Message to be logged.
354  */
355  #ifdef TERP_DEBUG_MODE
356  #define TERP_DEBUG_TRUE_OR_THROW( value , message ) \
357  TERP_TRUE_OR_THROW( value , message );
358  #else
359  #define TERP_DEBUG_TRUE_OR_THROW( value , message );
360  #endif
361 
362  /*!
363  \brief Checks if value is true. For false values a warning message will be logged
364  and a return of context with false value will be done.
365 
366  \note This macro will be disabled for non debug mode.
367  \param value Value to be checked.
368  \param message Message to be logged.
369  */
370  #ifdef TERP_DEBUG_MODE
371  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message ) \
372  TERP_TRUE_OR_RETURN_FALSE( value , message );
373  #else
374  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message );
375  #endif
376 
377  /*!
378  \brief Minimum of two values.
379  */
380  #ifndef MIN
381  #define MIN(x,y) std::min( x , y )
382  #endif
383 
384  /*!
385  \brief Maximum of two values.
386  */
387  #ifndef MAX
388  #define MAX(x,y) std::max( x , y )
389  #endif
390 
391  /*!
392  \brief Minimum of two values.
393  */
394  #ifndef ROUND
395  #define ROUND(x) ( ( (x) >= 0 ) ? (long int)( (x) + 0.5 ) : (long int)( (x) - 0.5 ) )
396  #endif
397 
398  /*!
399  \brief Absolute value.
400  */
401  #ifndef ABS
402  #define ABS(x) std::abs( x )
403  #endif
404 
405 #endif // __TERRALIB_RP_INTERNAL_MACROS_H
406 
Exception class.
This singleton defines the TerraLib Raster Processing module entry.