让C++的对象支持多类型
来源:优易学  2011-12-26 16:39:09   【优易学:中国教育考试门户网】   资料下载   IT书店

  bool operator==(XmlRpcValue const& other) const;
  bool operator!=(XmlRpcValue const& other) const;
  operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
  operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
  operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
  operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
  operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
  operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
  XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
  XmlRpcValue& operator[](int i) { assertArray(i+1); return _value.asArray->at(i); }
  XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
  XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s];
  }
  // Accessors
  //! Return true if the value has been set to something.
  bool valid() const { return _type != TypeInvalid; }
  //! Return the type of the value stored. \see Type.
  Type const &getType() const { return _type; }
  //! Return the size for string, base64, array, and struct values.
  int size() const;
  //! Specify the size for array values. Array values will grow beyond this size if needed.
  void setSize(int size) { assertArray(size); }
  //! Check for the existence of a struct member by name.
  bool hasMember(const std::string& name) const;
  // Type tag and values
  Type _type;
  // At some point I will split off Arrays and Structs into
  // separate ref-counted objects for more efficient copying.
  union {
  bool asBool;
  int asInt;
  double asDouble;
  struct tm* asTime;
  std::string* asString;
  BinaryData* asBinary;
  ValueArray* asArray;
  ValueStruct* asStruct;
  } _value;
  };
  xpdf中Object的核心代码如下
  enum ObjType {
  // simple objects
  objBool, // boolean
  objInt, // integer
  objReal, // real
  objString, // string
  objName, // name
  objNull, // null
  // complex objects
  objArray, // array
  objDict, // dictionary
  objStream, // stream
  objRef, // indirect reference
  // special objects
  objCmd, // command name
  objError, // error return from Lexer
  objEOF, // end of file return from Lexer
  objNone // uninitialized object
  };
  class Object {
  public:
  // Default constructor.
  Object():
  type(objNone) {}
  // Initialize an object.
  Object *initBool(GBool boolnA)
  { initObj(objBool); booln = boolnA; return this; }
  Object *initInt(int intgA)
  { initObj(objInt); intg = intgA; return this; }
  Object *initReal(double realA)
  { initObj(objReal); real = realA; return this; }
  Object *initString(GString *stringA)
  { initObj(objString); string = stringA; return this; }
  Object *initName(char *nameA)
  { initObj(objName); name = copyString(nameA); return this; }
  Object *initNull()
  { initObj(objNull); return this; }
  Object *initArray(XRef *xref);
  Object *initDict(XRef *xref);
  Object *initDict(Dict *dictA);
  Object *initStream(Stream *streamA);
  Object *initRef(int numA, int genA)
  { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
  Object *initCmd(char *cmdA)
  { initObj(objCmd); cmd = copyString(cmdA); return this; }
  Object *initError()
  { initObj(objError); return this; }
  Object *initEOF()
  { initObj(objEOF); return this; }
  // Copy an object.
  Object *copy(Object *obj);

上一页  [1] [2] [3] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训