Base/Tools.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef BASE_TOOLS_H
00025 #define BASE_TOOLS_H
00026 
00027 #include <functional>
00028 #include <algorithm>
00029 #include <cmath>
00030 #include <iostream>
00031 #include <vector>
00032 #include <string>
00033 
00034 namespace Base
00035 {
00036 
00037 template <class T>
00038 struct iotaGen
00039 {
00040 public:
00041     T operator()() { return n++; }
00042     iotaGen(T v) : n(v) {}
00043 
00044 private:
00045     T n;
00046 };
00047 
00048 // ----------------------------------------------------------------------------
00049 
00050 template <class T>
00051 class manipulator
00052 {
00053     T i_;
00054     std::ostream& (*f_)(std::ostream&, T);
00055 
00056 public:
00057     manipulator(std::ostream& (*f)(std::ostream&, T), T i) : i_(i), f_(f)
00058     {
00059     }
00060     friend std::ostream& operator<<( std::ostream& os, manipulator m)
00061     {
00062         return m.f_(os, m.i_);
00063     }
00064 };
00065 
00066 inline std::ostream& tabsN(std::ostream& os, int n)
00067 {
00068     for (int i=0;i<n;i++)
00069         os << "\t";
00070     return os;
00071 }
00072 
00073 inline std::ostream& blanksN(std::ostream& os, int n)
00074 {
00075     for (int i=0;i<n;i++)
00076         os << " ";
00077     return os;
00078 }
00079 
00080 inline manipulator<int> tabs(int n)
00081 {
00082     return manipulator<int>(&tabsN, n);
00083 }
00084 
00085 inline manipulator<int> blanks(int n)
00086 {
00087     return manipulator<int>(&blanksN, n);
00088 }
00089 
00090 // ----------------------------------------------------------------------------
00091 
00092 template<class T>
00093 inline T clamp (T num, T lower, T upper)
00094 {
00095     return std::max<T>(std::min<T>(upper,num),lower);
00096 }
00097 
00098 template<class T>
00099 inline T sgn (T t)
00100 {
00101     if (t == 0)
00102         return T(0);
00103     else
00104         return (t > 0) ? T(1) : T(-1);
00105 }
00106 
00107 #ifndef M_PI
00108 #define M_PI       3.14159265358979323846
00109 #endif
00110 
00111 template<class T>
00112 inline T toRadians(T d)
00113 {
00114     return static_cast<T>((d*M_PI)/180.0);
00115 }
00116 
00117 template<class T>
00118 inline T toDegrees(T r)
00119 {
00120     return static_cast<T>((r/M_PI)*180.0);
00121 }
00122 
00123 template<class T>
00124 inline T fmod(T numerator, T denominator)
00125 {
00126     T modulo = std::fmod(numerator, denominator);
00127     return (modulo >= T(0)) ? modulo : modulo + denominator;
00128 }
00129 
00130 // ----------------------------------------------------------------------------
00131 
00132 class BaseExport StopWatch
00133 {
00134 public:
00135     StopWatch();
00136     ~StopWatch();
00137 
00138     void start();
00139     int elapsed();
00140     std::string toString(int ms) const;
00141 
00142 private:
00143     struct Private;
00144     Private* d;
00145 };
00146 
00147 // ----------------------------------------------------------------------------
00148 
00149 struct BaseExport Tools
00150 {
00151     static std::string getUniqueName(const std::string&, const std::vector<std::string>&,int d=0);
00152     static std::string addNumber(const std::string&, unsigned int, int d=0);
00153     static std::string getIdentifier(const std::string&);
00154 };
00155 
00156 } // namespace Base
00157 
00158 #endif // BASE_TOOLS_H

Generated on Wed Nov 23 19:00:45 2011 for FreeCAD by  doxygen 1.6.1