InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NodeID.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Matt Joss
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 __NodeID__
25 #define __NodeID__
26 
27 
28 //----------------------------------------------------------------------------------------
29 // Forward Declarations
30 //----------------------------------------------------------------------------------------
31 #include "IPMStream.h"
32 #include "K2Vector.h"
33 
34 class NodeID;
35 class NodeID_rv;
36 
37 typedef K2Vector<NodeID> NodeIDList; // Sorted vector
38 
39 void K2Read(IPMStream* stream, NodeID& ref);
40 void K2Write(IPMStream* stream, const NodeID& ref);
41 
42 bool16 DV_ operator ==(const NodeID& a, const NodeID& b);
43 bool16 DV_ operator !=(const NodeID& a, const NodeID& b);
44 bool16 DV_ operator <(const NodeID& a, const NodeID& b);
45 bool16 DV_ operator >(const NodeID& a, const NodeID& b);
46 bool16 DV_ operator <=(const NodeID& a, const NodeID& b);
47 bool16 DV_ operator >=(const NodeID& a, const NodeID& b);
48 
49 
50 
51 //========================================================================================
52 // class NodeIDClass
53 //========================================================================================
55 {
56 public:
57  NodeIDClass() = default;
58  virtual ~NodeIDClass() {};
59  typedef int32 NodeType;
60 
61  virtual NodeType GetNodeType() const = 0;
62  virtual int32 Compare(const NodeIDClass* NodeID) const = 0;
63  virtual NodeIDClass* Clone() const = 0;
64  virtual void Read(IPMStream* stream) = 0;
65  virtual void Write(IPMStream* stream) const = 0;
66 
67  // This is mostly a debug call so that TreeView asserts have more context information
68  // Derived class should override this method to print out a description of their nodeID.
69  // For instance, a UIDNodeID may write out the UID and classID.
70  virtual PMString GetDescription() const { return PMString("Override NodeID::GetDescription() to provide useful information here"); };
71 
72  // NodeIDClass is not meant to be copied/assigned - it is meant to be cloned, hence deleting the copy constructor & copy assignment.
73  NodeIDClass(const NodeIDClass &) = delete;
74  NodeIDClass &operator =(const NodeIDClass &) = delete;
75 };
76 
77 
78 
79 
80 // This is just a simple return value class. It is similar to NodeID, but relinquishes control of its
81 // NodeIDClass ptr instead of copying it. Its purpose is easier to see when we consider using NodeID as a
82 // return value. If NodeID is used as a return type, when the return occurs, we will create a new NodeIDClass and
83 // store that in the NodeID that is created. When the return occurs, we either use operator= to assign to a new NodeID or
84 // use NodeID( const NodeID& ) constuctor. In both cases, the NodeIDClass will be cloned for the new NodeID so the temporary
85 // NodeID that was used as the return value can destruct its NodeIDClass. We could change the way operator= and NodeID(const NodeID&)
86 // work so that they relinquish control of the NodeIDClass instead of cloning, but then code like...
87 // NodeID x = y; //where y was a NodeID makes y invalid. This creates bugs that are difficult to find and easy to create. Instead,
88 // we create a return value class(NodeID_t). This way, we can have a constructor NodeID(const NodeID_t& ) which will not clone but will pass
89 // on responsibility for the NodeIDClass. Since NodeID_t should only be used for return values, we shouldn't run into the problems mentioned above.
90 
91 class NodeID_rv
92 {
93 public:
94  NodeID_rv() = delete;
95  NodeID_rv(NodeIDClass* nodeID) : fNodeID(nodeID) {}
96 
97  ~NodeID_rv() { ASSERT_MSG(fNodeID == nil, "NodeID_rv::fNodeID should be nil, NodeID_rv should only be used as a return value, and it should always Relinquish() it's NodeIDClass*. Otherwise we'll leak."); };
98 
99  NodeID_rv(const NodeID_rv& other) = delete;
100  NodeID_rv &operator =(const NodeID_rv& other) = delete;
101 
102  NodeID_rv(NodeID_rv && other) noexcept : fNodeID(other.fNodeID) { other.fNodeID = nil; }
103  NodeID_rv &operator =(NodeID_rv&& other) noexcept = delete;
104 
105  NodeID_rv(const NodeID& nodeID);
106 
107  NodeIDClass* Relinquish() const noexcept { NodeIDClass* tempNodeID = fNodeID; fNodeID = nil; return tempNodeID; }
108  NodeIDClass* Get() const noexcept { return fNodeID; }
109 
110 private:
111 
112  mutable NodeIDClass* fNodeID;
113 };
114 
115 //========================================================================================
116 // class NodeID
117 //========================================================================================
118 class DV_ NodeID
119 {
120 public:
121  typedef object_type data_type;
122 
123  NodeID() : fNodeID(nil) {}
124  NodeID(NodeIDClass* nodeID) { fNodeID = nodeID; }
125  NodeID( const NodeID_rv& tempNodeID) { fNodeID = tempNodeID.Relinquish(); }
126  NodeID( const NodeID& other);
127  NodeID(NodeID &&other) noexcept:
128  fNodeID(other.fNodeID)
129  {
130  other.fNodeID = nil;
131  }
132  NodeID(NodeID_rv&& tempNodeID) noexcept { fNodeID = tempNodeID.Relinquish(); }
133 
134  ~NodeID() { if (fNodeID) { delete fNodeID; } }
135 
136  NodeIDClass::NodeType GetNodeType() const { if (fNodeID) return fNodeID->GetNodeType(); else return 0; }
137  bool16 IsValid() const { return fNodeID != nil; }
138  void MakeInvalid()
139  {
140  if (fNodeID)
141  delete fNodeID;
142  fNodeID = nil;
143  }
144 
145 
146  friend inline void swap(NodeID &a, NodeID &b) noexcept
147  {
148  std::swap(a.fNodeID, b.fNodeID);
149  }
150 
151  NodeID& operator =(const NodeID& other);
152  NodeID& operator =(NodeID&& other) noexcept
153  {
154  using std::swap;
155  swap(*this, other);
156 
157  return *this;
158  }
159  NodeID& operator =( NodeID_rv& returnValNodeID);
160 
161 // operator NodeIDClass*() const { return fNodeID; }
162 // const NodeIDClass* operator->() const { return fNodeID; }
163  NodeIDClass* Get() const { return fNodeID; }
164  virtual PMString GetDescription() const { return fNodeID ? fNodeID->GetDescription() : ""; };
165 
166  static void SetNodeIDTemplate( const NodeID& templateNode );
167  static void ClearNodeIDTemplate( );
168 
169  friend void K2Read(IPMStream* stream, NodeID& ref);
170  friend void K2Write(IPMStream* stream, const NodeID& ref);
171 
172  friend bool16 operator ==(const NodeID& a, const NodeID& b);
173  friend bool16 operator !=(const NodeID& a, const NodeID& b);
174  friend bool16 operator <(const NodeID& a, const NodeID& b);
175  friend bool16 operator >(const NodeID& a, const NodeID& b);
176  friend bool16 operator <=(const NodeID& a, const NodeID& b);
177  friend bool16 operator >=(const NodeID& a, const NodeID& b);
178 
179 private:
180  NodeIDClass* fNodeID;
181  static NodeIDClass* sNodeIDTemplate; //defined in CTreeViewMgr.cpp
182 };
183 
184 
185 extern DV_ NodeID kInvalidNodeID;
186 
187 //----------------------------------------------------------------------------------------
188 // NodeID_rv::NodeID_rv
189 //----------------------------------------------------------------------------------------
190 inline NodeID_rv::NodeID_rv(const NodeID& nodeID)
191 {
192  fNodeID = nodeID.Get() != nil ? nodeID.Get()->Clone() : nil;
193 }
194 
195 
196 template <class TNode>
198 {
199  public:
200 // explicit TreeNodePtr(const NodeID& nodeID) { TNode::kNodeType == nodeID.GetNodeType() ? fNodeClass = static_cast<TNode*>(nodeID.Get()) : nil; }
201 // explicit TreeNodePtr(const NodeID_rv& nodeID) { TNode::kNodeType == static_cast<TNode*>(nodeID.Get())->GetNodeType() ? fNodeClass = static_cast<TNode*>(nodeID.Relinquish()) : nil; }
202  explicit TreeNodePtr(const NodeID& nodeID) { fNodeClass = TNode::kNodeType == nodeID.GetNodeType() ? static_cast<TNode*>(nodeID.Get()) : nil; }
203  explicit TreeNodePtr(const NodeID_rv& nodeID) { fNodeClass = TNode::kNodeType == static_cast<TNode*>(nodeID.Get())->GetNodeType() ? static_cast<TNode*>(nodeID.Relinquish()) : nil; }
204 // explicit TreeNodePtr(const NodeID& nodeID);
205 // explicit TreeNodePtr(const NodeID_rv& nodeID);
206  TNode* operator->() const { ASSERT_MSG(fNodeClass, "About to use nil TreeNodePtr!"); return fNodeClass; }
207  operator TNode*() const { return fNodeClass; }
208 
209  void reset(const NodeID& nodeID) { fNodeClass = static_cast<TNode*>(nodeID.Get()); }
210  private:
211  TNode* fNodeClass;
212 };
213 
214 
215 #endif