basix_doc 0.1
|
00001 00002 /****************************************************************************** 00003 * MODULE : symbol.hpp 00004 * DESCRIPTION: A symbol points to another object but is stored only once 00005 * unless explicitely specified 00006 * COPYRIGHT : (C) 2004 Joris van der Hoeven 00007 ******************************************************************************* 00008 * This software falls under the GNU general public license and comes WITHOUT 00009 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00010 * If you don't have this file, write to the Free Software Foundation, Inc., 00011 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00012 ******************************************************************************/ 00013 00014 #ifndef __MMX_SYMBOL_HPP 00015 #define __MMX_SYMBOL_HPP 00016 #include <basix/table.hpp> 00017 00019 00020 namespace mmx { 00021 #define TMPL_DEF template<typename C, typename V= exact_eq_table> 00022 #define TMPL template<typename C, typename V> 00023 #define Symbol symbol<C,V> 00024 #define Symbol_rep symbol_rep<C,V> 00025 TMPL class symbol_rep; 00026 TMPL class symbol; 00027 TMPL inline nat hash (const Symbol& s); 00028 TMPL inline bool operator == (const Symbol& s1, const Symbol& s2); 00029 TMPL inline bool operator != (const Symbol& s1, const Symbol& s2); 00030 TMPL inline nat exact_hash (const Symbol& s); 00031 TMPL inline bool exact_eq (const Symbol& s1, const Symbol& s2); 00032 TMPL inline bool exact_neq (const Symbol& s1, const Symbol& s2); 00033 00034 /****************************************************************************** 00035 * Symbol class and its representation class 00036 ******************************************************************************/ 00037 00038 TMPL_DEF 00039 class symbol_rep REP_STRUCT { 00040 private: 00041 typedef table<Symbol_rep*,C,V> table_type; 00042 00043 static inline table_type& ptr () { 00044 typedef table<Symbol_rep*,C,V> table_type; 00045 static table_type* t= mmx_new_one<table_type> (); 00046 // FIXME: this table is never destroyed explicitely. 00047 // This solution avoids segfaults due to corrupted order of 00048 // destruction of static variables on exit. 00049 return *t; } 00050 static inline table_type all_entries () { 00051 return copy (ptr ()); } 00052 static inline Symbol_rep* get_entry (const C& ref) { 00053 return (inside (ptr ())) -> get (ref); } 00054 static inline void set_entry (const C& ref, Symbol_rep* val) { 00055 (inside (ptr ())) -> set (ref)= val; } 00056 static inline void reset_entry (const C& ref) { 00057 (inside (ptr ())) -> reset (ref); } 00058 00059 private: 00060 C ref; 00061 00062 public: 00063 inline Symbol_rep (const C& ref2): ref (ref2) { 00064 set_entry (ref, this); } 00065 inline ~Symbol_rep () { 00066 reset_entry (ref); } 00067 friend class Symbol; 00068 static void show_symbol_entries () { 00069 mmout << "symbols\t= " << all_entries () << "\n"; } 00070 }; 00071 00072 TMPL_DEF 00073 class symbol { 00074 INDIRECT_PROTO_2 (symbol, symbol_rep, C, V) 00075 public: 00076 symbol (const C& ref= C()) { 00077 rep= Symbol_rep::get_entry (ref); 00078 if (rep == (Symbol_rep*) NULL) rep= new Symbol_rep (ref); 00079 else INC_COUNT (rep); } 00080 inline const C& operator * () const { return rep->ref; }; 00081 friend nat hash LESSGTR (const Symbol& s); 00082 friend bool operator == LESSGTR (const Symbol& s1, const Symbol& s2); 00083 friend bool operator != LESSGTR (const Symbol& s1, const Symbol& s2); 00084 friend nat exact_hash LESSGTR (const Symbol& s); 00085 friend bool exact_eq LESSGTR (const Symbol& s1, const Symbol& s2); 00086 friend bool exact_neq LESSGTR (const Symbol& s1, const Symbol& s2); 00087 }; 00088 INDIRECT_IMPL_2 (symbol, symbol_rep, typename C, C, typename V, V) 00089 00090 template<> struct symbolic_type_information<symbol<string> > { 00091 static const nat id= SYMBOLIC_LITERAL; }; 00092 template<typename C> struct symbolic_type_information<symbol<vector<C> > > { 00093 static const nat id= SYMBOLIC_COMPOUND; }; 00094 00095 /****************************************************************************** 00096 * Routines related to equality testing 00097 ******************************************************************************/ 00098 00099 TMPL inline nat hash (const Symbol& s) { 00100 return hash (*s); } 00101 TMPL inline bool operator == (const Symbol& s1, const Symbol& s2) { 00102 return (*s1) == (*s2); } 00103 TMPL inline bool operator != (const Symbol& s1, const Symbol& s2) { 00104 return (*s1) != (*s2); } 00105 00106 TMPL inline nat exact_hash (const Symbol& s) { 00107 return as_hash (s.rep); } 00108 TMPL inline bool exact_eq (const Symbol& s1, const Symbol& s2) { 00109 return s1.rep == s2.rep; } 00110 TMPL inline bool exact_neq (const Symbol& s1, const Symbol& s2) { 00111 return s1.rep != s2.rep; } 00112 00113 template<typename C> inline nat hash (const symbol<C,equal_table>& s) { 00114 return exact_hash (s); } 00115 template<typename C> inline bool 00116 operator == (const symbol<C,equal_table>& s1, 00117 const symbol<C,equal_table>& s2) { 00118 return exact_eq (s1, s2); } 00119 template<typename C> inline bool 00120 operator != (const symbol<C,equal_table>& s1, 00121 const symbol<C,equal_table>& s2) { 00122 return exact_neq (s1, s2); } 00123 00124 /****************************************************************************** 00125 * Printing symbols 00126 ******************************************************************************/ 00127 00128 TMPL void show_symbol_entries () { 00129 Symbol_rep::show_symbol_entries (); } 00130 00131 TMPL inline syntactic flatten (const Symbol& s) { 00132 return apply ("symbol", flatten (*s)); } 00133 00134 TMPL 00135 struct binary_helper<Symbol >: public void_binary_helper<Symbol > { 00136 static inline string short_type_name () { 00137 return "Sy" * Short_type_name (C); } 00138 static inline generic full_type_name () { 00139 return gen ("Symbol", Full_type_name (C)); } 00140 static inline generic disassemble (const Symbol& x) { 00141 return as<generic> (C (*x)); } 00142 static inline Symbol assemble (const generic& x) { 00143 return Symbol (as<C> (x)); } 00144 static inline void write (const port& out, const Symbol& s) { 00145 binary_write<C> (out, *s); } 00146 static inline Symbol read (const port& in) { 00147 return Symbol (binary_read<C> (in)); } 00148 }; 00149 00150 #undef TMPL_DEF 00151 #undef TMPL 00152 #undef Symbol 00153 #undef Symbol_rep 00154 } // namespace mmx 00155 #endif // __MMX_SYMBOL_HPP