basix_doc 0.1
|
00001 00002 /****************************************************************************** 00003 * MODULE : alias.cpp 00004 * DESCRIPTION: Syntactic alias type constructor 00005 * COPYRIGHT : (C) 2006 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #include <basix/alias.hpp> 00014 #include <basix/table.hpp> 00015 #include <basix/routine.hpp> 00016 namespace mmx { 00017 00018 void 00019 alias_type_info (nat& id, int& mode) { 00020 static table<nat,nat> t (0); 00021 static table<nat,nat> u (0); 00022 static table<nat,nat> v (0); 00023 if (mode == 0) { 00024 mode= t->contains (id)? 1: 0; 00025 id= read (t, id); 00026 } 00027 else if (mode == 1) { 00028 mode= u->contains (id)? 1: 0; 00029 id= read (u, id); 00030 } 00031 else if (mode == 2) { 00032 if (u->contains (id)) id= u[id]; 00033 else { 00034 nat new_id= new_type_id (); 00035 t[new_id]= id; 00036 u[id]= new_id; 00037 id= new_id; 00038 } 00039 } 00040 else if (mode == 3) { 00041 if (v->contains (id)) id= v[id]; 00042 else { 00043 nat new_id= new_type_id (); 00044 t[new_id]= id; 00045 v[id]= new_id; 00046 id= new_id; 00047 } 00048 } 00049 else ERROR ("wrong mode"); 00050 } 00051 00052 void 00053 alias_specializer (nat id, routine& r) { 00054 static table<routine,nat> t; 00055 if (is_nil (r)) r= read (t, id); 00056 else t[id]= r; 00057 } 00058 00059 generic 00060 specialize_alias (const generic& a) { 00061 generic val= get_alias (as<alias<generic> > (a)); 00062 routine r; 00063 alias_specializer (type (val), r); 00064 return r->apply (a); 00065 } 00066 00067 void 00068 alias_getter (nat id, routine& r) { 00069 static table<routine,nat> t; 00070 if (is_nil (r)) r= read (t, id); 00071 else t[id]= r; 00072 } 00073 00074 generic 00075 get_alias (const generic& a) { 00076 routine r; 00077 alias_getter (type (a), r); 00078 return r->apply (a); 00079 } 00080 00081 void 00082 alias_setter (nat id, routine& r) { 00083 static table<routine,nat> t; 00084 if (is_nil (r)) r= read (t, id); 00085 else t[id]= r; 00086 } 00087 00088 generic 00089 set_alias (const generic& a, const generic& val) { 00090 routine r; 00091 alias_setter (type (a), r); 00092 return r->apply (a, val); 00093 } 00094 00095 } // namespace mmx