InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Invariant.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Jesse Jones (jejones)
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 // ADOBE CONFIDENTIAL
24 //
25 // Purpose: Misc debugging features
26 //
27 //========================================================================================
28 
29 #ifndef __INVARIANT__H__
30 #define __INVARIANT__H__
31 
32 // ===================================================================================
33 // Design By Contract
34 // ===================================================================================
35 #ifdef DEBUG
36  class InvariantCount { // $$$ Use DECLARE_HELPER_METHODS to automate adding this when interfaces are used?
37  public:
38  InvariantCount() {fNesting = 0;}
39  void Enter() const {++fNesting; ASSERT(fNesting >= 1);}
40  void Exit() const {--fNesting; ASSERT(fNesting >= 0);}
41  bool16 Check() const {return fNesting == 1;}
42  private:
43  mutable int32 fNesting;
44  };
45 
46  template<typename T> class AutoInvariantChecker {
47  public:
48  explicit AutoInvariantChecker(InvariantCount& invariant, T& t) :
49  fInvariant(invariant),
50  fObject(t)
51  {
52  fInvariant.Enter();
53  if (fInvariant.Check())
54  fObject.Invariant();
55  }
56  ~AutoInvariantChecker()
57  {
58  if (fInvariant.Check())
59  fObject.Invariant();
60  fInvariant.Exit();
61  }
62  private:
63  InvariantCount& fInvariant;
64  T& fObject;
65  };
66 
67 #define PRECONDITION(p) ASSERT(p); fInvariant.Enter(); if (fInvariant.Check()) this->Invariant()
68 #define POSTCONDITION(p) ASSERT(p); if (fInvariant.Check()) this->Invariant(); fInvariant.Exit()
69 
70 #define CHECK_INVARIANT this->Invariant
71 #define AUTO_INVARIANT_CHECKER(TYPE) AutoInvariantChecker<TYPE> autoInvariantChecker(fInvariant, *this)
72 
73 #else
74 #define PRECONDITION(p) ((void) 0)
75 #define POSTCONDITION(p) ((void) 0)
76 #define CHECK_INVARIANT() ((void) 0)
77 #define AUTO_INVARIANT_CHECKER(TYPE) ((void) 0)
78 #endif
79 
80 #endif // __INVARIANT__H__
81