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  /**! * @brief Checks if value is true and logs an warning message if not.
168  \param value Value to be checked.
169  \@param message Message to be logged.
170  */
171  #define TERP_TRUE_OR_LOG( value , message ) \
172  if( ( value ) == 0 ) { \
173  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
174  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
175  };
176 
177  /*!
178  \brief Checks if value is true. For false values a warning message will be logged
179  and a return of context with false value will be done.
180 
181  \param value Value to be checked.
182  \param message Message to be logged.
183  */
184  #define TERP_TRUE_OR_RETURN_FALSE( value , message ) \
185  if( ( value ) == 0 ) { \
186  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
187  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
188  return false; \
189  };
190 
191  /*!
192  \brief Checks if value is true. For false values a warning message will be logged
193  and a return of context will be done.
194 
195  \param value Value to be checked.
196  \param message Message to be logged.
197  */
198  #define TERP_TRUE_OR_RETURN( value , message ) \
199  if( ( value ) == 0 ) { \
200  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
201  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
202  return; \
203  };
204 
205  /*!
206  \brief Checks if value is false. For true values a warning message
207  will be logged and a return of context with false value will be done.
208  *
209  \param value Value to be checked.
210  \param message Message to be logged.
211  */
212  #define TERP_FALSE_OR_RETURN_FALSE( value , message ) \
213  if( ( value ) != 0 ) { \
214  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
215  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
216  return false; \
217  };
218 
219  /*!
220  \brief Checks if value is false. For true values a warning message
221  will be logged and a return of context will be done.
222  *
223  \param value Value to be checked.
224  \param message Message to be logged.
225  */
226  #define TERP_FALSE_OR_RETURN( value , message ) \
227  if( ( value ) != 0 ) { \
228  TERP_LOGWARN( boost::lexical_cast< std::string >( #value ) ); \
229  TERP_LOGWARN( boost::lexical_cast< std::string >( message ) ); \
230  return; \
231  };
232 
233  /*!
234  \brief Logs a warning message will and return false.
235  \param message Message to be logged.
236  */
237  #define TERP_LOG_AND_RETURN_FALSE( message ) \
238  { \
239  TERP_LOGWARN( message ); \
240  return false; \
241  };
242 
243  /*!
244  \brief Logs a warning message will and return.
245  \param message Message to be logged.
246  */
247  #define TERP_LOG_AND_RETURN( message ) \
248  { \
249  TERP_LOGWARN( message ); \
250  return; \
251  };
252 
253  /*!
254  \brief Checks if value is false and logs an warning message if not.
255  \param value Value to be checked.
256  \param message Message to be logged.
257  */
258  #define TERP_FALSE_OR_LOG( 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  };
263 
264  /*!
265  \brief Checks if two values are equal and throws an exception if not.
266  \param value1 Value to be checked.
267  \param value2 Value to be checked.
268  \param message Message to be logged.
269  */
270  #define TERP_CHECK_EQUAL( value1 , value2 , message ) \
271  if( value1 != value2 ) \
272  { \
273  TERP_LOGERR(std::string( "[" ) + \
274  boost::lexical_cast< std::string >( #value1 ) \
275  + std::string( "!=" ) + \
276  + boost::lexical_cast< std::string >( #value2 ) \
277  + "][" \
278  + boost::lexical_cast< std::string >( value1 ) \
279  + std::string( "!=" ) + \
280  + boost::lexical_cast< std::string >( value2 ) ); \
281  TERP_LOG_AND_THROW( message ); \
282  };
283 
284  /*!
285  \brief Checks if two values are diferent and throws an exception if not.
286  \param value1 Value to be checked.
287  \param value2 Value to be checked.
288  \param message Message to be logged.
289  */
290  #define TERP_CHECK_NOT_EQUAL( value1 , value2 , message ) \
291  if( value1 == value2 ) \
292  { \
293  TERP_LOGERR(std::string( "[" ) + \
294  boost::lexical_cast< std::string >( #value1 ) \
295  + std::string( "==" ) + \
296  + boost::lexical_cast< std::string >( #value2 ) \
297  + "][" \
298  + boost::lexical_cast< std::string >( value1 ) \
299  + std::string( "==" ) + \
300  + boost::lexical_cast< std::string >( value2 ) ); \
301  TERP_LOG_AND_THROW( message ); \
302  };
303 
304  /*!
305  \brief Checks if two values are equal ( within an EPS ) and throws an exception if not.
306  \param value1 Value to be checked.
307  \param value2 Value to be checked.
308  \param eps EPS ( threshold )
309  \param message Message to be logged.
310  */
311  #define TERP_CHECK_EPS( value1 , value2 , eps , message ) \
312  { \
313  TERP_TRUE_OR_THROW( ( (eps) >= 0), "Invalid eps" ); \
314  double TERP_CHECK_EPS_double_diff = 0; \
315  double TERP_CHECK_EPS_double_value1 = (double)(value1); \
316  double TERP_CHECK_EPS_double_value2 = (double)(value2); \
317  double TERP_CHECK_EPS_double_eps = (double)(eps); \
318  if( TERP_CHECK_EPS_double_value1 < TERP_CHECK_EPS_double_value2 ) { \
319  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value2 - \
320  TERP_CHECK_EPS_double_value1 ); \
321  } else { \
322  TERP_CHECK_EPS_double_diff = ( TERP_CHECK_EPS_double_value1 - \
323  TERP_CHECK_EPS_double_value2 ); \
324  }; \
325  if( TERP_CHECK_EPS_double_diff > TERP_CHECK_EPS_double_eps ) \
326  { \
327  TERP_LOGERR( \
328  std::string( "Values are not equal: " ) + \
329  boost::lexical_cast< std::string >( #value1 ) + \
330  std::string( "=[") + \
331  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value1 ) + \
332  std::string( "] " ) + \
333  boost::lexical_cast< std::string >( #value2 ) + \
334  std::string( "=[") + \
335  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_value2 ) + \
336  std::string( "] eps=[") + \
337  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_eps ) + \
338  std::string( "] diff=[") + \
339  boost::lexical_cast< std::string >( TERP_CHECK_EPS_double_diff ) \
340  ); \
341  TERP_LOG_AND_THROW( message ); \
342  }; \
343  };
344 
345  /*!
346  \brief Throws an exception for not implemented source.
347  */
348  #define TERP_NOT_IMPLEMENTED TERP_LOG_AND_THROW( "Not Implemented." );
349 
350  /*!
351  \brief Checks if value is true and throws an exception if not.
352  \note This macro will be disabled for non debug mode.
353  \param value Value to be checked.
354  \param message Message to be logged.
355  */
356  #ifdef TERP_DEBUG_MODE
357  #define TERP_DEBUG_TRUE_OR_THROW( value , message ) \
358  TERP_TRUE_OR_THROW( value , message );
359  #else
360  #define TERP_DEBUG_TRUE_OR_THROW( value , message );
361  #endif
362 
363  /*!
364  \brief Checks if value is true. For false values a warning message will be logged
365  and a return of context with false value will be done.
366 
367  \note This macro will be disabled for non debug mode.
368  \param value Value to be checked.
369  \param message Message to be logged.
370  */
371  #ifdef TERP_DEBUG_MODE
372  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message ) \
373  TERP_TRUE_OR_RETURN_FALSE( value , message );
374  #else
375  #define TERP_DEBUG_TRUE_OR_RETURN_FALSE( value , message );
376  #endif
377 
378  /*!
379  \brief Minimum of two values.
380  */
381  #ifndef MIN
382  #define MIN(x,y) std::min( x , y )
383  #endif
384 
385  /*!
386  \brief Maximum of two values.
387  */
388  #ifndef MAX
389  #define MAX(x,y) std::max( x , y )
390  #endif
391 
392  /*!
393  \brief Absolute value.
394  */
395  #ifndef ABS
396  #define ABS(x) std::abs( x )
397  #endif
398 
399 #endif // __TERRALIB_RP_INTERNAL_MACROS_H
400 
Exception class.
This singleton defines the TerraLib Raster Processing module entry.