yast2-core
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
YCPElementRep Class Referenceabstract

Abstract base class of all YCP elements. More...

#include <YCPElement.h>

Inheritance diagram for YCPElementRep:
YCPValueRep YCPBooleanRep YCPBreakRep YCPByteblockRep YCPCodeRep YCPEntryRep YCPExternalRep YCPFloatRep YCPIntegerRep YCPListRep YCPMapRep YCPPathRep YCPReferenceRep YCPReturnRep YCPStringRep YCPSymbolRep YCPTermRep YCPVoidRep

Public Member Functions

YCPValue asValue () const
 
virtual string toString () const =0
 
virtual std::ostream & toStream (std::ostream &str) const =0
 
virtual std::ostream & toXml (std::ostream &str, int indent) const =0
 
virtual const YCPElementRepshallowCopy () const
 

Protected Member Functions

 YCPElementRep ()
 
virtual ~YCPElementRep ()
 

Private Member Functions

YCPElementRepoperator= (const YCPElementRep &)
 
 YCPElementRep (const YCPElementRep &)
 
void destroy () const
 
const YCPElementRepclone () const
 

Private Attributes

int reference_counter
 

Friends

class YCPElement
 

Detailed Description

Abstract base class of all YCP elements.

There are some basic rules of memory managesment common to all YCPElementRep classes. If you call a constructor of any YCPElementRep subclass or if you call an add method, that adds elements to a YCPListRep, YCPTermRep YCPDeclTermRep or YCPBlockRep, and if this constructor or add method has arguments of type const YCPElementRep * (or of a subclass), then the responsibility for the values you gave for those arguments goes over to the object whose constructor or add function has been called. You may refer that object afterwards in any way. Therefore create the object either with new or with the YCPElementRep::clone method.

Example1:

YCPTermRep *term = new YCPTermRep (new YCPSymbolRep ("foo"));

Example2:

YCPSymbolRep sym ("foo");
YCPTermRep term (sym.clone ());

The second rule is that you never should delete any YCPElementRep object directly. Rather use the method YCPElementRep::destroy for that purpose. YCPElementRep::clone and YCPElementRep::destroy keep keep reference counter in order to decide, if any valid reference to the object exists. destroy calls delete when the last reference is dropped.

The third rule is about return values of the methods YCPElementRep and its subclasses. Member methods that return pointers to YCPElementRep subclasses simply return a pointer to the answer without increasing its reference counter. You must not destroy such an answer.

Example:

YCPTermRep *term = new YCPTermRep (new YCPSymbolRep ("foo"));
cout << term->symbol ()->toString ();

If you want to keep the result beyond the scope of the object whose method you called, use YCPElementRep that create a valid reference to the returned object.

The fourth rule is: Pointers to YCPElementRep are always const with the only exception of YCPListRep, YCPDeclTermRep, YCPTermRep and YCPBlockRep who have an add member function that allows the parser to construct those objects element by element.

Constructor & Destructor Documentation

YCPElementRep::YCPElementRep ( const YCPElementRep )
private

Prevent implicite definition of copy constructor. Assignment of YCPElements is not possible nor allowed.

YCPElementRep::YCPElementRep ( )
protected

Initializes this object. Sets the to 1.

YCPElementRep::~YCPElementRep ( )
protectedvirtual

Frees all resources used by this object. Never call this method directly (don't use delete). Rather use the member function destroy. It manages the reference counting.

Member Function Documentation

YCPValue YCPElementRep::asValue ( ) const

Casts this element into a pointer of type YCPValueRep

const YCPElementRep * YCPElementRep::clone ( ) const
private

Gives you a new reference to this object. Increases the reference_counter. This method is delared const though it changes a member value - the reference_counter. But it is constant in that it doesn't change the YCP meaning of this YCPElementRep.

References reference_counter.

Referenced by YCPElement::operator=(), and YCPElement::writeCopy().

void YCPElementRep::destroy ( ) const
private

Call this method instead of delete, when you have a pointer to a YCPElementRep that you don't need any longer. The data the YCPElementRep pointer points to may be shared data. This method handles reference counting and deletes the object when the reference counter drops to 0. Destroy is declared const, but of course changes the objects data. But it doesn't change the actual YCP value / meaning of the YCPElementRep.

References reference_counter, and y2internal.

Referenced by YCPElement::operator=(), YCPElement::writeCopy(), and YCPElement::~YCPElement().

YCPElementRep& YCPElementRep::operator= ( const YCPElementRep )
private

Prevent implicite definition of assignment. Assignment of YCPElements is not possible nor allowed.

virtual const YCPElementRep* YCPElementRep::shallowCopy ( ) const
inlinevirtual

Returns a shallow copy of this elementRep. Redefine this method to implement copy on write.

Reimplemented in YCPListRep, YCPTermRep, and YCPMapRep.

Referenced by YCPElement::writeCopy().

virtual std::ostream& YCPElementRep::toStream ( std::ostream &  str) const
pure virtual
virtual string YCPElementRep::toString ( ) const
pure virtual
virtual std::ostream& YCPElementRep::toXml ( std::ostream &  str,
int  indent 
) const
pure virtual

Friends And Related Function Documentation

friend class YCPElement
friend

Member Data Documentation

int YCPElementRep::reference_counter
mutableprivate

Counts the references for this value. Each time the clone method is called, the reference counter is increased. The destroy method decreases the reference counter and deletes the object, if the counter drops to 0.

Referenced by clone(), destroy(), and YCPElement::writeCopy().


The documentation for this class was generated from the following files:

Generated on a sunny day for yast2-core by doxygen 1.8.5