Polaris: Element.h Source File

Element.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef _ELEMENT_H
00004 #define _ELEMENT_H
00005 ///
00006 /// \class Element 
00007 /// \brief template for types derived from Listable
00008 /// \defgroup Polaris
00009 /// \ingroup Polaris
00010 ///  C++ VDL
00011 /// \see Element.h
00012 ///
00013 /// \endcode
00014 /// \section DESCRIPTION DESCRIPTION
00015 /// Element is a Collection structure which takes ownership of a
00016 /// single Listable object.  It can be viewed as providing the
00017 /// functionality of a single-element List.  However, there is
00018 /// much less overhead than a list and a more convenient
00019 /// (pointer-esque) interface.
00020 ///
00021 /// The following examples should describe it:
00022 ///
00023 /// \code
00024 /// void
00025 /// example( Statement *stmt, Statement *stmt2)
00026 /// {
00027 ///   Element<Statement> p;
00028 /// 
00029 ///   p = stmt; // 'p' now has ownership of 'stmt': Error if stmt already owned.
00030 ///             // 'p.modify(stmt);' does the same thing.
00031 ///
00032 ///   p = stmt2; // 'p' now owns 'stmt2'.  'stmt' either deleted or zombied.
00033 ///
00034 ///   cout << *p;  // '*p' returns a reference to stmt
00035 ///   cout << p->tag();  // 'p->' allows access to stmt's class members
00036 ///
00037 ///   Element<Statement> q = p;  // Copies (clones) p's object
00038 ///
00039 ///   Statement *s = q.grab();  // Returns ownership of object from q
00040 ///
00041 ///   p.del();      // Deletes p's owned object
00042 /// }
00043 /// \endcode
00044 ///
00045 /// There also exist methods valid, member and modify_and_grab.
00046 ///
00047 #include <stream.h>
00048 #include "BaseElement.h"
00049 ///
00050 template <class T> class Element : public Listable {
00051 
00052  protected:
00053     BaseElement     _el;
00054 
00055  public:
00056     INLINE Element();
00057     INLINE Element(T &e);
00058     INLINE Element(const Element<T> &e);
00059 
00060     INLINE  ~Element();
00061 
00062     INLINE Element<T> & operator=(T *l);
00063     ///< Element takes ownership of l.  If l already
00064     ///< owned an error is raised.  Previous object deleted or zombied.
00065     
00066     INLINE const T & operator*() const;
00067     INLINE T & operator*();
00068     ///< Return a reference to the object owned by Element.
00069 
00070     INLINE const T * operator->() const;
00071     INLINE T * operator->();
00072     ///< Allows class member access of the object owned by Element
00073     
00074     INLINE void     modify(T *l);
00075     ///< Element takes ownership of l.  If l already
00076     ///< owned an error is raised. Previous object deleted or zombied.
00077 
00078     INLINE T *      grab();
00079     ///< Remove the object from the Element and return it.
00080 
00081     INLINE void     del();
00082     ///< Delete the object (or Zombie it).
00083 
00084     INLINE void     clear();
00085     ///< Delete the object (or Zombie it).
00086 
00087     INLINE T *      modify_and_grab(T *el);
00088     ///< Change Element's object to el and return the old object.
00089 
00090     INLINE Boolean  member(T &el) const;
00091     ///< Check to see if node 'el' the same as the Element.  Return
00092     ///< 0 if the element was not the same, else return 1.
00093 
00094     INLINE Boolean  valid() const;
00095     ///< Check to see if the element if valid.
00096 
00097     INLINE Element<T> & operator = (const Element<T> &e);
00098     ///< Copy operator copies object of Element
00099 
00100     INLINE int      structures_OK() const;
00101     ///< Check the structure of the data for errors or inconsistency
00102     ///< Return 0 and print error message if problems found, otherwise
00103     ///< return 1 without message.
00104 
00105     INLINE Listable *listable_clone() const;
00106     ///< Needed for Listable class.
00107 
00108     INLINE void      print(ostream &o) const;
00109     ///< Needed for Listable class.
00110 };
00111 
00112 
00113 ///< implementation
00114 
00115 
00116 template <class T>
00117 INLINE 
00118 Element<T>::Element()
00119 {
00120     ///< Nothing to do
00121 }
00122 
00123 template <class T>
00124 INLINE 
00125 Element<T>::Element(const Element<T> &e)
00126 {
00127     _el = e._el;
00128 }
00129 
00130 template <class T>
00131 INLINE
00132 Element<T>::~Element()
00133 {
00134     ///< Nothing to do
00135 }
00136 
00137 template <class T>
00138 INLINE void
00139 Element<T>::print(ostream & out) const
00140 {
00141     _el.print(out);
00142 }
00143 
00144 template <class T>
00145 INLINE Element<T> &
00146 Element<T>::operator=(T *l)
00147 {
00148     _el = l;
00149     return *this;
00150 }
00151     
00152 template <class T>
00153 INLINE const T &
00154 Element<T>::operator*() const
00155 {
00156     return (const T &) *_el;
00157 }
00158 
00159 template <class T>
00160 INLINE T &
00161 Element<T>::operator*()
00162 {
00163     return (T &) *_el;
00164 }
00165 
00166 template <class T>
00167 INLINE const T *
00168 Element<T>::operator->() const
00169 {
00170     return (const T *) _el.operator -> ();
00171 }
00172 
00173 template <class T>
00174 INLINE T *
00175 Element<T>::operator->()
00176 {
00177     return (T *) _el.operator -> ();
00178 }
00179 
00180 template <class T>
00181 INLINE void
00182 Element<T>::modify(T *l)
00183 {
00184     _el.modify(l);
00185 }
00186 
00187 template <class T>
00188 INLINE T *
00189 Element<T>::grab()
00190 {
00191     return (T *) _el.grab();
00192 }
00193 
00194 template <class T>
00195 INLINE void
00196 Element<T>::del()
00197 {
00198     _el.del();
00199 }
00200 
00201 template <class T>
00202 INLINE void
00203 Element<T>::clear()
00204 {
00205     _el.del();
00206 }
00207 
00208 template <class T>
00209 INLINE T *
00210 Element<T>::modify_and_grab(T *el)
00211 {
00212     return (T *) _el.modify_and_grab(el);
00213 }
00214 
00215 template <class T>
00216 INLINE Boolean  
00217 Element<T>::member(T &el) const
00218 {
00219     return _el.member(el);
00220 }
00221 
00222 template <class T>
00223 INLINE Boolean  
00224 Element<T>::valid() const
00225 {
00226     return _el.valid();
00227 }
00228 
00229 template <class T>
00230 INLINE Element<T> &
00231 Element<T>::operator = (const Element<T> &e) 
00232 {
00233     _el = e._el;
00234     return *this;
00235 }
00236 
00237 template <class T>
00238 INLINE int      
00239 Element<T>::structures_OK() const
00240 {
00241     return 1;   ///< I'm OK, you're OK
00242 }
00243 
00244 template <class T>
00245 INLINE Listable *
00246 Element<T>::listable_clone() const
00247 {
00248     return (Listable *) new Element<T>(*this);
00249 }
00250 
00251 ///< This interface is for testing purposes only.  It invokes a non-virtual
00252 ///< member function to print a representation of the data structure at this
00253 ///< level of the Collection hierarchy, and, since it is not a class member
00254 ///< function, it must be explicitly instantiated before use.
00255 
00256 template <class T>
00257 ostream & 
00258 operator << (ostream & o, const Element<T> &e)
00259 {
00260   e.print(o);
00261   return o;
00262 }
00263 
00264 #endif
00265 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:45 2005