InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
UIDRef.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: ?
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 
24 #ifndef __UIDREF__
25 #define __UIDREF__
26 
27 #include "IPMUnknown.h"
28 
29 class IDataBase;
30 
43 class UIDRef
44 {
45  public:
50 
51  /* UIDRef's constructed without parameters are invalid. They can be used, but
52  attempts to instantiate them will fail.
53  */
54  UIDRef() :
55  fDB(nil), fUID(kInvalidUID)
56  {}
57 
60  UIDRef(IDataBase *db, UID id) :
61  fDB(db), fUID(id)
62  {}
63 
69  { return fDB; }
70 
75  UID GetUID() const
76  { return fUID; }
77 
83  void ResetUID(UID newUID)
84  { fUID = newUID; }
85 
86 // Don't remove these next two. Even though we are just POD (plain old data) and
87 // seem unnecessary (and slow). Otherwise Metrowerks will generate horrendous code
88 // for the operator! and operator void* below. Yuck.
94  bool16 operator==(const UIDRef& other) const
95  { //An open question here--there are 3 ways to have an invalid UIDRef. Should any invalid UIDRef compare equal to any other?
96  //Assert for now that we only use "doubly-invalid" invalid UIDRefs when comparing and see what shows up.
97 
98  //ASSERT( fDB || fUID == kInvalidUID);
99  //ASSERT( other.fDB || other.fUID == kInvalidUID);
100 // ASSERT( fDB == other.fDB || fUID != kInvalidUID || other.fUID != kInvalidUID);
101 
102 // ASSERT( fDB == nil && fUID == kInvalidUID || fDB != nil && fUID != kInvalidUID);
103 // ASSERT( other.fDB == nil && other.fUID == kInvalidUID || other.fDB != nil && other.fUID != kInvalidUID);
104  return fUID == other.fUID && fDB == other.fDB;
105  }
111  bool16 operator!=(const UIDRef& other) const
112  { return !(*this == other); }
113 
114 
120  bool16 operator<(const UIDRef& other) const
121  { return fDB == other.fDB ? fUID < other.fUID : fDB < other.fDB; }
122 
123 // The next two functions allow testing of UIDRefs in conditionals, e.g. :
124 //
125 // UIDRef myUIDRef(aDB, aUID);
126 // ...
127 // if(!myUIDRef) {
128 // myUIDRef = giveMeAnotherUIDRef();
129 // }
130 // ASSERT_MSG(myUIDRef, "nil");
131 //
132 
137  bool16 operator!() const; // true if bad
142  operator void*() const; // nonzero if good
143 
144 
164  bool16 ExistsInDB() const;
165 
172  IPMUnknown* Instantiate( PMIID iid ) const ;
173 
174 public:
175 
176 // For convenience we provide a null UIDRef. E.g.:
177 // UIDRef myUID(UIDRef::gNull);
178 
179  static const UIDRef gNull;
180 
181 // Debugger cannot handle long symbol names
182 // disable warning again
183 #ifdef WIN32
184 
185 #endif // WIN32
186 
187 private:
188  IDataBase *fDB;
189  UID fUID;
190 };
191 
193 extern const UIDRef kInvalidUIDRef;
194 
195 inline bool16 UIDRef::operator!() const
196 {
197  //return *this == UIDRef::gNull;
198  return fDB == nil || fUID == kInvalidUID;
199 }
200 
201 inline UIDRef::operator void*() const
202 {
203  // Is there a cleaner way to do this?
204  //return *this == UIDRef::gNull ? nil : const_cast<UIDRef*>(this);
205 
206  // Require both members to be valid:
207  return (fDB == nil || fUID == kInvalidUID) ? nil : const_cast<UIDRef*>(this);
208 }
209 
210 inline bool16 UIDRef::ExistsInDB() const
211 {
212  if (!(*this)) // use operator! for validity test
213  return kFalse;
214  else
215  return (fDB->GetClass(fUID) != kInvalidClass ? kTrue : kFalse);
216 }
217 
219 {
220  return ( fDB ? fDB->Instantiate( fUID, iid ) : nil ) ;
221 }
222 
223 #endif // __UIDREF__