InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ChangeTree.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Ryan Gano
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 __ChangeTree__
25 #define __ChangeTree__
26 
27 #include "TreeNode.h"
28 #include <map>
29 #ifdef DEBUG
30 //#define VALIDATE_TREE
31 #endif
32 #ifdef VALIDATE_TREE
33 #include <queue>
34 #include <boost/bind.hpp>
35 #endif
36 
45 template<class ItemType>
47 {
48 public:
51 
53  ChangeTree() : fRoot(nil) {}
54 
58  ChangeTree(const ChangeTree &o);
59 
62  {
63  if (fRoot != nil)
64  Remove(fRoot);
65  }
66 
71  ChangeTree &operator=(const ChangeTree &o);
72 
77  void swap(ChangeTree &o);
78 
81  int size() const;
82 
91  void CloneBranch(Node *myStart, const Node *otherStart);
92 
101  void Insert(Node *node, Node *parent);
102 
106  void Remove(Node *node);
107 
113  Node *find(ItemType item);
114 
118  Node *GetRoot();
119 
120 private:
121 
123  Node *fRoot;
124 
126  typedef std::map<ItemType, Node *> NodeMap;
127  NodeMap fLookup;
128 
129 public:
130 #ifdef VALIDATE_TREE
131  // Validator for fLookup map -- check for fRoot's tree and fLookup being out of sync
132  template<class SameItemType>
133  class ChangeTreeValidator : public std::unary_function<SameItemType, void>
134  {
135  public:
136  ChangeTreeValidator(NodeMap lookup, Node *root) : fMyLookup(lookup),fMyRoot(root)
137  {
138  //ASSERT(fMyRoot != nil);
139  }
140  void operator()()
141  {
142  typename NodeMap::iterator iter;
143  for (iter = fMyLookup.begin(); iter != fMyLookup.end(); ++iter)
144  {
145  // Check fMyRoot and its descendants for a match on current iter
146  std::queue<Node*> treeQueue;
147  treeQueue.push(fMyRoot);
148  Node* targetNode = iter->second;
149  bool16 bFound = kFalse;
150  while (!treeQueue.empty())
151  {
152  // Get the current node
153  Node *curNode = treeQueue.front();
154  treeQueue.pop();
155 
156  if (curNode->fItem == targetNode->fItem)
157  {
158  bFound = kTrue;
159  break; // Found map item in tree... this is good
160  }
161  // Add its direct descendents to the queue
162  std::for_each(curNode->fChildren.begin(), curNode->fChildren.end(), boost::bind(&std::queue<Node*>::push, &treeQueue, _1));
163  }
164  ASSERT_MSG(bFound, "ChangeTree out of sync -- some fLookup node not found in fRoot's tree!");
165  }
166  }
167 
168  // void operator()(ItemType const& item);
169  private:
170  Node *fMyRoot;
171  NodeMap fMyLookup;
172  };
173 #endif
174 
175 };
176 #endif