HyperspaceExplorer 0.7.1
Util.h
00001 /*
00002 Hyperspace Explorer - visualizing higher-dimensional geometry
00003 Copyright (C) 2010  Lene Preuss <lene.preuss@gmail.com>
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License along
00016 with this program; if not, write to the Free Software Foundation, Inc.,
00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 */
00020 
00021 #ifndef UTIL_H
00022 #define UTIL_H
00023 
00024 #include <iostream>
00025 #include <vector>
00026 
00027 namespace VecMath {
00028   template <unsigned D, typename N> class Vector;
00029 }
00030 class QString;
00031 
00036 #define stringify(X) #X
00037 #define make_str(X) stringify(X)
00038 
00042 //#ifndef __PRETTY_FUNCTION__
00043 //#   define __PRETTY_FUNCTION__ (std::string(__FILE__)+std::string(" line //")+Globals::Instance().itoa(__LINE__)).c_str()
00044 //#endif
00045 
00047 namespace Util {
00048 
00050 
00055   template <typename T> typename std::vector<T>::iterator find(T find_me, std::vector<T> &v) {
00056     typename std::vector<T>::iterator i = v.begin();
00057     while(i != v.end() && *i != find_me) ++i;
00058     return i;
00059   }
00060 
00061   int GetGLList();
00062 
00063   void CheckGLErrors (const char *op = 0);
00064 
00067   void glVertex (const VecMath::Vector<3, double> &V);
00068 
00069   VecMath::Vector<3, double> vnormalize (double xx, double yy, double zz);
00070 
00071   std::string itoa (int x);
00073 
00074   double atod (QString);
00076 
00077   double atod (const std::string &);
00079 
00080   int atoi(const std::string &);
00082 
00083   unsigned atou(const std::string &);
00084 
00085   std::string ftoa (double x);
00086 
00088   std::string sup2();
00089 
00091   std::string sup3();
00092 
00094   template<typename T>
00095   bool isPermutation(T m0, T m1,
00096                      T n0, T n1) {
00097     if (m0 == n0 && m1 == n1) return true;
00098     if (m0 == n1 && m1 == n0) return true;
00099     return false;
00100   }
00101 
00103   template<typename T>
00104   bool isPermutation(T m0, T m1, T m2,
00105                      T n0, T n1, T n2) {
00106     if (m0 == n0 && isPermutation(m1, m2, n1, n2)) return true;
00107     if (m0 == n1 && isPermutation(m1, m2, n0, n2)) return true;
00108     if (m0 == n2 && isPermutation(m1, m2, n0, n1)) return true;
00109     return false;
00110   }
00111 
00113   template<typename T>
00114   bool isPermutation(T m0, T m1, T m2, T m3,
00115                      T n0, T n1, T n2, T n3) {
00116     if (m0 == n0 && isPermutation(m1, m2, m3, n1, n2, n3)) return true;
00117     if (m0 == n1 && isPermutation(m1, m2, m3, n0, n2, n3)) return true;
00118     if (m0 == n2 && isPermutation(m1, m2, m3, n0, n1, n3)) return true;
00119     if (m0 == n3 && isPermutation(m1, m2, m3, n0, n1, n2)) return true;
00120     return false;
00121   }
00122 
00124   template<typename T>
00125   bool isPermutation(T m0, T m1, T m2, T m3, T m4,
00126                      T n0, T n1, T n2, T n3, T n4) {
00127     if (m0 == n0 && isPermutation(m1, m2, m3, m4, n1, n2, n3, n4)) return true;
00128     if (m0 == n1 && isPermutation(m1, m2, m3, m4, n0, n2, n3, n4)) return true;
00129     if (m0 == n2 && isPermutation(m1, m2, m3, m4, n0, n1, n3, n4)) return true;
00130     if (m0 == n3 && isPermutation(m1, m2, m3, m4, n0, n1, n2, n4)) return true;
00131     if (m0 == n4 && isPermutation(m1, m2, m3, m4, n0, n1, n2, n3)) return true;
00132     return false;
00133   }
00134   
00135   /*
00136    * unfinished attempts at rewriting isPermutation() as a variadic template
00137    * using std::tr1::tuple follow.
00138    * needs the g++ option
00139    * QMAKE_CXXFLAGS += -std=c++0x
00140    * to compile.
00141 
00142     // attempt 1. function template.
00143     // failed because i could not find the right syntax to even learn the number
00144     // of elements in the tuple.
00145     bool isPermutation(tuple<members...> list1,
00146                        tuple<members...> list2) {
00147       if (list1 == list2) return true;
00148       if (tuple_size< list1 >::value != tuple_size< list2 >::value) {
00149         return false;
00150       }
00151       ...
00152 
00153       return false;
00154     }
00155 
00156     // attempt 2. use a class with variadic templates and a static member.
00157     // failed at the point of g++ error message:
00158     // sorry, unimplemented: cannot expand 'Args ...' into a fixed-length argument list
00159     template<typename T> struct IsPermutation<> { };
00160     template<typename T, T Arg1, T... Args>
00161     struct IsPermutation {
00162       static const bool value = IsPermutation<T, Args...>::value;
00163     };
00164 */
00165 
00167 
00170   std::string trim(const std::string &str);
00171 
00173 
00178   std::vector<std::string> explode(const std::string &delimiter, const std::string &str);
00179   
00180   bool isMultithreadedSensible();
00181   
00182 }
00183 
00184 #endif // UTIL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends

Generated on Mon Apr 9 2012 20:25:16 for HyperspaceExplorer 0.7.1 by doxygen 1.7.4  -  Hosted bySourceForge.net Logo