InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HelperInterface.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Robin_Briggs
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //
23 // The purpose of HelperInterface is to make it easier to define
24 // the functions from IPMUnknown that are required for every interface.
25 // HelperInterface defines a class, HelperInterface, that implements
26 // QueryInterface, AddRef, and Release. HelperInterface is designed to
27 // be included in the implementation class of an interface as a member
28 // variable. HelperInterface defines a set of macros that allow a
29 // mapping from the QueryInterface, AddRef, and Release in the implementation
30 // class, and the ones in HelperInterface.
31 //
32 // How to use HelperInterface:
33 // 1. In the class's declaration, include DECLARE_HELPER_METHODS.
34 // 2. Where the class is defined, include DEFINE_HELPER_METHODS.
35 // 3. In the constructor for the class, include INIT_HELPER_METHODS.
36 //
37 // Example:
38 // class Foo : public IFoo {
39 // public:
40 // Foo(IPMUnknown *boss);
41 // void Bar();
42 //
43 //
44 // DEFINE_HELPER_METHODS(Foo)
45 //
46 // Foo :: Foo(IPMUnknown *boss) :
47 // INIT_HELPER_METHODS(boss)
48 //
49 //========================================================================================
50 
51 #ifndef __HelperInterface__
52 #define __HelperInterface__
53 
54 #include "InterfaceFactory.h"
55 #include "IPMPersist.h"
56 #include "PersistUtils.h"
57 #include "IViewObjectChanges.h"
58 #include "IControllingUnknown.h"
59 #include "DBChangesMonitorDisabler.h"
60 class ObjectModel;
61 
69 {
70  friend class ObjectModelBase;
71 
72 public:
75 #ifdef DEBUG
76  fInterfaceRefCount(0),
77 #endif
78  fBoss(boss)
79  {}
80 
85  IPMUnknown *QueryInterface(PMIID interfaceID) const
86  {
87 #if DEBUG
88  if (interfaceID == IID_IINTERFACEREFCOUNT)
89  return (IPMUnknown *)fInterfaceRefCount;
90 #endif
91  if (interfaceID == IID_ICONTROLLINGUNKNOWN && fBoss)
92  {
93  fBoss->AddRef();
94  return fBoss;
95  }
96 
97  return fBoss ? fBoss->QueryInterface(interfaceID) : nil;
98  }
103  void AddRef() const
104  {
105  if (fBoss)
106  {
107 #if DEBUG
108  fInterfaceRefCount++;
109 #endif
110  fBoss->AddRef();
111  }
112  }
117  void Release() const
118  {
119  if (fBoss)
120  {
121 #if DEBUG
122  fInterfaceRefCount--;
123 #endif
124  fBoss->Release();
125  }
126  }
127 
132  {
133  return static_cast<IControllingUnknown*>(fBoss);
134  }
135 
136 private:
137  IPMUnknown *fBoss;
138 
139  void SetBoss(IPMUnknown *boss)
140  { fBoss = boss; }
141 
142 #ifdef DEBUG
143 public:
144  void AddRef(IPMUnknown* whichInterface) const
145  { if (fBoss) ((IControllingUnknown*)fBoss)->DebugAddRef(whichInterface); }
146  void Release(IPMUnknown* whichInterface) const
147  { if (fBoss) ((IControllingUnknown*)fBoss)->DebugRelease(whichInterface); }
148  // This is here for debugging. Most bosses are PMControllingUnknown,
149  // but that's not guaranteed.
150  mutable size_t fInterfaceRefCount;
151 #endif
152 };
153 
154 
159 #define \
160 public: \
161  IPMUnknown *QueryInterface(PMIID interfaceID) const; \
162  void AddRef() const; \
163  void Release() const; \
164  void PreDirty(ImplementationID prop = kInvalidImpl, bool16 allowModification = kTrue); \
165  void PreDirtyNoMessage(ImplementationID prop = kInvalidImpl, bool16 allowModification = kTrue); \
166 protected: \
167  HelperInterface fHelperInterface;
168 
169 #define DECLARE_VIEW_HELPER_METHODS() \
170 public: \
171  /*fViewIFaceConsistencyChecker is used only to produce compiler errors \
172  if people use the wrong helper method declaraton type \
173  while using CREATE_VIEW_PMINTERFACE*/ \
174  void* fViewIFaceConsistencyChecker;\
175 
176 
177 #define DECLARE_PERSIST_DONTSNAPHOT_HELPER_METHODS() \
178 public: \
179  /*fDontSnapshotIFaceConsistencyChecker is used only to produce compiler errors \
180  if people use the wrong helper method declaraton type \
181  while using CREATE_PERSIST_DONTSNAPSHOT_PMINTERFACE*/ \
182  void* fDontSnapshotIFaceConsistencyChecker;\
183 
184 
190 #ifndef DEBUG
191 #define DEFINE_BASIC_HELPER_METHODS(cn) \
192  IPMUnknown * cn::QueryInterface( PMIID interfaceID) const \
193  {return fHelperInterface.QueryInterface(interfaceID); } \
194  void cn::AddRef() const \
195  { fHelperInterface.AddRef(); } \
196  void cn::Release() const\
197  { fHelperInterface.Release(); }
198 #else
199 #define DEFINE_BASIC_HELPER_METHODS(cn) \
200  IPMUnknown * cn::QueryInterface( PMIID interfaceID) const \
201  { \
202  if (interfaceID == IID_IGETINTERFACEIMPL) \
203  {return const_cast<cn*>(this);} \
204  return fHelperInterface.QueryInterface(interfaceID); \
205  } \
206  void cn::AddRef() const \
207  { fHelperInterface.AddRef(); } \
208  void cn::Release() const \
209  { fHelperInterface.Release(); }
210 #endif
211 
212 #define DEFINE_HELPER_METHODS(cn) \
213 DEFINE_BASIC_HELPER_METHODS(cn)\
214 void cn::PreDirty(ImplementationID prop, bool16 allowModification) \
215  { ::PreDirty(fHelperInterface.GetBoss(), this, prop, allowModification); } \
216 void cn::PreDirtyNoMessage(ImplementationID prop, bool16 allowModification) \
217  { ::PreDirtyNoMessage(fHelperInterface.GetBoss(), this, prop, allowModification); }
218 
219 #define DEFINE_VIEW_HELPER_METHODS(cn) \
220 DEFINE_BASIC_HELPER_METHODS(cn)\
221 void cn::PreDirty(ImplementationID prop, bool16 allowModification) \
222  {\
223  InterfacePtr<IViewObjectChanges>viewObjectChanges (this, UseDefaultIID());\
224  if (viewObjectChanges)\
225  viewObjectChanges->SetDirty(fHelperInterface.GetBoss(), this, prop, allowModification);\
226  } \
227 void cn::PreDirtyNoMessage(ImplementationID prop, bool16 allowModification) \
228  { ASSERT_FAIL("Not appropriate for ViewInterfaces"); }
229 
230 #define DEFINE_PERSIST_DONTSNAPHOT_HELPER_METHODS(cn) \
231 DEFINE_BASIC_HELPER_METHODS(cn)\
232 void cn::PreDirty(ImplementationID prop, bool16 allowModification) \
233  {\
234  IDataBase* db = ::GetDataBase(fHelperInterface.GetBoss());\
235  DBChangesMonitorDisabler disabler(db);\
236  ::PreDirty(fHelperInterface.GetBoss(), this, prop, allowModification); \
237  } \
238 void cn::PreDirtyNoMessage(ImplementationID prop, bool16 allowModification) \
239  {\
240  IDataBase* db = ::GetDataBase(fHelperInterface.GetBoss());\
241  DBChangesMonitorDisabler disabler(db);\
242  ::PreDirtyNoMessage(fHelperInterface.GetBoss(), this, prop, allowModification);\
243  }
244 
248 #define HELPER_METHODS_INIT(boss) fHelperInterface(boss)
249 
252 #define DONTSNAPSHOT_HELPER_METHODS_INIT(boss) HELPER_METHODS_INIT(boss), fDontSnapshotIFaceConsistencyChecker(nil)
253 
254 #endif // __HelperInterface__