dumper.test.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #include <lestes/common.hh>
00029 #include <lestes/std/reflect.hh>
00030 #include <lestes/std/dumper_visitor.hh>
00031 #include <lestes/std/objectize.hh>
00032 #include <lestes/std/objectize_macros.hh>
00033 #include <lestes/std/map.hh>
00034 #include <lestes/std/set.hh>
00035 #include <lestes/std/list.hh>
00036 #include <lestes/std/pair.hh>
00037 #include <lestes/std/vector.hh>
00038 #include <fstream>
00039 #include <iostream>
00040 #include <lestes/std/dumper.hh>
00041 
00042 #define LENGTHOFARRAY( ar ) (sizeof(ar)/sizeof(ar[0]))
00043 
00044 package(lestes);
00045 package(std);
00046 
00047 class middle_object : public object {
00048 private:
00049         static ptr<reflection_list> reflection;
00050 
00051 protected:
00052         lint i;
00053         middle_object( int a_i ) : i(a_i)
00054         {}
00055 public:
00056         virtual ptr<reflection_list> reflection_get() const
00057         {
00058                 if (!reflection) {
00059                         typedef class_reflection::field_metadata md;
00060                         typedef class_reflection::field_metadata_list mdlist;
00061                         ptr<mdlist> mdl = mdlist::create();
00062                         mdl->push_back( md::create( "i", "lint" ) );
00063                         // append our one to a copy of the base class's list
00064                         reflection = reflection_list::create( object::reflection_get() );
00065                         reflection->push_back( class_reflection::create( "middle_object", mdl ) );
00066                 }
00067                 return reflection;
00068         }
00069 
00070         virtual ptr<field_list_list> field_values_get() const
00071         {
00072                 ptr<field_list_list> result = object::field_values_get();
00073                 result->push_back( value_list::create() );
00074                 result->back()->push_back( objectize<int>::create(i) );
00075                 return result;
00076         }
00077         static ptr<middle_object> create( int a_i )
00078         {
00079                 return new middle_object(a_i);
00080         }
00081 };
00082 
00083 ptr<object::reflection_list> middle_object::reflection;
00084 
00085 class dumped_object : public middle_object {
00086 private:
00087         static ptr<reflection_list> reflection;
00088 
00089 protected:
00090         srp<object> o;
00091         srp< ::lestes::std::list< srp<object> > > c;
00092         dumped_object() : middle_object(0)
00093         {}
00094         dumped_object( int a_i, ptr<object> a_o, ptr< list< srp< object> > > a_c ) : middle_object(a_i), o(a_o), c(a_c)
00095         {}
00096 
00097 public:
00098         virtual ptr<reflection_list> reflection_get() const
00099         {
00100                 if (!reflection) {
00101                         typedef class_reflection::field_metadata md;
00102                         typedef class_reflection::field_metadata_list mdlist;
00103                         ptr<mdlist> mdl = mdlist::create();
00104                         mdl->push_back( md::create( "o", "object" ) );
00105                         mdl->push_back( md::create( "c", "list&lt;dumped_object&gt;" ) );
00106                         // create a copy of base class's list and append our own item
00107                         reflection = reflection_list::create( middle_object::reflection_get() );
00108                         reflection->push_back( class_reflection::create( "dumped_object", mdl ) );
00109                 }
00110                 return reflection;
00111         }
00112         virtual ptr<field_list_list> field_values_get() const
00113         {
00114                 ptr<field_list_list> result = middle_object::field_values_get();
00115                 result->push_back( value_list::create() );
00116                 result->back()->push_back(o);
00117                 result->push_back( value_list::create() );
00118                 result->back()->push_back(c);
00119                 return result;
00120         }
00121         static ptr<dumped_object> create()
00122         {
00123                 return new dumped_object();
00124         }
00125         static ptr<dumped_object> create( int a_i, ptr<dumped_object> a_o, ptr< ::lestes::std::list< srp<object> > > a_c )
00126         {
00127                 return new dumped_object( a_i, a_o, a_c );
00128         }
00129 };
00130 
00131 ptr<object::reflection_list> dumped_object::reflection;
00132 
00133 enum test_enum {
00134         EN_1 = -10,
00135         EN_2 = 30,
00136         EN_3,
00137         EN_4 = EN_1 + 15,
00138 };
00139 
00140 specialize_objectize_for_enum(test_enum);
00141 
00142 end_package(std);
00143 end_package(lestes);
00144 
00145 using namespace ::lestes::std;
00146 using ::std::cout;
00147 using ::std::cerr;
00148 using ::std::endl;
00149 using ::std::ostream;
00150 using ::std::ofstream;
00151 using ::std::ostringstream;
00152 
00153 typedef map< srp<object>, ulint > map_type;
00154 
00155 package(lestes);
00156 package(std);
00157 
00158 // declare the iterator as simple, non-dumpable type
00159 specialize_objectize_nodump( ::map_type::iterator );
00160 specialize_objectize_nodump_reference( lint & );
00161 
00162 end_package(std);
00163 end_package(lestes);
00164 
00165 int main()
00166 {
00167         typedef list< srp<object> > olist;
00168         ptr<olist> li = olist::create();
00169         li->push_back( objectize<lint>::create(5) );
00170         li->push_back( objectize<ulint>::create(6) );
00171         ptr<dumped_object> o1 = dumped_object::create( 1, dumped_object::create(0,NULL,NULL), li );
00172         li->push_back( o1 );
00173         li->push_back( NULL );
00174 
00175         typedef list<bool> blist;
00176         ptr<blist> bl = blist::create();
00177         bl->push_back(true);
00178         bl->push_back(true);
00179         bl->push_back(false);
00180         li->push_back(bl);
00181 
00182         typedef set<lint> liset;
00183         ptr<liset> s = liset::create();
00184         s->insert(5);
00185         s->insert(3);
00186         s->insert(-1);
00187 
00188         typedef map< test_enum, srp<object> > mt;
00189         ptr<mt> m = mt::create();
00190         (*m)[EN_3] = m;
00191         (*m)[EN_1] = NULL;
00192         (*m)[EN_4] = s;
00193 
00194         typedef vector< srp<object> > vt;
00195         ptr<vt> v = vt::create();
00196         v->push_back(m);
00197         v->push_back(NULL);
00198         v->push_back(v);
00199         // the following line tests that reference types as arguments to
00200         // objectize template are working
00201         lint i = 5;
00202         v->push_back( objectize<int &>::create(i) );
00203 
00204         li->push_back(v);
00205 
00206         // test the nondumpable iterator ;)
00207         typedef map_type::iterator nd_type;
00208         ptr< list<nd_type> > lnd = list<nd_type>::create();
00209         lnd->push_back( map_type::create()->begin() );
00210         li->push_back(lnd);
00211 
00212         // test barrier
00213         ptr<object> o2 = dumped_object::create( 77, dumped_object::create(88,NULL,NULL), NULL );
00214         o2->dump_barrier_set( true );
00215         li->push_back( o2 );
00216 
00217         ofstream f("dumper.test.xml");
00218 
00219         readable_dumper::dump( f, o1 ) << "<!-- end of readable dump -->" << endl;
00220         dumper::dump( f, o1 ) << "<!-- end of first flat dump -->" << endl;
00221 
00222         li->push_front( vt::create() );
00223         dumper::dump( f, o1, false );
00224 
00225         f.close();
00226 
00227         return 0;
00228 }

Generated on Mon Feb 12 18:22:33 2007 for lestes by doxygen 1.5.1-20070107