yast2-core
YCPElement.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: YCPElement.h
14 
15  Author: Mathias Kettner <kettner@suse.de>
16  Maintainer: Klaus Kaempf <kkaempf@suse.de>
17 
18 /-*/
19 // -*- c++ -*-
20 
21 #ifndef YCPElement_h
22 #define YCPElement_h
23 
24 // MemUsage.h defines/undefines D_MEMUSAGE
25 #include <y2util/MemUsage.h>
26 
27 // include only forward declarations of iostream
28 #include <iosfwd>
29 #include <string>
30 #include <vector>
31 #include <map>
32 
33 using std::string;
34 using std::wstring;
35 using std::vector;
36 using std::map;
37 using std::pair;
38 
39 #include "toString.h"
40 
41 // forward declarations
42 
43 class YCPElement;
44 class YCPValue;
45 class YCPVoid;
46 class YCPBoolean;
47 class YCPInteger;
48 class YCPFloat;
49 class YCPString;
50 class YCPByteblock;
51 class YCPPath;
52 class YCPSymbol;
53 class YCPLocale;
54 class YCPList;
55 class YCPTerm;
56 class YCPMap;
57 class YCPBuiltin;
58 class YCPCode;
59 class YCPEntry;
60 class YCPReference;
61 class YCPExternal;
62 
63 
64 #define DEF_OPS(name) \
65 public: \
66  const YCP##name##Rep *operator ->() const { \
67  return static_cast<const YCP##name##Rep *>(element); } \
68  YCP##name##Rep *operator ->() { \
69  return const_cast<YCP##name##Rep *>( \
70  static_cast<const YCP##name##Rep *>(element)); } \
71 private: \
72  int operator !() const; \
73  int operator ==(const YCPElement &) const;
74 
75 
76 #ifdef D_MEMUSAGE
77 #define DEF_MEMSIZE(name) \
78  virtual size_t mem_size () const { return sizeof (YCP##name); }
79 #else
80 #define DEF_MEMSIZE(name)
81 #endif
82 
83 #define DEF_COMMON(name, base) \
84  DEF_OPS(name) \
85  friend class YCP##base##Rep; \
86 public: \
87  DEF_MEMSIZE(name) \
88  YCP##name(const YCPNull &n) : YCP##base(n) {} \
89 protected: \
90  YCP##name (const YCP##name##Rep *x) : YCP##base(x) {}
91 
92 
93 #define DEF_COW_OPS(name) \
94 public: \
95  const YCP##name *operator ->() const { \
96  return static_cast<const YCP##name *>(this); } \
97  YCP##name *operator ->() { \
98  return const_cast<YCP##name *>( \
99  static_cast<const YCP##name *>(this)); } \
100 private: \
101  int operator !() const; \
102  int operator ==(const YCPElement &) const;
103 
104 #define DEF_COW_COMMON(name, base) \
105  friend class YCP##base##Rep; \
106  DEF_COW_OPS(name) \
107 public: \
108  YCP##name(const YCPNull &n) : YCP##base(n) {} \
109 protected: \
110  YCP##name (const YCP##name##Rep *x) : YCP##base(x) {} \
111 public: \
112  YCPOrder compare(const YCP##name x) const { \
113  return (static_cast<const YCP##name##Rep*>(element))->compare(x); \
114  } \
115  string toString () const { return element->toString (); } \
116  std::ostream & toStream (std::ostream & str ) const { \
117  return element->toStream (str); \
118  } \
119  std::ostream & toXml (std::ostream & str, int indent ) const { \
120  return element->toXml( str, indent ); \
121  } \
122  YCPValueType valuetype () const { return (static_cast<const YCP##name##Rep*>(element))->valuetype (); }
123 
124 
125 class YCPNull {};
126 
127 /***
128  * <h2>The YCP Datastructures</h2>
129  *
130  * <p>YCP is both a scripting language and a communication protocol.
131  * A YCP value is a data structure. It has currently two possible
132  * representations. One ist an ASCII representation, the other is
133  * a representation a network of C++ objects. The class framework
134  * for this object representation is laid in this library. Furthermore
135  * It contains the @ref Parser, that transforms an ASCII representation
136  * of YCP values into the object-representation.
137  *
138  * <h2>YCP Data management</h2>
139  *
140  * <p>YCP data are managed via "smart pointers". This means that an application
141  * instantiates an object from a class in a conventional way but does not get
142  * the object itself. The result is a wrapper object that "points" to the real
143  * object which is automatically created on instantiation. This real object
144  * (the representation of the actual data) holds a reference counter and
145  * therefore "knows" who is using it. After all references to this object have
146  * diminished (e.g. auto variables get out of scope) the real object is
147  * automatically destroyed. This way it is neither necessary nor allowed
148  * to "new" or "delete" YCP data objects. Furthermore all members of the
149  * object must be accessed using pointer notation.
150  *
151  * <p>So all YCP data objects do exist in two flavours:
152  * <ul>
153  * <li>Class name without "Rep" is the usable object, e.g. @ref YCPInteger.</li>
154  * <li>Class name with "Rep" is the real Object, e.g. @ref YCPIntegerRep.</li>
155  * </ul>
156  *
157  * <p>Important: Applications <i>never</i> use "Rep" classes directly!
158  *
159  * <p>Example:
160  * <pre>
161  * {
162  * YCPInteger a (18); // here a YCPIntegerRep is created automatically
163  * YCPInteger b = a; // b points to the same YCPIntegerRep as a
164  *
165  * cout << b->toString (); // use pointer notation to access members
166  * cout << b->value () - 18; // a and b out of scope, last reference lost, YCPIntegerRep is destroyed automatically
167  * }
168  * </pre>
169  */
170 
223 #ifdef D_MEMUSAGE
224  : public MemUsage
225 #endif
226 {
232 
238 
245  mutable int reference_counter;
246 
247 protected:
248 
249  friend class YCPElement;
250 
254  YCPElementRep();
255 
261  virtual ~YCPElementRep();
262 
263 public:
267  YCPValue asValue() const;
268 
275  virtual string toString() const = 0;
276 
280  virtual std::ostream & toStream (std::ostream & str) const = 0;
281  virtual std::ostream & toXml (std::ostream & str, int indent ) const = 0;
282 
287  virtual const YCPElementRep* shallowCopy() const { return this; }
288 
289 private:
300  void destroy() const;
301 
308  const YCPElementRep *clone() const;
309 };
310 
318 #ifdef D_MEMUSAGE
319  : public MemUsage
320 #endif
321 {
322  DEF_OPS(Element);
323 protected:
325 
331  if (element->reference_counter == 1 ) return element;
332 
333  const YCPElementRep* old = element;
335  element->clone ();
336  old->destroy ();
337  return element;
338  }
339 
340 public:
341  YCPElement();
342  YCPElement(const YCPNull&);
343  YCPElement(const YCPElementRep *e);
344  YCPElement(const YCPElement &e);
345  ~YCPElement();
346  const YCPElement& operator=(const YCPElement& e);
347  bool isNull() const { return element == 0; }
348  bool refersToSameElementAs(const YCPElement& e) const { return element == e.element; }
349 };
350 
351 #endif // YCPElement_h
#define str
Definition: scanner.cc:997
int reference_counter
Definition: YCPElement.h:245
YCPElement()
Definition: YCPElement.cc:63
static string old(const string &filename, int i, const char *suffix)
Definition: y2log.cc:418
Wrapper for YCPIntegerRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPIntegerRep with the arrow operator. See YCPIntegerRep.
Definition: YCPInteger.h:92
void destroy() const
Definition: YCPElement.cc:38
YCPElementRep()
Definition: YCPElement.cc:28
virtual std::ostream & toXml(std::ostream &str, int indent) const =0
Wrapper for YCPExternalRep This class realizes an automatic memory management via YCPElement...
Definition: YCPExternal.h:90
Wrapper for YCPByteblockRep This class realizes an automatic memory management via YCPElement...
Definition: YCPByteblock.h:108
Wrapper for YCPElementRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPElementRep with the arrow operator. See YCPElementRep.
Definition: YCPElement.h:317
bool refersToSameElementAs(const YCPElement &e) const
Definition: YCPElement.h:348
const YCPElementRep * element
Definition: YCPElement.h:324
Wrapper for YCPCodeRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPCodeRep with the arrow operator. See YCPCodeRep.
Definition: YCPCode.h:96
Wrapper for YCPMapRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPMapRep with the arrow operator. See YCPMapRep.
Definition: YCPMap.h:184
Wrapper for YCPSymbolRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPSymbolRep with the arrow operator. See YCPSymbolRep.
Definition: YCPSymbol.h:101
const YCPElement & operator=(const YCPElement &e)
Definition: YCPElement.cc:90
Wrapper for YCPReferenceRep This class realizes an automatic memory management via YCPElement...
Definition: YCPCode.h:377
bool isNull() const
Definition: YCPElement.h:347
DEF_OPS(Element)
~YCPElement()
Definition: YCPElement.cc:83
Wrapper for YCPStringRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPStringRep with the arrow operator. See YCPStringRep.
Definition: YCPString.h:133
Wrapper for YCPEntryRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPEntryRep with the arrow operator. See YCPEntryRep.
Definition: YCPCode.h:305
Wrapper for YCPListRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPListRep with the arrow operator. See YCPListRep.
Definition: YCPList.h:236
Abstract base class of all YCP elements.
Definition: YCPElement.h:222
Wrapper for YCPVoidRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPVoidRep with the arrow operator. See YCPVoidRep.
Definition: YCPVoid.h:75
YCPValue asValue() const
Definition: YCPElement.cc:55
Wrapper for YCPTermRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPTermRep with the arrow operator. See YCPTermRep.
Definition: YCPTerm.h:177
YCPElementRep & operator=(const YCPElementRep &)
Wrapper for YCPValueRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPValueRep with the arrow operator. See YCPValueRep.
Definition: YCPValue.h:275
Wrapper for YCPPathRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPPathRep with the arrow operator. See YCPPathRep.
Definition: YCPPath.h:175
const YCPElementRep * clone() const
Definition: YCPElement.cc:48
virtual ~YCPElementRep()
Definition: YCPElement.cc:33
virtual string toString() const =0
Wrapper for YCPFloatRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPFloatRep with the arrow operator. See YCPFloatRep.
Definition: YCPFloat.h:101
virtual const YCPElementRep * shallowCopy() const
Definition: YCPElement.h:287
Wrapper for YCPBooleanRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPBooleanRep with the arrow operator. See YCPBooleanRep.
Definition: YCPBoolean.h:93
const YCPElementRep * writeCopy()
Definition: YCPElement.h:330
Definition: MemUsage.h:37
virtual std::ostream & toStream(std::ostream &str) const =0
Definition: YCPElement.h:125

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