home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Internet 2000 May / MICD_2000_05.iso / CBuilder5 / INSTALL / DATA1.CAB / Program_Built_Files / Include / Vcl / systobj.h < prev    next >
C/C++ Source or Header  |  2000-02-01  |  14KB  |  392 lines

  1. // SYSTOBJ.H: Pascal TObject and Interface types
  2. // Copyright (c) 1997, 1999 Borland International
  3.  
  4. #if !defined(SYSTOBJ_H)
  5. #define SYSTOBJ_H
  6.  
  7. #if !defined(SystemHPP)
  8. #error Do not include this file directly.  Include 'system.hpp'.
  9. #endif
  10.  
  11. #if !defined(SYSMAC_H)
  12. #include <sysmac.h>
  13. #endif
  14. #if !defined(DSTRING_H)
  15. #include <dstring.h>
  16. #endif
  17. #if !defined(WSTRING_H)
  18. #include <wstring.h>
  19. #endif
  20.  
  21. #pragma option push -w-inl -w-lvc
  22.  
  23. namespace System
  24. {
  25.         struct PACKAGE TInterfaceEntry
  26.         {
  27.           TGUID IID;
  28.           Pointer VTable;
  29.           int IOffset;
  30.         };
  31.         typedef TInterfaceEntry *PInterfaceEntry;
  32.  
  33.         struct PACKAGE TInterfaceTable
  34.         {
  35.           int EntryCount;
  36.           TInterfaceEntry Entries[];
  37.         };
  38.         typedef TInterfaceTable *PInterfaceTable;
  39.  
  40.         // NOTE: Do not mess with TObject unless you really know what you're doing.
  41.         //       In particular, do not add, remove or change the order of virtual methods.
  42.         //
  43.         class DELPHICLASS TObject
  44.         {
  45.         public:
  46.                           __fastcall  TObject(); /* Body provided by VCL {} */
  47.  
  48.                           __fastcall Free();
  49.           TClass  __fastcall ClassType();
  50.           void    __fastcall CleanupInstance();
  51.           void *  __fastcall FieldAddress(const ShortString &Name);
  52.  
  53.           /*         class method */ static TObject * __fastcall InitInstance(TClass cls, void *instance);
  54.           /*         class method */ static ShortString __fastcall ClassName(TClass cls);
  55.           /*         class method */ static bool __fastcall ClassNameIs(TClass cls, const AnsiString string);
  56.           /*         class method */ static TClass __fastcall ClassParent(TClass cls);
  57.           /*         class method */ static void * __fastcall ClassInfo(TClass cls);
  58.           /*         class method */ static long __fastcall InstanceSize(TClass cls);
  59.           /*         class method */ static bool __fastcall InheritsFrom(TClass cls, TClass aClass);
  60.           /*         class method */ static void * __fastcall MethodAddress(TClass cls, const ShortString &Name);
  61.           /*         class method */ static ShortString __fastcall MethodName(TClass cls, void *Address);
  62.           
  63.           /* Hack: GetInterface is an untyped out object parameter and
  64.            * so is mangled as a void*. In practice, however, it is
  65.            * really a void**. Be sure when using this method to provide
  66.            * two levels of indirection and cast away one of them.
  67.            */
  68.  
  69.           bool __fastcall GetInterface(const TGUID &IID, /* out */ void *Obj);
  70.  
  71.           /*         class method */ static PInterfaceEntry __fastcall GetInterfaceEntry(const TGUID IID);
  72.           /*         class method */ static PInterfaceTable * __fastcall GetInterfaceTable(void);
  73.  
  74.           ShortString __fastcall ClassName()
  75.           {
  76.                 return ClassName(ClassType());
  77.           }
  78.  
  79.           bool __fastcall ClassNameIs(const AnsiString string)
  80.           {
  81.                 return ClassNameIs(ClassType(), string);
  82.           }
  83.  
  84.           TClass __fastcall ClassParent()
  85.           {
  86.                 return ClassParent(ClassType());
  87.           }
  88.  
  89.           void * __fastcall ClassInfo()
  90.           {
  91.                 return ClassInfo(ClassType());
  92.           }
  93.  
  94.           long __fastcall InstanceSize()
  95.           {
  96.                 return InstanceSize(ClassType());
  97.           }
  98.  
  99.           bool __fastcall InheritsFrom(TClass aClass)
  100.           {
  101.                 return InheritsFrom(ClassType(), aClass);
  102.           }
  103.  
  104.           void * __fastcall MethodAddress(const ShortString &Name)
  105.           {
  106.                 return MethodAddress(ClassType(), Name);
  107.           }
  108.  
  109.           ShortString __fastcall MethodName(void *Address)
  110.           {
  111.                 return MethodName(ClassType(), Address);
  112.           }
  113.  
  114.           virtual HResult __fastcall SafeCallException(TObject *ExceptObject, void *ExceptAddr);
  115.           virtual void __fastcall AfterConstruction();
  116.           virtual void __fastcall BeforeDestruction();
  117.           virtual void __fastcall Dispatch(void *Message);
  118.           virtual void __fastcall DefaultHandler(void* Message);
  119.  
  120.         private:
  121.           virtual TObject* __fastcall NewInstance(TClass cls);
  122.  
  123.         public:
  124.           virtual void __fastcall FreeInstance();
  125.           virtual __fastcall ~TObject();  /* Body provided by VCL {} */
  126.         };
  127.  
  128.         class PACKAGE TMetaClass
  129.         {
  130.         public:
  131.           TObject * __fastcall InitInstance(void *instance)
  132.           {
  133.                 return TObject::InitInstance(this, instance);
  134.           }
  135.  
  136.           ShortString __fastcall ClassName()
  137.           {
  138.                 return TObject::ClassName(this);
  139.           }
  140.  
  141.           bool __fastcall ClassNameIs(const AnsiString &string)
  142.           {
  143.                 return TObject::ClassNameIs(this,string);
  144.           }
  145.  
  146.           TClass __fastcall ClassParent()
  147.           {
  148.                 return TObject::ClassParent(this);
  149.           }
  150.  
  151.           void * __fastcall ClassInfo()
  152.           {
  153.                 return TObject::ClassInfo(this);
  154.           }
  155.  
  156.           long __fastcall InstanceSize()
  157.           {
  158.                 return TObject::InstanceSize(this);
  159.           }
  160.  
  161.           bool __fastcall InheritsFrom(TClass aClass)
  162.           {
  163.                 return TObject::InheritsFrom(this, aClass);
  164.           }
  165.  
  166.           void * __fastcall MethodAddress(const ShortString &Name)
  167.           {
  168.                 return TObject::MethodAddress(this, Name);
  169.           }
  170.  
  171.           ShortString __fastcall MethodName(void *Address)
  172.           {
  173.                 return TObject::MethodName(this,Address);
  174.           }
  175.         };
  176.  
  177.         /* DelphiInterface
  178.           This template class is to be used for allowing Delphi-owned interfaces to be returned
  179.           from Pascal function to C++ functions.  Since Delphi automatically provides reference
  180.           counting for these references (removing a huge burden from the programmer), the
  181.           class implemented strives to maintain this feature for the C++ programmer.
  182.  
  183.           The following invariants must hold:
  184.                 sizeof(DelphiInterface<T>) == 4
  185.                 template DelphiInterface<T> must be marked DELPHIRETURN
  186.                 BCC32 for BCB 3.0 or later must be used to compile this template
  187.                 reference counting must be provided by the ctor, copy ctor, operator= and dtor.
  188.                 the template type T must be derived from a class which has an AddRef and Release.
  189.  
  190.           The following general rules apply:
  191.                 - the T* and T** operators do not perform reference counting; it is the
  192.                   responsibility of the programmer to ensure that reference handling is
  193.                   correctly performed when these two conversion operators are used.
  194.                 - taking the address of the interface stored in a DelphiInterface<T> does NOT:
  195.                   - release the previous interface
  196.                   - increment the reference count of the current interface
  197.                         - it is the responsibility of the programmer to ensure that
  198.                         reference counting is handled properly when the T** operator is used.
  199.                 - if no interface is stored in a DelphiInterface<T>, the T* operator will return 0.
  200.         */
  201.         template <class T> class RTL_DELPHIRETURN DelphiInterface
  202.         {
  203.         private:
  204.           T       *intf;
  205.  
  206.           /* This is a temp hack to allow the C++ compiler to allow us to derrive
  207.                  from DelphiInterface even though we're not implementing and methods
  208.            */
  209.   //    virtual void __InternalPureHack() = 0;
  210.  
  211.         public:
  212.           __fastcall DelphiInterface<T>() : intf(0)
  213.           {}
  214.  
  215. #if (__BORLANDC__ >= 0x550)
  216.           // Allows a DelphiInterface<> to be created from another DelphiInterface<>.
  217.           // With __uuidof(), we know the IID of our interface
  218.           template <class ANOTHERINTF>
  219.           __fastcall DelphiInterface<T>(const DelphiInterface<ANOTHERINTF> &rhs) : intf(0)
  220.           {
  221.             rhs->QueryInterface(__uuidof(T), (LPVOID*)(&intf));
  222.           }
  223. #endif
  224.  
  225.           __fastcall DelphiInterface<T>(const DelphiInterface<T> &rhs)
  226.           {
  227.                 if (rhs.intf != 0)
  228.                   rhs.intf->AddRef();
  229.                 intf = rhs.intf;
  230.           }
  231.  
  232.           __fastcall DelphiInterface<T>(T* rhs)
  233.           {
  234.                 if (rhs != 0)
  235.                   rhs->AddRef();
  236.                 intf = rhs;
  237.           }
  238.  
  239.           __fastcall ~DelphiInterface<T>()
  240.           {
  241.                 if (intf != 0)
  242.                   intf->Release();
  243.                 intf = 0;
  244.           }
  245.  
  246. #if (__BORLANDC__ >= 0x550)
  247.           // Allows a DelphiInterface<> to be assigned to another DelphiInterface<>.
  248.           // With __uuidof(), we know the IID of our interface
  249.           template <class ANOTHERINTF>
  250.           DelphiInterface<T>& __fastcall operator =(DelphiInterface<ANOTHERINTF> &rhs)
  251.           {
  252.                 Release();
  253.                 rhs->QueryInterface(__uuidof(T), (LPVOID*)(&intf));
  254.           }
  255. #endif
  256.           DelphiInterface<T>& __fastcall operator =(DelphiInterface<T> &rhs)
  257.           {
  258.                 if (rhs.intf != 0)
  259.                   rhs.intf->AddRef();
  260.  
  261.                 if (intf != 0)
  262.                   intf->Release();
  263.  
  264.                 intf = rhs.intf;
  265.                 return *this;
  266.           }
  267.  
  268.           DelphiInterface<T>& __fastcall operator =(T *rhs)
  269.           {
  270.                 if (rhs != 0)
  271.                   rhs->AddRef();
  272.  
  273.                 if (intf != 0)
  274.                   intf->Release();
  275.  
  276.                 intf = rhs;
  277.                 return *this;
  278.           }
  279.  
  280.           T* __fastcall operator->() const
  281.           {
  282.                 return intf;
  283.           }
  284.  
  285.           bool operator ! () const
  286.           {
  287.                 return (intf == 0);
  288.           };
  289.  
  290.           __fastcall operator T*() const
  291.           {
  292.                 return intf;
  293.           }
  294.  
  295.           T& __fastcall operator *()
  296.           {
  297.                 return *intf;
  298.           }
  299.  
  300.           void Release()
  301.           {
  302.                 if (intf)
  303.                   intf->Release();
  304.                 intf = 0;
  305.           }
  306.  
  307.           T** __fastcall operator &()
  308.           {
  309.                 return &intf;
  310.           }
  311.         };
  312.  
  313.         class DELPHICLASS TInterfacedObject : public TObject
  314.         {
  315.         private:
  316.           void    *IUnknown;
  317.         protected:
  318.       int     FRefCount;
  319.           HResult __stdcall QueryInterface(const GUID& IID, /* out */ void **Obj);
  320.           int __stdcall _AddRef(void);
  321.           int __stdcall _Release(void);
  322.         public:
  323.       virtual void __fastcall BeforeDestruction();
  324.           __property int RefCount       = { read=FRefCount };
  325.         };
  326. }
  327.  
  328. // Type definition of interfaces wrapped by a smart object
  329. //
  330. #if !defined(DECLARE_DINTERFACE_TYPE)
  331. #define DECLARE_DINTERFACE_TYPE(iface) \
  332.         interface iface;               \
  333.         typedef System::DelphiInterface< iface >  _di_ ## iface;
  334. #endif  // DECLARE_DINTERFACE_TYPE
  335.  
  336. DECLARE_DINTERFACE_TYPE(IUnknown)
  337. DECLARE_DINTERFACE_TYPE(IDispatch)
  338. DECLARE_DINTERFACE_TYPE(IAdviseSink)
  339. DECLARE_DINTERFACE_TYPE(IDataAdviseHolder)
  340. DECLARE_DINTERFACE_TYPE(IDataObject)
  341. DECLARE_DINTERFACE_TYPE(IEnumOLEVERB)
  342. DECLARE_DINTERFACE_TYPE(IEnumFORMATETC)
  343. DECLARE_DINTERFACE_TYPE(IEnumSTATDATA)
  344. DECLARE_DINTERFACE_TYPE(ILockBytes)
  345. DECLARE_DINTERFACE_TYPE(IMalloc)
  346. DECLARE_DINTERFACE_TYPE(IMoniker)
  347. DECLARE_DINTERFACE_TYPE(IOleAdviseHolder)
  348. DECLARE_DINTERFACE_TYPE(IOleClientSite)
  349. DECLARE_DINTERFACE_TYPE(IOleContainer)
  350. DECLARE_DINTERFACE_TYPE(IOleInPlaceActiveObject)
  351. DECLARE_DINTERFACE_TYPE(IOleInPlaceFrame)
  352. DECLARE_DINTERFACE_TYPE(IOleInPlaceObject)
  353. DECLARE_DINTERFACE_TYPE(IOleInPlaceSite)
  354. DECLARE_DINTERFACE_TYPE(IOleInPlaceUIWindow)
  355. DECLARE_DINTERFACE_TYPE(IOleObject)
  356. DECLARE_DINTERFACE_TYPE(IStorage)
  357. DECLARE_DINTERFACE_TYPE(IStream)
  358. DECLARE_DINTERFACE_TYPE(ITypeInfo)
  359. DECLARE_DINTERFACE_TYPE(ICreateTypeInfo)
  360. DECLARE_DINTERFACE_TYPE(ITypeLib)
  361. DECLARE_DINTERFACE_TYPE(ICreateTypeLib)
  362. DECLARE_DINTERFACE_TYPE(IConnectionPointContainer)
  363. DECLARE_DINTERFACE_TYPE(IConnectionPoint)
  364. DECLARE_DINTERFACE_TYPE(IEnumConnectionPoints)
  365. DECLARE_DINTERFACE_TYPE(ISimpleFrameSite)
  366. DECLARE_DINTERFACE_TYPE(IPropertyBag)
  367. DECLARE_DINTERFACE_TYPE(IEnumConnections)
  368. DECLARE_DINTERFACE_TYPE(IOleControl)
  369. DECLARE_DINTERFACE_TYPE(IOleControlSite)
  370. DECLARE_DINTERFACE_TYPE(IPerPropertyBrowsing)
  371. DECLARE_DINTERFACE_TYPE(IFontDisp)
  372. DECLARE_DINTERFACE_TYPE(IPersistStreamInit)
  373. DECLARE_DINTERFACE_TYPE(IPicture)
  374. DECLARE_DINTERFACE_TYPE(IPictureDisp)
  375. DECLARE_DINTERFACE_TYPE(IPropertyPageSite)
  376. DECLARE_DINTERFACE_TYPE(IOleClientSite)
  377. DECLARE_DINTERFACE_TYPE(IErrorLog)
  378. DECLARE_DINTERFACE_TYPE(IEnumOLEVERB)
  379. DECLARE_DINTERFACE_TYPE(IOleDocumentView)
  380. DECLARE_DINTERFACE_TYPE(IOleAdviseHolder)
  381. DECLARE_DINTERFACE_TYPE(IOleContainer)
  382. DECLARE_DINTERFACE_TYPE(IOleInPlaceActiveObject)
  383. DECLARE_DINTERFACE_TYPE(IOleInPlaceObject)
  384. DECLARE_DINTERFACE_TYPE(IOleInPlaceUIWindow)
  385. DECLARE_DINTERFACE_TYPE(IOleInPlaceFrame)
  386. DECLARE_DINTERFACE_TYPE(IOleObject)
  387. DECLARE_DINTERFACE_TYPE(IOleInPlaceSite)
  388.  
  389. #pragma option pop
  390.  
  391. #endif
  392.