Essential COM

, . . , , , , . , , . , ( ) . , , . , (contracts), . .

, , . , , vtbl . IFastString:

 class IFastString {    public:      virtual void Delete(void) = 0;      virtual int Length(void) = 0;      virtual int Find(const char *psz) = 0;  };  

vtbl, , - vtbl , . , , vtbl:

 class IFastString {   public:        // faux version 1.0        //     1.0      virtual void Delete(void) = 0;      virtual int Length(void) = 0;      virtual int Find(const char *psz) = 0;        // faux version 2.0        //     2.0      virtual int FindN(const char *psz, int n) = 0;  };  

. , , vtbl, . , vtbl FindN, . , , , IFastString , , FindN . FindN , , . .

, , . , C++ , , , . , . , . , , , . , . C++ Runtime Type Identification RTTI, , .

, . IFastString FindN, n- , IFastString :

 class IFastString2 : public IFastString {    public:        // real version 2.0        //     2.0      virtual int FindN(const char *psz, int n) = 0;  };  

C++ dynamic_cast, , IFastString2

 int Find10thBob(IFastString *pfs)  {      IFastString2 *pfs2 = dynamic_cast<IFastString2*>(pfs);      if(pfs2)        // the object derives from IFastString2        //       IFastString2        return pfs2->FindN("Bob", 10);      else {        // object doesn't derive from IFastString2        //         IFastString2        error("Cannot find 10th occurrence of Bob");      return -1;  }  

, dynamic_cast , IFastString2, . , dynamic_cast (null) . , , . , .

, . , , , (persistence), IFastString. , , Load Save IFastString, , IFastString, . , IFastString:

 class IPersistentObject : public IFastString {    public:      virtual bool Load(const char *pszFileName) = 0;      virtual bool Save(const char *pszFileName) = 0;  };  

, Length Find. , . , IPersistentObject , , IFastString:

 class IPersistentObject {    public:      virtual void Delete(void) = 0;      virtual bool Load(const char *pszFileName) = 0;      virtual bool Save(const char *pszFileName) = 0;  };  

FastString ; , FastString : IFastString, IPersistentObject:

 class FastString : public IFastString, public IPersistentObject {      int m_cch;        // count of characters        //          char *m_psz;    public:      FastString(const char *psz);      ~FastString(void);        // Common methods        //          void Delete(void);        // deletes this instance        //              // IFastString methods        //   IFastString      int Length(void) const;        // returns # of characters        //            int Find(const char *psz) const;       // returns offset        //                 // IPersistentObject methods        //   IPersistentObject      bool Load(const char *pszFileName);      bool Save(const char *pszFileName);  };  

FastString , RTTI IPerststentObject, :

 bool SaveString(IFastString *pfs, const char *pszFN) {      bool bResult = false;      IPersistentObject *ppo = dynamic_cast<IPersistentObject*>(pfs);      if (ppo)          bResult = ppo->Save(pszFN);      return bResult;  }  

, , , IPersistentObject. .

RTTI - , . , DWP RTTI, RTTI . , . , . dynamic_cast , . , dynamic_cast, , , C++:

 class IPersistentObject {    public:      virtual void *Dynamic_Cast(const char *pszType) = 0;      virtual void Delete(void) = 0;      virtual bool Load(const char *pszFileName) = 0;      virtual bool Save(const char *pszFileName) = 0;  };  class IFastString {   public:      virtual void *Dynamic_Cast(const char *pszType) = 0;      virtual void Delete(void) = 0;      virtual int Length(void) = 0;      virtual int Find(const char *psz) = 0;  };  

Delete, , :

 class IExtensibleObject {   public:      virtual void *Dynamic_Cast(const char* pszType) = 0;      virtual void Delete(void) = 0;  };  class IPersistentObject : public IExtensibleObject {   public:      virtual bool Load(const char *pszFileName) = 0;      virtual bool Save(const char *pszFileName) = 0;  };  class IFastString : public IExtensibleObject {   public:      virtual int Length(void) = 0;      virtual int Find(const char *psz) = 0;  };  

, :

 bool SaveString(IFastString *pfs, const char *pszFN) {     boot bResult = false;      IPersistentObject *ppo = (IPersistentObject) pfs->Dynamic_Cast("IPers1stentObject");      if (ppo)          bResult = ppo->Save(pszFN);      return bResult;  }  

, :

 class FastString : public IFastString, public IPersistentObject {      int   m_c h;        // count of characters        //          char *m_psz;    public:      FastString(const char *psz);      ~FastString(void);        // IExtensibleObject methods        //   IExtensibleObject      void *Dynamic_Cast(const char *pszType);      void Delete(void);        // deletes this instance        //              // IFastString methods        //   IFastString      int Length(void) const;        // returns # of characters        //            int Find(const char *psz) const;        // returns offset        //            // IPersistentObject methods        //   IPersistentObject      bool Load(const char *pszFileName);      bool Save(const char *pszFileName);  };  

Dynamic_Cast RTTI . 1.8 FastString. , , Dynamic_Cast FastString (explicit static casts), this, , :

 void *FastString::Dynam1c_Cast(const char *pszType)  {      if (strcmp(pszType, "IFastString") == 0)          return static_cast<IFastString*>(this);      else if (strcmp(pszType, "IPersistentObject") == 0)          return static_cast<IPersistentObject*>(this);     else if (strcmp(pszType, "IExtensibleObject") == 0)          return static_cast<IFastString*>(this);      else return 0;       // request for unsupported interface       //          } 

 . 1.8.     faststring

, , this, .

, IExtensibleObject IFastString. , (intuitive version)

 return static_cast<IExtensibleObject*>(this);  

, IFastString, IPersistentObject IExtensibleObject. IExtensibleObject IFastString, IPersistentObject, . , . , C++, .



Suschnost' tehnologii SOM
Essential COM
ISBN: 0201634465
EAN: 2147483647
Year: N/A
Pages: 103
Authors: Don Box

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net