templates.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // -*- C++ -*-
00031 //
00032 // -----------------------------------------------------------------------
00033 // This file should define some platform dependent features and some
00034 // useful utilities.
00035 // -----------------------------------------------------------------------
00036 
00037 // =======================================================================
00038 // Gabriele Cosmo - Created: 5th September 1995
00039 // Gabriele Cosmo - Minor change: 08/02/1996
00040 // Gabriele Cosmo - Added DBL_MIN, FLT_MIN, DBL_DIG,
00041 //                        DBL_MAX, FLT_DIG, FLT_MAX  : 12/04/1996
00042 // Gabriele Cosmo - Removed boolean enum definition : 29/11/1996
00043 // Gunter Folger  - Added G4SwapPtr() and G4SwapObj() : 31/07/1997
00044 // Gabriele Cosmo - Adapted signatures of min(), max() to
00045 //                  STL's ones, thanks to E.Tcherniaev : 31/07/1997
00046 // Gabriele Cosmo,
00047 // Evgueni Tcherniaev - Migrated to CLHEP: 04/12/1997 
00048 // =======================================================================
00049 
00050 #ifndef templates_h
00051 #define templates_h 1
00052 
00053 #include <limits>
00054 #include <climits>
00055 
00056 //
00057 // If HIGH_PRECISION is defined to TRUE (ie. != 0) then the type "Float"
00058 // is typedefed to "double". If it is FALSE (ie. 0) it is typedefed
00059 // to "float".
00060 //
00061 #ifndef HIGH_PRECISION
00062 #define HIGH_PRECISION 1
00063 #endif
00064 
00065 #if HIGH_PRECISION
00066 typedef double Float;
00067 #else
00068 typedef float Float;
00069 #endif
00070 
00071 // Following values have been taken from limits.h
00072 // and temporarly defined for portability on HP-UX.
00073 
00074 #ifndef DBL_MIN   /* Min decimal value of a double */
00075 #define DBL_MIN   std::numeric_limits<double>::min()  // 2.2250738585072014e-308
00076 #endif
00077 
00078 #ifndef DBL_DIG   /* Digits of precision of a double */
00079 #define DBL_DIG   std::numeric_limits<double>::digits10   // 15
00080 #endif
00081 
00082 #ifndef DBL_MAX   /* Max decimal value of a double */
00083 #define DBL_MAX   std::numeric_limits<double>::max()  // 1.7976931348623157e+308
00084 #endif
00085 
00086 #ifndef DBL_EPSILON
00087 #define DBL_EPSILON std::numeric_limits<double>::epsilon()
00088 #endif                                                // 2.2204460492503131e-16
00089 
00090 #ifndef FLT_MIN   /* Min decimal value of a float */
00091 #define FLT_MIN   std::numeric_limits<float>::min()   // 1.17549435e-38F
00092 #endif
00093 
00094 #ifndef FLT_DIG   /* Digits of precision of a float */
00095 #define FLT_DIG   std::numeric_limits<float>::digits10     // 6
00096 #endif
00097 
00098 #ifndef FLT_MAX   /* Max decimal value of a float */
00099 #define FLT_MAX   std::numeric_limits<float>::max()   // 3.40282347e+38F
00100 #endif
00101 
00102 #ifndef FLT_EPSILON
00103 #define FLT_EPSILON std::numeric_limits<float>::epsilon()
00104 #endif                                                // 1.192092896e-07F
00105 
00106 #ifndef MAXFLOAT   /* Max decimal value of a float */
00107 #define MAXFLOAT  std::numeric_limits<float>::max()   // 3.40282347e+38F
00108 #endif
00109 
00110 #ifndef INT_MAX   /* Max decimal value of a int */
00111 #define INT_MAX   std::numeric_limits<int>::max()   // 2147483647
00112 #endif
00113 
00114 #ifndef INT_MIN   /* Min decimal value of a int */
00115 #define INT_MIN   std::numeric_limits<int>::min()   // -2147483648
00116 #endif
00117 
00118 //---------------------------------
00119 
00120 template <class T>
00121 inline void G4SwapPtr(T*& a, T*& b)
00122 {
00123   T* tmp= a;
00124   a = b;
00125   b = tmp;
00126 }
00127 
00128 template <class T>
00129 inline void G4SwapObj(T* a, T* b)
00130 {
00131   T tmp= *a;
00132   *a = *b;
00133   *b = tmp;
00134 }
00135 
00136 //-----------------------------
00137 
00138 #ifndef G4_SQR_DEFINED
00139   #define G4_SQR_DEFINED
00140   #ifdef sqr
00141     #undef sqr
00142   #endif
00143 
00144 template <class T>
00145 inline T sqr(const T& x)
00146 {
00147   return x*x;
00148 }
00149 #endif
00150 
00151 #ifdef G4_ABS_DEFINED
00152   #ifdef abs
00153     #undef abs
00154   #endif
00155 
00156 template <class T>
00157 inline T std::abs(const T& a)
00158 {
00159   return a < 0 ? -a : a;
00160 }
00161 #endif
00162 
00163 inline int G4lrint(double ad)
00164 {
00165   return (ad>0) ? static_cast<int>(ad+.5) : static_cast<int>(ad-.5);
00166 }
00167 
00168 inline int G4lint(double ad)
00169 {
00170   return (ad>0) ? static_cast<int>(ad) : static_cast<int>(ad-1.);
00171 }
00172 
00173 inline int G4rint(double ad)
00174 {
00175   return (ad>0) ? static_cast<int>(ad+1) : static_cast<int>(ad);
00176 }
00177 
00178 #endif // templates_h

Generated on Mon May 27 17:50:35 2013 for Geant4 by  doxygen 1.4.7