OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
olefacto.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectComponents
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4/// \file
5/// Templatized class implementation of OLE component creation code for
6/// TComponentFactory callback for ObjectWindows applications
7/// All templates assume an OWLGetAppDictionary() global and a Registrar global
8/// User can provide alternate implementation for any or all of the functions
9/// The user's derived TApplication class is passed as the template parameter
10//
11//----------------------------------------------------------------------------
12
13#if !defined(OCF_OLEFACTO_H)
14#define OCF_OLEFACTO_H
15
16#include <owl/private/defs.h>
17#if defined(BI_HAS_PRAGMA_ONCE)
18# pragma once
19#endif
20
21#include <ocf/defs.h>
22#include <ocf/oleutil.h>
23#include <ocf/ocreg.h>
24#include <ocf/olewindo.h>
25#include <owl/docmanag.h>
26#include <owl/appdict.h>
27#include <owl/pointer.h>
28
29namespace ocf {
30
31// Generic definitions/compiler options (eg. alignment) preceeding the
32// definition of classes
33#include <owl/preclass.h>
34
35//----------------------------------------------------------------------------
36/// Non-automated application automation callout stub implementation
37//
38template <class T> struct TOleFactoryNoAuto {
39 static void AutomateApp(T* app, owl::uint32 options);
40 static TUnknown* AutomateObject(T* app, const void* obj,const std::type_info& info);
41 static void UnregisterAutomation(T* app);
42};
43
44//
45template<class T> void
49
50//
51template<class T> TUnknown*
52TOleFactoryNoAuto<T>::AutomateObject(T*, const void*,const std::type_info&) {
53 return 0;
54}
55
56//
57template<class T> void
61
62//----------------------------------------------------------------------------
63/// Automated application default automation callout implementation
64//
65template <class T> struct TOleFactoryAuto {
66 static void AutomateApp(T* app, owl::uint32 options);
67 static TUnknown* AutomateObject(T* app, const void* obj,const std::type_info& info);
68 static void UnregisterAutomation(T* app);
69};
70
71//
72/// Default callout to aggregate an automation helper to an automated object
73//
74template <class T> TUnknown*
75TOleFactoryAuto<T>::AutomateObject(T* app,const void* obj,const std::type_info& info)
76{
77 using ::Registrar;
79 return Registrar->CreateAutoObject(obj, info, app, typeid(*app));
80}
81
82//
83/// Default callout to unregister automation active object
84//
85template <class T> void
87{
88 using ::Registrar;
90 Registrar->ReleaseAutoApp(TAutoObject<T>(app));
91}
92
93//
94/// Default callout to aggregate an automation helper to an automated app
95//
96template <class T> void
98{
99 using ::Registrar;
101 Registrar->CreateAutoApp(TAutoObjectDelete<T>(app), options);
102}
103
104//----------------------------------------------------------------------------
105/// Application creation/destruction callouts shared for doc/view and non-d/v
106//
107template<class T, class Auto> struct TOleFactoryAppBase {
108 static T* CreateApp(owl::uint32 options);
109 static void DestroyApp(T* app);
110};
111
112//
113template<class T, class Auto> T*
115{
116 T* app = new T;
117 if ((options & amEmbedding) || !(options & amExeMode))
118 app->SetCmdShow(SW_HIDE);
119 using ::Registrar;
121 app->OcInit(dynamic_cast<TOcRegistrar&>(*Registrar), options);
122 Auto::AutomateApp(app, options);
123 app->Start();
124 return app;
125}
126
127//
128template<class T, class Auto>
130{
131 Auto::UnregisterAutomation(app);
132 delete app;
133}
134
135//----------------------------------------------------------------------------
136/// Non-docview application callout implementation, no CreateObject implemented
137/// User must either provide an implementation of CreateOleObject for app class
138/// or else reimplement this template function for the particular class
139//
140template<class T, class Auto> struct TOleFactoryNoDocView
141 : TOleFactoryAppBase<T, Auto> {
142 static TUnknown* CreateObject(T* app, owl::uint32 options, owl::TRegLink* link);
143};
144
145//
146template <class T, class Auto> TUnknown*
148 owl::TRegLink* link)
149{
150 return app->CreateOleObject(options, link);
151}
152
153//----------------------------------------------------------------------------
154/// Docview application callout implementation - supplies CreateObject function
155//
156template <class T, class Auto> struct TOleFactoryDocView
157 : TOleFactoryAppBase<T, Auto> {
158 static TUnknown* CreateObject(T* app, owl::uint32 options, owl::TRegLink* link);
159};
160
161//
162template <class T, class Auto> TUnknown*
164{
165 TUnknown* obj = 0;
166 if (!link) // if not coming up embedded, we don't know what to make here
167 return 0;
168 owl::TDocManager* docMan = app->GetDocManager();
169 if (!docMan)
170 return 0;
171 owl::TDocTemplate* tpl = static_cast<owl::TDocTemplate*>(link);
172 owl::TDocument* doc = docMan->CreateDoc(tpl, 0, 0, owl::dtNewDoc | owl::dtNoAutoView);
173 owl::TView* view = tpl->ConstructView(*doc);
175 if (ow) {
176 obj = ow->CreateOcView(tpl, (options & amEmbedding) != 0, 0);
177 ocf::TUnknown* autoObj;
178 void* viewObj = dynamic_cast<void*>(view); // get address of derived most
179 void* docObj = dynamic_cast<void*>(doc);
180 if ((autoObj = Auto::AutomateObject(app, viewObj, typeid(*view))) != 0 ||
181 (autoObj = Auto::AutomateObject(app, docObj, typeid(*doc))) != 0) {
182 obj->SetOuter(&autoObj->Aggregate(*obj));
183 obj->AdjustRefCount(-1); // remove extra ref count
184 obj = autoObj;
185 obj->AdjustRefCount(+1); // add it back to the new outer sub-object
186 }
187 }
188 doc->InitView(view);
189 return (options & amEmbedding) ? obj : 0;
190}
191
192//----------------------------------------------------------------------------
193// Standard factory for OWL components, callouts supplied via template args
194//
195/// A template class, TOleFactoryBase<> creates callback code for ObjectWindows
196/// classes. The main purpose of the factory code is to provide a callback function,
197/// Create, that ObjectComponents calls to create objects.
198///
199/// Just as a recipe consists of a list of instructions about how to make a
200/// particular kind of food, a template, such as TOleFactoryBase<>, contains
201/// instructions about how to make an object, in this case, a factory object.
202/// TOleFactoryBase<> includes two public member functions. The three additional
203/// functions are passed as template arguments. Although these template arguments
204/// actually belong to the class that is instantiated when you fill in the arguments
205/// to TOleFactoryBase<>, they are described here for convenience.
206///
207/// Use TOleFactoryBase<> to manufacture objects in general, whether or not they are
208/// embedded, OLE-enabled, or use the Doc/View model. These objects might or might
209/// not be connected to OLE.
210///
211/// The callouts are supplied through the arguments passed to the template class.
212/// The factory base class takes three template parameters: the application type, a
213/// set of functions to create the object, and a set of functions to create an
214/// automation object. Depending on the arguments passed, you can make the following
215/// OLE-enabled components:
216/// - Doc/View components that are automated
217/// - Doc/View components that are not automated
218/// - Non-Doc/View components that are automated
219/// - Non-Doc/View components that are not automated
220///
221/// ObjectWindows provides a standard implementation for object creation and
222/// automation. Factory Template Classes gives an overview of these classes.
223///
224/// By using TOleFactoryBase<> to obtain an OLE interface for your application, you
225/// can make objects that are accessible to OLE. That is, TOleFactoryBase<> handles
226/// any relationships with IUnknown, a standard OLE interface.
227template <class T, class Obj> class TOleFactoryBase {
228 public:
229/// Converts the object into a pointer to the factory. ObjectComponents uses this
230/// pointer to create the object.
231 operator TComponentFactory() {return Create;}
232 static IUnknown* Create(IUnknown* outer, owl::uint32 options, owl::uint32 id);
233};
234
235/*
236//
237template <class T, class Obj>
238TOleFactoryBase<T, Obj>::operator TComponentFactory()
239{
240 return Create;
241}
242*/
243
244//
245// Main Create callback function called to create app and/or object
246//
247/// A TComponentFactory callback function that creates or destroys the application
248/// or creates objects. If an application object does not already exist, Create
249/// creates a new one. The outer argument points to the OLE 2 IUnknown interface
250/// with which this object aggregates itself. If outer is 0, If outer is 0, the
251/// object will become an independent object.
252/// The options argument indicates the application's mode while it is running. The
253/// values for options are either set from the command line or set by
254/// ObjectComponents. They are passed in by the Registrar to this callback. The
255/// application looks at these flags in order to know how to operate, and the
256/// factory callback looks at them in order to know what to do. For example, a value
257/// of amExeMode indicates that the server is running as an .EXE either because it
258/// was built as an .EXE or because it is a .DLL that was launched by an .EXE stub
259/// and is now running as an executable program. The TOcAppMode enum description
260/// shows the possible values for the options argument.
261/// If the application already exists and the object ID (id) equals 0, Create
262/// returns the application's OLE interface. Otherwise, it calls OCInit to create a
263/// new TOcApp and register the options from TOcAppMode enum, which contains
264/// OLE-related flags used in the application's command line. (These flags tell the
265/// application whether it has been run as a server, whether it needs to register
266/// itself, and so on.) If a component ID was passed, that becomes the component;
267/// otherwise, Create runs the application itself based on the values of the
268/// TOcAppMode enum and returns the OLE interface.
269///
270/// \image html bm59.BMP
271template <class T, class Obj> IUnknown*
273{
274 owl::TRegLink* link = reinterpret_cast<owl::TRegLink*>(id);
275
276 // Look for the app object for this process, creating one if necessary
277 //
279 T* app;
280 if (!existingApp) {
281 if (options & amShutdown) // app already destructed
282 return 0;
283 app = Obj::CreateApp(options);
284 if (!link && !(options & amEmbedding)) {
285 Obj::CreateObject(app, options, link);
286 }
287 } else {
288 app = TYPESAFE_DOWNCAST(existingApp, T);
289 if (options & amShutdown) {
290 Obj::DestroyApp(app);
291 return 0; // any interface present when passed in has now been released
292 }
293 }
294 TUnknown* obj = app->OcApp; // app's COM interface, used if not doc object
295
296 // If a component ID was passed, make that component, otherwise return app
297 //
298 if (link) {
299 TUnknown* doc = Obj::CreateObject(app, options, link);
300 if (doc)
301 obj = doc;
302 else if (!(options & amEmbedding)) // run DLL in ExeMode from doc factory
303 app->OcApp->SetOption(amServedApp, true);
304 else
305 return 0; // doc factory failed
306 }
307 IUnknown* ifc = obj ? obj->SetOuter(outer) : 0; // aggregate to passed outer
308
309 // Run message look if ExeMode, either EXE server or DLL server force to run
310 // EXE servers come through here twice, no Run if 2nd pass from factory call
311 //
312 if (options & amRun)
313 app->Run();
314
315 return ifc;
316}
317
318//----------------------------------------------------------------------------
319/// Factory for OWL non-Doc/View, non-automated OLE components
320//
321template <class T> class TOleFactory
322: public TOleFactoryBase<T, TOleFactoryNoDocView<T, TOleFactoryNoAuto<T> > >
323{
324};
325
326//
327/// Factory for OWL Doc/View, non-automated OLE components
328//
329template <class T> class TOleDocViewFactory
330: public TOleFactoryBase<T, TOleFactoryDocView<T, TOleFactoryNoAuto<T> > >
331{
332};
333
334//
335/// Factory for OWL non-Doc/View, automated OLE components
336//
337template <class T> class TOleAutoFactory
338: public TOleFactoryBase<T, TOleFactoryNoDocView<T, TOleFactoryAuto<T> > >
339{
340};
341
342//
343/// Factory for OWL Doc/View, automated OLE components
344//
345template <class T> class TOleDocViewAutoFactory
346: public TOleFactoryBase<T, TOleFactoryDocView<T, TOleFactoryAuto<T> > >
347{
348};
349
350//----------------------------------------------------------------------------
351// Factory for OWL automated OLE components, no linking/embedding support
352//
353
354/// A template class, TAutoFactory<> creates callback code for ObjectWindows
355/// classes. The application class is passed as the argument to the template. By
356/// itself, TAutoFactory<> does not provide linking or embedding support for
357/// ObjectWindows automated applications.
358///
359/// Although TAutoFactory<> simplifies the process of creating the callback
360/// function, you can write your own callback function or provide alternate
361/// implementation for any or all of TAutoFactory<>'s functions.
362template <class T> class TAutoFactory {
363 public:
364/// Converts the object into a pointer to the factory. ObjectComponents uses this
365/// pointer to create the automated object.
366 operator TComponentFactory() {return Create;}
367
368 // Callouts to allow replacement of individual creation steps
369 //
370 static T* CreateApp(owl::uint32 options);
371 static void DestroyApp(T* app);
372
373 // Main Create callback function called to create app and/or object
374 //
375 static IUnknown* Create(IUnknown* outer, owl::uint32 options, owl::uint32 id);
376};
377
378/*
379//
380template <class T>
381TAutoFactory<T>::operator TComponentFactory()
382{
383 return Create;
384}
385*/
386
387//
388/// Called when the app is not found and needs to be created
389//
390/// CreateApp creates a new automated application. By default, it creates a new
391/// application of template type T with no arguments. The options argument is one of
392/// the TOcAppMode enum values, for example, amRun, amAutomation, and so on that
393/// indicate the application's mode when running.
394//
395template <class T> T*
397{
398 T* app = new T;
399 if ((options & amEmbedding) || !(options & amExeMode))
400 app->nCmdShow = SW_HIDE;
401 app->Start();
402 return app;
403}
404
405//
406/// Destroys the previously created application referred to in app.
407//
408template <class T> void
410{
411 delete app;
412}
413
414//
415/// Create is a TComponentFactory callback function that creates or destroys the
416/// application or creates objects. If an application object does not already exist,
417/// Create creates a new one. The outer argument points to the OLE 2 IUnknown
418/// interface with which this object aggregates itself. If outer is 0, the new
419/// object is not aggregated, or it will become the main object.
420/// The options argument indicates the application's mode while it is running. The
421/// values for options are either set from the command line or set by
422/// ObjectComponents. They are passed in by the "Registrar" to this callback. The
423/// application looks at these flags to know how to operate, and the factory
424/// callback looks at them to know what to do. For example, a value of amExeMode
425/// indicates that the server is running as an .EXE either because it was built as
426/// an .EXE or because it is a .DLL that was launched by an .EXE stub and is now
427/// running as an executable program. See TOcAppMode enum for a description of the
428/// possible values for the options argument.
429/// If the application already exists, Create returns the application's OLE
430/// interface and registers the options from TOcAppMode enum which contains
431/// OLE-related flags used in the application's command line. For example, the
432/// amAutomation flag tells the server to register itself as a single-user
433/// application. (In general, these flags tell the application whether it has been
434/// run as a server, whether it needs to register itself, and so on.)
435/// The id argument, which is not used for TAutoFactory, is always 0.
436///
437/// \image html bm59.BMP
438//
439template <class T> IUnknown*
440TAutoFactory<T>::Create(IUnknown* outer, owl::uint32 options, owl::uint32 /*id*/)
441{
442 IUnknown* ifc = 0;
444 T* app;
445 if (!existingApp) {
446 if (options & amShutdown) // app already destructed
447 return 0;
448 app = CreateApp(options);
449 }
450 else {
451 app = TYPESAFE_DOWNCAST(existingApp, T);
452 if (options & amShutdown) {
453 DestroyApp(app);
454 return (options & amServedApp) ? 0 : outer;
455 }
456 }
457
458 if (options & amServedApp) {
459 using ::Registrar;
461 TUnknown* obj = Registrar->CreateAutoApp(TAutoObjectDelete<T>(app),
462 options, outer);
463 ifc = *obj; // does an AddRef, reference count owned by container
464 }
465
466 if (options & amRun) {
467 app->Run();
468 }
469 return ifc;
470}
471
472// Generic definitions/compiler options (eg. alignment) following the
473// definition of classes
474#include <owl/posclass.h>
475
476
477} // OCF namespace
478
479
480#endif // OWL_OLEFACTO_H
Definition of class TAppDictionary.
#define CHECK(condition)
Definition checks.h:239
A template class, TAutoFactory<> creates callback code for ObjectWindows classes.
Definition olefacto.h:362
static void DestroyApp(T *app)
Destroys the previously created application referred to in app.
Definition olefacto.h:409
static T * CreateApp(owl::uint32 options)
Called when the app is not found and needs to be created.
Definition olefacto.h:396
static IUnknown * Create(IUnknown *outer, owl::uint32 options, owl::uint32 id)
Create is a TComponentFactory callback function that creates or destroys the application or creates o...
Definition olefacto.h:440
holders for C++ object pointers for automation conversions
Definition autodefs.h:1120
Linking & embeding version of the Registrar.
Definition ocapp.h:109
Factory for OWL non-Doc/View, automated OLE components.
Definition olefacto.h:339
Factory for OWL Doc/View, automated OLE components.
Definition olefacto.h:347
Factory for OWL Doc/View, non-automated OLE components.
Definition olefacto.h:331
A template class, TOleFactoryBase<> creates callback code for ObjectWindows classes.
Definition olefacto.h:227
static IUnknown * Create(IUnknown *outer, owl::uint32 options, owl::uint32 id)
A TComponentFactory callback function that creates or destroys the application or creates objects.
Definition olefacto.h:272
Factory for OWL non-Doc/View, non-automated OLE components.
Definition olefacto.h:323
The generic OLE2 window. Use as a client of a frame window.
Definition olewindo.h:91
virtual TOcView * CreateOcView(owl::TRegLink *link, bool isRemote, IUnknown *outer)
Called to perform the actual setting up of the OcView member.
Definition olewindo.cpp:307
Standard implementation of a controlling IUnknown for an object, to be inherited with other COM inter...
Definition oleutil.h:264
IUnknown * SetOuter(IUnknown *outer=0)
Definition oleutil.h:298
unsigned long AdjustRefCount(int i)
Definition oleutil.h:314
IUnknown & Aggregate(TUnknown &inner)
Definition oleutil.cpp:243
TApplication * GetApplication(uint pid=0)
Looks up and returns the application associated with a given process ID.
Definition appdict.cpp:192
Derived from TModule and TMsgThread and virtually derived from TEventHandler, TApplication acts as an...
Definition applicat.h:141
TDocManager creates a document manager object that manages the list of current documents and register...
Definition docmanag.h:100
virtual TDocument * CreateDoc(TDocTemplate *tpl, LPCTSTR path, TDocument *parent=nullptr, long flags=0)
CreateDoc creates a document based on the directory path and the specified template.
Definition docmanag.cpp:603
TDocTemplate is an abstract base class that contains document template functionality.
Definition doctpl.h:54
virtual TView * ConstructView(TDocument &doc)=0
A pure virtual function that must be defined in a derived class, ConstructView creates the view speci...
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
Definition docview.h:187
TView * InitView(TView *view)
called from template InitView
Definition document.cpp:502
Abstract base class for view access from document.
Definition docview.h:397
Definition of class TDocManager.
const uint dtNewDoc
create new document, no path used
Definition doctpl.h:218
const uint dtNoAutoView
no automatic create of default view
Definition doctpl.h:220
Object Component Framework (COM encapsulation)
Definition appdesc.h:22
IUnknown *(* TComponentFactory)(IUnknown *outer, owl::uint32 options, owl::uint32 id)
Definition appdesc.h:35
@ amShutdown
set in factory call to shutdown/delete app
Definition ocreg.h:93
@ amServedApp
per instance flag, app refcnt held by container
Definition ocreg.h:89
@ amExeMode
may be overridden per instance if running DLL
Definition ocreg.h:88
@ amEmbedding
cmdline, overridden per Instance if embedded DLL
Definition ocreg.h:82
@ amRun
set in factory call to run application msg loop
Definition ocreg.h:92
unsigned long uint32
Definition number.h:34
TAppDictionary & OWLGetAppDictionary()
Global exported TAppDictionary in Owl.
Definition appdict.cpp:35
ObjectComponents fundamental definitions.
OLE Registration definitions.
owl::TPointer< ocf::TRegistrar > Registrar
Global registrar object defined by the client application.
Low level OLE Utility class definitions.
TOleWindow - Class encapsulating a window which can be an OLE container or server window.
#define TYPESAFE_DOWNCAST(object, toClass)
Definition defs.h:269
Various types of smart pointer templatized classes.
Application creation/destruction callouts shared for doc/view and non-d/v.
Definition olefacto.h:107
static T * CreateApp(owl::uint32 options)
Definition olefacto.h:114
static void DestroyApp(T *app)
Definition olefacto.h:129
Automated application default automation callout implementation.
Definition olefacto.h:65
static void AutomateApp(T *app, owl::uint32 options)
Default callout to aggregate an automation helper to an automated app.
Definition olefacto.h:97
static TUnknown * AutomateObject(T *app, const void *obj, const std::type_info &info)
Default callout to aggregate an automation helper to an automated object.
Definition olefacto.h:75
static void UnregisterAutomation(T *app)
Default callout to unregister automation active object.
Definition olefacto.h:86
Docview application callout implementation - supplies CreateObject function.
Definition olefacto.h:157
static TUnknown * CreateObject(T *app, owl::uint32 options, owl::TRegLink *link)
Definition olefacto.h:163
Non-automated application automation callout stub implementation.
Definition olefacto.h:38
static TUnknown * AutomateObject(T *app, const void *obj, const std::type_info &info)
Definition olefacto.h:52
static void UnregisterAutomation(T *app)
Definition olefacto.h:58
static void AutomateApp(T *app, owl::uint32 options)
Definition olefacto.h:46
Non-docview application callout implementation, no CreateObject implemented User must either provide ...
Definition olefacto.h:141
static TUnknown * CreateObject(T *app, owl::uint32 options, owl::TRegLink *link)
Definition olefacto.h:147