semval.hh

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 #ifndef lestes__lang__cplus__syn__semval_hh__included
00029 #define lestes__lang__cplus__syn__semval_hh__included
00030 
00031 #include <lestes/common.hh>
00032 #include <lestes/intercode/intercode.g.hh>
00033 
00034 package(lestes);
00035 package(lang);
00036 package(cplus);
00037 package(syn);
00038 
00039 // base type; all referenced pointer have to be to a class derived from it
00040 typedef ::lestes::intercode::as_base semref_type;
00041 
00042 template<typename T>
00043 class semref {
00044         template<typename Y>
00045         friend class semref;
00046 private:
00047         ptr<semref_type> & ref;
00048 public:
00049         explicit semref<T>( ptr<semref_type> & a_ref ) : ref(a_ref)
00050         {}
00051         void operator= ( const ptr<T> & p ) const
00052         {
00053                 ref = p;
00054         }
00055         void operator= ( const semref<T> & a ) const
00056         {
00057                 // run-time check if `a' really references ptr<T> or class derived from it
00058                 ref = a.ref.template dncast<T>();
00059                 lassert2( ref, "Pointer assigned to a semref should not be NULL." );
00060         }
00061         template<typename Y>
00062         void operator= ( const semref<Y> & a ) const
00063         {
00064                 // run-time check if `a' really references ptr<Y> or class derived from it
00065                 // compile-time enforcement that Y is derived from T
00066                 ptr<T> tmp = a.ref.template dncast<Y>();
00067                 ref = tmp;
00068                 lassert2( ref, "Pointer assigned to a semref should not be NULL." );
00069         }
00070         ptr<T> operator-> () const
00071         {
00072                 // TODO pt workaround for 3.2.3
00073                 return ref.template dncast<T>();
00074                 // equivalent to the original:
00075                 //return operator ptr<T>();
00076         }
00077 #if 0
00078 operator ptr<T> () const
00079 {
00080         return ref.template dncast<T>();
00081 }
00082 operator srp<T> () const
00083 {
00084         return srp<T>(ref.template dncast<T>());
00085 }
00086 #endif
00087         // the following one not only makes ptr<T> from semval<T>, but also ptr<Y> when Y is base class of T
00088         template<typename Y>
00089         operator ptr<Y> () const
00090         {
00091                 // T below is intentional
00092                 return ref.template dncast<T>();
00093         }
00094         // the following one not only makes ptr<T> from semval<T>, but also ptr<Y> when Y is base class of T
00095         template<typename Y>
00096         operator srp<Y> () const
00097         {
00098                 // T below is intentional
00099                 return srp<Y>(ref.template dncast<T>());
00100         }
00101 };
00102 
00103 class semval {
00104 private:
00105         ptr<semref_type> pp;
00106 public:
00107         explicit semval() : pp() {}
00108         template<typename T> semref<T> select()
00109         {
00110                 return semref<T>(pp);
00111         }
00112 };
00113 
00114 end_package(syn);
00115 end_package(cplus);
00116 end_package(lang);
00117 end_package(lestes);
00118 
00119 #endif  // lestes__lang__cplus__syn__semval_hh__included

Generated on Mon Feb 12 18:23:15 2007 for lestes by doxygen 1.5.1-20070107