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