OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
appdict.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TAppDictionary, a dictionary of associations
7/// between pids (Process IDs) and TApplication pointers.
8/// Used to support GetApplicationObject().
9//----------------------------------------------------------------------------
10#include <owl/pch.h>
11#include <owl/appdict.h>
12#include <owl/applicat.h>
13#if defined(BI_APP_DLL)
14# if !defined(__CYGWIN__) && !defined(WINELIB)
15# include <dos.h>
16# endif
17# include <string.h>
18#endif
19
20#if defined(__BORLANDC__)
21# pragma option -w-ccc // Disable "Condition is always true/false"
22#endif
23
24namespace owl {
25
27
28// Defining LOCALALLOC_TABLE instructs OWL to use LocalAlloc for the table
29//
30//#define LOCALALLOC_TABLE
31
32/// Global dictionary used by OWL for EXE Application lookup
33//
34_OWLFUNC(TAppDictionary&)
40
41//----------------------------------------------------------------------------
42
43//
44/// Dictionary implementation used to associate Pids (hTasks) with running Owl
45/// apps when Owl is in a DLL or used by a DLL. 32bit only needs this when
46/// running win32s (no per-instance data) since per-instance data makes it
47/// unnecesasry
48//
49
50# if defined(BI_COMP_BORLANDC)
51# pragma warn -inl
52# endif
53
54/// \addtogroup internal
55/// @{
56//
57/// Abstract Base for dictionary implementation
58//
59class TAppDictImp {
60 public:
61 virtual ~TAppDictImp() {}
62
63 virtual void Add(unsigned pid, TApplication* app) = 0;
64 virtual void Remove(unsigned pid) = 0;
65 virtual TAppDictionary::TEntry* Lookup(unsigned pid) = 0;
66 virtual TAppDictionary::TEntry* Lookup(TApplication* app) = 0;
67 virtual void Iterate(TAppDictionary::TEntryIterator) = 0;
68 virtual int GetCount() const = 0;
69};
70
71//
72/// Fast, small, per-instance data based Dictionary implementation (32bit only)
73//
74class TAppDictInstImp : public TAppDictImp {
75 public:
76 TAppDictInstImp() {Entry.App = nullptr;}
77
78 void Add(unsigned, TApplication* app) {Entry.App = app;}
79 void Remove(unsigned) {Entry.App = nullptr;}
80 TAppDictionary::TEntry* Lookup(unsigned) {return &Entry;}
81 TAppDictionary::TEntry* Lookup(TApplication*) {return &Entry;}
83 {(*iter)(Entry);}
84 int GetCount() const {return Entry.App ? 1 : 0;}
85
86 private:
87 TAppDictionary::TEntry Entry;
88};
89# if defined(BI_COMP_BORLANDC)
90# pragma warn .inl
91# endif
92
93/// @}
94
95//} // OWL namespace
96
97//----------------------------------------------------------------------------
98/// \class TAppDictionary
99/// TAppDictionary implementation for DLLs only. EXE version is all inline.
100/// Flat model must implement here, not inline, because same lib is used by DLLs
101///
102/// A TAppDictionary is a dictionary of associations between a process ID and an
103/// application object. A process ID identifies a process: a program (including all
104/// its affiliated code, data, and system resources) that is loaded into memory and
105/// ready to execute. A TAppDictionary object supports global application lookups
106/// using the global GetApplicationObject function or TAppDictionary's
107/// GetApplication function. If you do not define an application dictionary,
108/// ObjectWindows provides a default, global application dictionary that is
109/// exported. In fact, for .EXEs, this global application dictionary is
110/// automatically used.
111///
112/// TAppDictionary includes a TEntry struct, which stores a process ID and the
113/// corresponding application object associated with the ID. The public member
114/// functions add, find, and remove the entries in the application dictionary.
115///
116/// If you are statically linking ObjectWindows, you do not have to explicitly
117/// create an application dictionary because the default global ObjectWindows
118/// application dictionary is used. However, when writing a DLL component that is
119/// using ObjectWindows in a DLL, you do need to create your own dictionary. To make
120/// it easier to define an application dictionary, ObjectWindows includes a macro
121/// DEFINE_APP_DICTIONARY, which automatically creates or references the correct
122/// dictionary for your application.
123///
124/// Although this class is transparent to most users building EXEs, component DLLs
125/// need to create an instance of a TApplication class for each task that they
126/// service. This kind of application differs from an .EXE application in that it
127/// never runs a message loop. (All the other application services are available,
128/// however.)
129///
130/// Although a component may consist of several DLLs, each with its own TModule, the
131/// component as a whole has only one TApplication for each task. A TAppDictionary,
132/// which is used for all servers (including DLL servers) and components, lets users
133/// produce a complete, self-contained application or component. By using a
134/// TAppDictionary, these components can share application objects.
135///
136/// When 16-bit ObjectWindows is statically linked with an .EXE or under Win32, with
137/// per- instance data, the TAppDictionary class is implemented as a wrapper to a
138/// single application pointer. In this case, there is only one TApplication that
139/// the component ever sees.
140///
141/// To build a component DLL using the ObjectWindows DLL, a new TAppDictionary
142/// object must be constructed for that DLL. These are the steps an application must
143/// follow in order to associate the component DLL with the TAppDictionary, the
144/// application, and the window class hierarchy:
145/// 1. Use the DEFINE_APP_DICTIONARY macro to construct an instance of
146/// TAppDictionary. Typically, this will be a static global in one of the
147/// application's modules (referred to as "AppDictionary"). The DEFINE_DICTIONARY
148/// macro allows the same code to be used for EXEs and DLLs.
149/// DEFINE_APP_DICTIONARY(AppDictionary);
150/// 2. Construct a generic TModule and assign it to the global ::Module. This is
151/// the default provided in the ObjectWindows' LibMain function.
152/// LibMain(...)
153/// ::Module = new TModule(0, hInstance);
154/// 3. When each TApplication instance is constructed, pass a pointer to the
155/// TAppDictionary as the last argument to ensure that the application will insert
156/// itself into this dictionary. In addition, for 16 bit DLLs, the global module argument
157/// needs to be supplied with a placeholder value because the Module construction
158/// has already been completed at this point, as a result of the process performed
159/// in step 2 above.
160/// TApplication* app = new TMyApp(..., app, AppDictionary);
161/// 4. If the Doc/View model is used, supply the application pointer when
162/// constructing the TDocManager object.
163/// SetDocManager(new TDocManager(mode, this));
164/// 5. When a non-parented window (for example, the main window) is being
165/// constructed, pass the application as the module.
166/// SetMainWindow(new TFrameWindow(0, "", false, this));
167
168//
169/// Application dictionary constructor
170//
172{
173 Imp = new TAppDictInstImp(); // Could also use this case if linked to exe
174}
175
176//
177/// Destroys the TAppDictionary object and calls DeleteCondemned to clean up the
178/// condemned applications.
179//
181{
183 delete Imp;
184}
185
186//
187/// Looks up and returns the application associated with a given process ID. The
188/// default ID is the ID of the current process. If no application is associated
189/// with the process ID, GetApplication returns 0.
190//
193{
194 if (!pid)
196 TAppDictionary::TEntry* entry = Imp->Lookup(pid);
197 return entry ? entry->App : nullptr;
198}
199
200//
201/// Adds an application object (app) and corresponding process ID to this
202/// dictionary. The default ID is the current process's ID.
203//
204void
206{
207 if (!pid)
209 Imp->Add(pid, app);
210}
211
212//
213/// Searches for the dictionary entry using the specified application (app). Then
214/// removes a given application and process ID entry from this dictionary, but does
215/// not delete the application object.
216//
217void
219{
220 TAppDictionary::TEntry* entry = Imp->Lookup(app);
221 if (entry) {
222 entry->App = nullptr;
223 entry->Pid = 0;
224 }
225}
226
227//
228/// Searches for the dictionary entry using the specified process ID (pid). Then
229/// removes a given application and its associated process ID entry from this
230/// dictionary, but does not delete the application.
231//
232void
234{
235 TAppDictionary::TEntry* entry = Imp->Lookup(pid);
236 if (entry) {
237 entry->App = nullptr;
238 entry->Pid = 0;
239 }
240}
241
242//
243/// Marks an application in this dictionary as condemned by zeroing its process ID
244/// so that the application can be deleted later when DeleteCondemned is called.
245//
246void
248{
249 TAppDictionary::TEntry* entry = Imp->Lookup(app);
250 if (entry)
251 entry->Pid = 0;
252}
253
254//
255//
256//
257static void _DeleteCondemnedIter(TAppDictionary::TEntry& entry)
258{
259 if (!entry.Pid) {
260 delete entry.App;
261 entry.App = nullptr;
262 }
263}
264
265//
266/// Iterates through the entries in the application dictionary, calling the iter
267/// callback function for each entry.
268//
269void
274
275//
276/// Deletes all condemned applications from the dictionary. If no applications
277/// remain in the dictionary, DeleteCondemned returns true.
278//
279bool
281{
282 Imp->Iterate(_DeleteCondemnedIter);
283 return Imp->GetCount() == 0;
284}
285
286} // OWL namespace
287//
288/// Exported entry for Debugger use
289//
290extern "C" _OWLFUNC(owl::TApplication*)
295
296namespace owl {
297
299{
301// CHECK(app);
302 WARN(app == 0, _T("OWLGetAppDictionary().GetApplication(pid) returned NULL"));
303 if (app)
304 return app;
305
306 // Make alias app (will add itself to dictionary) because OWL always needs an
307 // app around. If the app is non-OWL, no TApplication will have been
308 // constructed.
309 // Override default constructor argument to prevent overwrite of ::Module,
310 // and pass the default dictionary.
311 //
313 return new TApplication(_T("ALIAS"), tempModule, &(OWLGetAppDictionary()));
314}
315
316} // OWL namespace
317
owl::TApplication * GetTaskApplicationObject(unsigned pid)
Exported entry for Debugger use.
Definition appdict.cpp:291
Definition of class TAppDictionary.
Definition of class TApplication.
#define WARN(condition, message)
Definition checks.h:273
TAppDictionary implementation for DLLs only.
Definition appdict.h:30
void(* TEntryIterator)(TEntry &)
A dictionary iterator function pointer type that receives a reference to an entry.
Definition appdict.h:44
~TAppDictionary()
Destroys the TAppDictionary object and calls DeleteCondemned to clean up the condemned applications.
Definition appdict.cpp:180
void Condemn(TApplication *app)
Marks an application in this dictionary as condemned by zeroing its process ID so that the applicatio...
Definition appdict.cpp:247
TApplication * GetApplication(uint pid=0)
Looks up and returns the application associated with a given process ID.
Definition appdict.cpp:192
TAppDictionary()
Application dictionary constructor.
Definition appdict.cpp:171
bool DeleteCondemned()
Deletes all condemned applications from the dictionary.
Definition appdict.cpp:280
void Iterate(TEntryIterator iter)
Iterates through the entries in the application dictionary, calling the iter callback function for ea...
Definition appdict.cpp:270
void Remove(TApplication *app)
Searches for the dictionary entry using the specified application (app).
Definition appdict.cpp:218
void Add(TApplication *app, uint pid=0)
Adds an application object (app) and corresponding process ID to this dictionary.
Definition appdict.cpp:205
Derived from TModule and TMsgThread and virtually derived from TEventHandler, TApplication acts as an...
Definition applicat.h:141
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
#define _T(x)
Definition cygwin.h:51
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TApplication * GetApplicationObject(uint pid=0)
Global function that calls GetApplication() on owl's app-dictionary.
Definition appdict.cpp:298
TAppDictionary & OWLGetAppDictionary()
Global exported TAppDictionary in Owl.
Definition appdict.cpp:35
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned int uint
Definition number.h:25
#define _OWLFUNC(p)
Definition defs.h:341
Definition of class TString, a flexible universal string envelope class.
An application dictionary entry that associates a process ID (Pid) with an application (App).
Definition appdict.h:36
TApplication * App
Definition appdict.h:38