00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef YCPElement_h
00022 #define YCPElement_h
00023
00024
00025 #include <y2util/MemUsage.h>
00026
00027
00028 #include <iosfwd>
00029 #include <string>
00030 #include <vector>
00031 #include <map>
00032
00033 using std::string;
00034 using std::vector;
00035 using std::map;
00036 using std::pair;
00037
00038 #include "toString.h"
00039
00040
00041
00042 class YCPElement;
00043 class YCPValue;
00044 class YCPVoid;
00045 class YCPBoolean;
00046 class YCPInteger;
00047 class YCPFloat;
00048 class YCPString;
00049 class YCPByteblock;
00050 class YCPPath;
00051 class YCPSymbol;
00052 class YCPLocale;
00053 class YCPList;
00054 class YCPTerm;
00055 class YCPMap;
00056 class YCPBuiltin;
00057 class YCPCode;
00058 class YCPEntry;
00059 class YCPReference;
00060 class YCPExternal;
00061
00062
00063 #define DEF_OPS(name) \
00064 public: \
00065 const YCP##name##Rep *operator ->() const { \
00066 return static_cast<const YCP##name##Rep *>(element); } \
00067 YCP##name##Rep *operator ->() { \
00068 return const_cast<YCP##name##Rep *>( \
00069 static_cast<const YCP##name##Rep *>(element)); } \
00070 private: \
00071 int operator !() const; \
00072 int operator ==(const YCPElement &) const;
00073
00074
00075 #ifdef D_MEMUSAGE
00076 #define DEF_MEMSIZE(name) \
00077 virtual size_t mem_size () const { return sizeof (YCP##name); }
00078 #else
00079 #define DEF_MEMSIZE(name)
00080 #endif
00081
00082 #define DEF_COMMON(name, base) \
00083 DEF_OPS(name) \
00084 friend class YCP##base##Rep; \
00085 public: \
00086 DEF_MEMSIZE(name) \
00087 YCP##name(const YCPNull &n) : YCP##base(n) {} \
00088 protected: \
00089 YCP##name (const YCP##name##Rep *x) : YCP##base(x) {}
00090
00091
00092 #define DEF_COW_OPS(name) \
00093 public: \
00094 const YCP##name *operator ->() const { \
00095 return static_cast<const YCP##name *>(this); } \
00096 YCP##name *operator ->() { \
00097 return const_cast<YCP##name *>( \
00098 static_cast<const YCP##name *>(this)); } \
00099 private: \
00100 int operator !() const; \
00101 int operator ==(const YCPElement &) const;
00102
00103 #define DEF_COW_COMMON(name, base) \
00104 friend class YCP##base##Rep; \
00105 DEF_COW_OPS(name) \
00106 public: \
00107 YCP##name(const YCPNull &n) : YCP##base(n) {} \
00108 protected: \
00109 YCP##name (const YCP##name##Rep *x) : YCP##base(x) {} \
00110 public: \
00111 YCPOrder compare(const YCP##name x) const { \
00112 return (static_cast<const YCP##name##Rep*>(element))->compare(x); \
00113 } \
00114 string toString () const { return element->toString (); } \
00115 std::ostream & toStream (std::ostream & str ) const { \
00116 return element->toStream (str); \
00117 } \
00118 YCPValueType valuetype () const { return (static_cast<const YCP##name##Rep*>(element))->valuetype (); }
00119
00120
00121 class YCPNull {};
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00218 class YCPElementRep
00219 #ifdef D_MEMUSAGE
00220 : public MemUsage
00221 #endif
00222 {
00227 YCPElementRep& operator=(const YCPElementRep&);
00228
00233 YCPElementRep(const YCPElementRep&);
00234
00241 mutable int reference_counter;
00242
00243 protected:
00244
00245 friend class YCPElement;
00246
00250 YCPElementRep();
00251
00257 virtual ~YCPElementRep();
00258
00259 public:
00263 YCPValue asValue() const;
00264
00271 virtual string toString() const = 0;
00272
00276 virtual std::ostream & toStream (std::ostream & str) const = 0;
00277
00282 virtual const YCPElementRep* shallowCopy() const { return this; }
00283
00284 private:
00295 void destroy() const;
00296
00303 const YCPElementRep *clone() const;
00304 };
00305
00312 class YCPElement
00313 #ifdef D_MEMUSAGE
00314 : public MemUsage
00315 #endif
00316 {
00317 DEF_OPS(Element);
00318 protected:
00319 const YCPElementRep *element;
00320
00325 const YCPElementRep *writeCopy() {
00326 if (element->reference_counter == 1 ) return element;
00327
00328 const YCPElementRep* old = element;
00329 element = element->shallowCopy ();
00330 element->clone ();
00331 old->destroy ();
00332 return element;
00333 }
00334
00335 public:
00336 YCPElement();
00337 YCPElement(const YCPNull&);
00338 YCPElement(const YCPElementRep *e);
00339 YCPElement(const YCPElement &e);
00340 ~YCPElement();
00341 const YCPElement& operator=(const YCPElement& e);
00342 bool isNull() const { return element == 0; }
00343 bool refersToSameElementAs(const YCPElement& e) const { return element == e.element; }
00344 };
00345
00346 #endif // YCPElement_h