basix_doc 0.1
/Users/mourrain/Devel/mmx/basix/src/glue.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : glue.cpp
00004 * DESCRIPTION: Routines for defining glue
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/tuple.hpp>
00014 #include <basix/literal.hpp>
00015 #include <basix/compound.hpp>
00016 #include <basix/glue.hpp>
00017 namespace mmx {
00018 
00019 #ifdef BASIX_OLD_GNU_COMPILER
00020 template define_type_helper<true,generic>;
00021 // NOTE: this explicit instantiation avoids a bug in gcc 2.95.3
00022 #endif
00023 
00024 /******************************************************************************
00025 * These definitions should be made before anything else
00026 ******************************************************************************/
00027 
00028 bool
00029 fall_back_equal (const generic& x, const generic& y) {
00030   return exact_eq (x, y);
00031 }
00032 
00033 static bool
00034 fall_back_unequal (const generic& x, const generic& y) {
00035   return exact_neq (x, y);
00036 }
00037 
00038 void
00039 define_prerequisites () {
00040   define_type<generic> (GEN_GENERIC_TYPE);
00041   define_type<bool> (GEN_BOOLEAN_TYPE);
00042   define_type<int> (GEN_INT_TYPE);
00043   define_type<double> (GEN_DOUBLE_TYPE);
00044   define_type<literal> (GEN_LITERAL_TYPE);
00045   define_type<compound> (GEN_COMPOUND_TYPE);
00046   define_type<routine> (GEN_ROUTINE_TYPE);
00047   define_type<primitive> (GEN_MACRO_TYPE);
00048   define_constant<bool> (GEN_FALSE, false);
00049   define_constant<bool> (GEN_TRUE, true);
00050   define<bool,generic,generic> (GEN_EQUAL, fall_back_equal);
00051   define<bool,generic,generic> (GEN_UNEQUAL, fall_back_unequal);
00052 }
00053 
00054 /******************************************************************************
00055 * Gluing new data types
00056 ******************************************************************************/
00057 
00058 vector<generic>
00059 all_type_names () {
00060   generic ret;
00061   if (current_ev->get (gen (GEN_ALL_TYPES), ret))
00062     return as<vector<generic> > (ret);
00063   return vec<generic> ();
00064 }
00065 
00066 void
00067 define_type_sub (const generic& name, nat id) {
00068   current_ev->set (gen (GEN_TYPE_NAME, as<generic> (id)), name);
00069   current_ev->set (gen (GEN_TYPE_ID, name), as<generic> (id));
00070   if (!is_tuple_type (id) && !is_alias_type (id)) {
00071     vector<generic> all_types= all_type_names ();
00072     all_types << name;
00073     current_ev->set (gen (GEN_ALL_TYPES), as<generic> (all_types));
00074   }
00075 }
00076 
00077 nat
00078 type_id (const generic& name) {
00079   generic id;
00080   if (current_ev->get (gen (GEN_TYPE_ID, name), id) && is<nat> (id))
00081     return as<nat> (id);
00082   else return 1;
00083 }
00084 
00085 generic
00086 type_name (nat id) {
00087   generic name;
00088   if (current_ev->get (gen (GEN_TYPE_NAME, as<generic> (id)), name))
00089     return name;
00090   else return GEN_UNSPECIFIED_TYPE;
00091 }
00092 
00093 generic
00094 type_name (const generic& g) {
00095   return type_name (type (g));
00096 }
00097 
00098 nat
00099 accelerate_number () {
00100   static nat nr_ids= 0;
00101   return nr_ids++;
00102 }
00103 
00104 /******************************************************************************
00105 * Glued function table
00106 ******************************************************************************/
00107 
00108 static table<void (*) (), string>& glue_table () {
00109   static table<void (*) (), string> t;
00110   return t;
00111 }
00112 
00113 void register_glue (const string& s, void (*f) ()) {
00114   inside_set (glue_table (), s, f);
00115 }
00116 
00117 void call_glue (const string& s) {
00118   if (! contains (glue_table (), s))
00119     ERROR ("Can not find glued function " * s);
00120   (glue_table () [s]) ();
00121 }
00122 
00123 } // namespace mmx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines