MathTypeLibrary(libmath++) 0.0.3
|
00001 00002 // Math Type Library 00003 // $Id: library.h,v 1.6 2002/04/29 19:34:30 cparpart Exp $ 00004 // (module info here) 00005 // 00006 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net> 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library 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 GNU 00016 // Library General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Library General Public License 00019 // along with this library; see the file COPYING.LIB. If not, write to 00020 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00021 // Boston, MA 02111-1307, USA. 00023 #ifndef libmath_library_h 00024 #define libmath_library_h 00025 00026 #include <math++/error.h> 00027 00028 #include <list> 00029 #include <map> 00030 #include <string> 00031 00032 namespace math { 00033 00034 template<class> class TNode; 00035 template<class> class TLibrary; 00036 00040 template<typename T> 00041 class TFunction { 00042 private: 00043 std::string FName; 00044 TNode<T> *FExpression; 00045 00046 public: 00047 TFunction(); 00048 TFunction(const TFunction<T>&); 00049 TFunction(const std::string& AName, const std::string& AExprStr = std::string()); 00050 TFunction(const std::string& AName, const TNode<T> *AExprTree); 00051 ~TFunction(); 00052 00053 T call(const T& AParam, const TLibrary<T>& ALibrary, unsigned ALimit = 64) const; 00054 00055 void name(const std::string&); 00056 std::string name() const; 00057 00058 void expression(const TNode<T> *ACopyOf); 00059 void expression(const std::string& AExprStr); 00060 TNode<T> *expression() const; 00061 }; 00062 00066 template<typename T> 00067 class TConstant { 00068 private: 00069 std::string FName; 00070 T FValue; 00071 00072 public: 00073 TConstant(); 00074 TConstant(const TConstant<T>&); 00075 TConstant(const std::string& AName, const T& AValue = T()); 00076 00077 void name(const std::string&); 00078 std::string name() const; 00079 00080 void value(const T&); 00081 T value() const; 00082 }; 00083 00088 class ELibraryLookup : public EMath { 00089 public: 00090 ELibraryLookup(const std::string& AMsg) : EMath(AMsg) {} 00091 }; 00092 00093 00099 template<typename T> 00100 class TLibrary { 00101 private: 00102 typedef std::list<TFunction<T> > TFunctionList; 00103 typedef std::list<TConstant<T> > TConstantList; 00104 00105 TFunctionList FFunctions; 00106 TConstantList FConstants; 00107 00108 void removeIf(const std::string& AName, bool AReplaceIfExists); 00109 00110 public: 00111 TLibrary(); 00112 TLibrary(const TLibrary<T>&); 00113 00115 void insert(const TFunction<T>&, bool AReplaceIfExists = false); 00117 void insert(const TConstant<T>&, bool AReplaceIfExists = false); 00118 00120 void remove(const std::string& AName); 00121 00123 TFunction<T> function(const std::string& AName) const; 00124 00126 TConstant<T> constant(const std::string& AName) const; 00127 00129 bool hasFunction(const std::string& AName) const; 00130 00132 bool hasConstant(const std::string& AName) const; 00133 00135 T call(const std::string& AName, const T& AParam) const; 00137 T value(const std::string& AName) const; 00138 00140 unsigned functions() const; 00142 unsigned constants() const; 00143 }; 00144 00145 } // namespace math 00146 00147 template<typename T> std::ostream& operator<<(std::ostream&, const math::TFunction<T>&); 00148 00149 #include <math++/library.tcc> 00150 00151 #endif