Polaris: BaseElement.cc Source File

BaseElement.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifdef POLARIS_GNU_PRAGMAS
00004 #pragma implementation
00005 #endif
00006 ///
00007 #include "BaseElement.h"
00008 #include "../macros.h"
00009 
00010 BaseElement::~BaseElement()
00011 {
00012     if (_wrap.get()) {
00013         _listable_set_wrapper(_wrap.get(), NULL);
00014         dec_refs_delete_or_zombie(_wrap.get());
00015     }
00016 }
00017 
00018 void
00019 BaseElement::print(ostream & out) const
00020 {
00021     if (_wrap.get())
00022         _wrap.get()->print(out);
00023     else
00024         out << "<INVALID>";
00025 }
00026 
00027 Listable *
00028 BaseElement::listable_clone() const
00029 {
00030     return (Listable *) new BaseElement(*this);
00031 }
00032 
00033 /// This interface is for testing purposes only.
00034 
00035 ostream & 
00036 operator << (ostream & o, const BaseElement &e)
00037 { 
00038     e.print(o); 
00039     return o; 
00040 }
00041 
00042 void
00043 BaseElement::modify(Listable *el)
00044 {
00045     if (el) {
00046         p_assert((!el->wrapper()) && (!el->in_collection()),
00047                  "BaseElement::modify(Listable *): "
00048                  "object already in a collection");
00049 
00050         if (_wrap.get())  { /// ...  we had an object - remove it
00051             _wrap.get()->wrapper(NULL);
00052             dec_refs_delete_or_zombie(_wrap.get());
00053         }
00054 
00055         _wrap.set(el);
00056 
00057         el->ref_inc();  /// ...  new_object gains a ref
00058         el->in_collection(1);  /// ...  now in a collection
00059 
00060     // el->wrapper(&_wrap);  /// ...  COULD BE TROUBLE!
00061     /// ...  This is illegal because wrapper( *) expects a Wrapper
00062     /// ...  not a ProtoWrapper--we do it directly...shouldn`t cause
00063     /// ...  any serious problems.
00064 
00065     el->_wrapper = CASTAWAY(Wrapper *) &_wrap;
00066     }
00067     else {
00068         /// ...  We are becoming invalid
00069         if (_wrap.get()) {
00070             _wrap.get()->wrapper(0);
00071             dec_refs_delete_or_zombie(_wrap.get());
00072             _wrap.set(0);
00073         }
00074     }       
00075 }
00076 
00077 void
00078 BaseElement::del()
00079 {
00080     if (_wrap.get()) {
00081         _wrap.get()->wrapper(0);
00082         dec_refs_delete_or_zombie(_wrap.get());
00083         _wrap.set(0);
00084     }
00085 }
00086 
00087 Listable *
00088 BaseElement::grab()
00089 {
00090     p_assert(_wrap.get(), "BaseElement:grab():  object invalid");
00091     
00092     Listable *el = _wrap.get();
00093 
00094     _wrap.get()->wrapper(0);
00095     _wrap.get()->ref_remove(); /// ...  we no longer point to it (but don't delete
00096                                /// ...  it by accident)
00097     _wrap.get()->in_collection(0);  /// ...  It is no longer in a collection
00098     _wrap.set(0);
00099 
00100     return el;
00101 }
00102 
00103 Listable *
00104 BaseElement::modify_and_grab(Listable *el)
00105 {
00106     p_assert(_wrap.get(), "BaseElement:modify_and_grab():  object invalid");
00107 
00108     Listable *p = grab();
00109 
00110     modify(el);
00111 
00112     return p;
00113 }
00114 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:40 2005