CoCoALib-0.9905 date: 23 May 2007


convert.H

Go to the documentation of this file.
00001 #ifndef CoCoA_convert_H
00002 #define CoCoA_convert_H
00003 
00004 //   Copyright (c)  2007  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib is distributed in the hope that it will be useful,
00014 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //   GNU General Public License for more details.
00017 
00018 //   You should have received a copy of the GNU General Public License
00019 //   along with CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 //   -------------------------------------------------------------------------
00022 
00023 #include "CoCoA/error.H"
00024 
00025 #include <string>
00026 // using std::string;
00027 
00028 namespace CoCoA
00029 {
00030 
00031   class ZZ; // fwd decl, defined in ZZ.H
00032 
00033   bool convert(long& lhs, const ZZ& src);
00034   bool convert(int& lhs, const ZZ& src);
00035   bool convert(unsigned long& lhs, const ZZ& src);
00036   bool convert(unsigned int& lhs, const ZZ& src);
00037   bool convert(std::string& lhs, const ZZ& src, int base=10);
00038   bool convert(ZZ& lhs, const std::string& src, int base=0);
00039 
00040   bool convert(ZZ& num, ZZ& den, double z);   ///< convert double z into an exact rational ==> den will be a power of 2.
00041   bool convert(double& z, const ZZ& N);                   ///< conversion may discard information!
00042   bool convert(double& z, const ZZ& num, const ZZ& den);  ///< conversion may discard information!
00043 
00044 
00045 
00046   // Convert various sorts of unsigned integral value into a signed long;
00047   // result is true iff faithful conversion is possible.
00048   // All of these functions employ a static_cast to do the real work.
00049   // One of these functions will also convert a std::size_t to a long.
00050   inline bool convert(long& dest, const unsigned long& src)
00051   { dest = static_cast<long>(src); return (dest >= 0); }
00052 
00053   inline bool convert(long& dest, const unsigned int& src)
00054   { dest = static_cast<long>(src); return (dest >= 0); }
00055 
00056   inline bool convert(long& dest, const unsigned short& src)
00057   { dest = static_cast<long>(src); return true; } // always succeeds
00058 
00059   inline bool convert(long& dest, const unsigned char& src)
00060   { dest = static_cast<long>(src); return true; } // always succeeds
00061 
00062 
00063   // Write it this way to allow user to specify just one of the template args when calling.
00064   template<typename T>
00065   class CheckedCast
00066   {
00067   public:
00068     template<typename T2>
00069     CheckedCast(const T2& val, const char* const FnName) { if (!convert(myVal, val)) CoCoA_ERROR(ERR::BadCheckedCast, FnName); }
00070     operator T() const { return myVal; }
00071   private:
00072     CheckedCast(const CheckedCast<T>&);                // NEVER DEFINED
00073     CheckedCast<T>& operator=(const CheckedCast<T>&);  // NEVER DEFINED
00074   private:
00075     T myVal;
00076   };
00077 
00078 
00079 } // end of namespace CoCoA
00080 
00081 
00082 
00083 // RCS header/log in the next few lines
00084 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/convert.H,v 1.1 2007/03/23 18:38:43 abbott Exp $
00085 // $Log: convert.H,v $
00086 // Revision 1.1  2007/03/23 18:38:43  abbott
00087 // Separated the "convert" functions (and CheckedCast) into their own files.
00088 // Many consequential changes.  Also corrected conversion to doubles.
00089 //
00090 //
00091 
00092 #endif

Generated on Wed May 23 13:43:34 2007 for CoCoALib by  doxygen 1.4.6