convert line ends
[canaan.git] / prj / cam / src / object / traisrch.h
blobc8d5a8cc0fdec55a4a1c41de6b8d78c58daf4c5e
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/src/object/traisrch.h,v 1.7 2000/02/22 20:00:47 toml Exp $
7 #pragma once
8 #ifndef __TRAISRCH_H
9 #define __TRAISRCH_H
10 #include <appagg.h>
11 #include <objquer_.h>
12 #include <traitman.h>
13 #include <dlist.h>
14 #include <trait.h>
15 #include <donorq_.h>
16 #include <relation.h>
17 #include <dynarray.h>
19 #include <dbmem.h>
21 typedef cSimpleDList<IObjectQuery*> LinkQueryList;
25 template <class TYPE>
26 class cSimpleStack
28 typedef cDynArray<TYPE> cArray;
30 protected:
31 cArray mArray;
32 int top;
34 enum { kInitSize = 16 };
36 public:
37 cSimpleStack() :mArray(kInitSize),top(0) { };
39 void Push(const TYPE& q)
41 int sz = mArray.Size();
42 if (top >= sz)
43 mArray.Grow(sz); // double in size
45 mArray[top++] = q;
48 TYPE& Top()
50 if (top <= 0)
52 mArray[0] = TYPE();
53 return mArray[0];
55 return mArray[top-1];
58 TYPE Pop()
60 if (top <= 0)
62 top = 0;
63 return TYPE();
65 return mArray[--top];
68 int Size()
70 return top;
73 // Returns nth from top, zero is top
74 BOOL Nth(int n, TYPE* val)
76 if (n >= top)
77 return FALSE;
78 *val = mArray[top-1 -n];
79 return TRUE;
85 // Dumb pointer class to interface with template
88 class QueryPtr : public cIPtr<IObjectQuery>
90 typedef cIPtr<IObjectQuery> cParent;
92 public:
93 QueryPtr(IObjectQuery* q = NULL) : cParent(q) {};
94 QueryPtr(const QueryPtr& q) : cParent((IObjectQuery*)q) {};
96 IObjectQuery* operator =(const QueryPtr& q) { return (IObjectQuery*) (pUnknown = q.pUnknown); };
99 class QueryStack : public cSimpleStack<QueryPtr>
105 ////////////////////////////////////////////////////////////
106 // TRAIT SEARCH QUERY
108 // This one searches the entire inheritance graph upwards,
109 // in prioritized order.
111 // Takes the object to start the search from, and doesn't
112 // yield that object
115 class cDepthFirstObjectQuery : public cBaseObjectQuery
117 protected:
118 QueryStack Queries;
119 ObjID CurObj;
121 // "close up" an object. Second arg is the parent
122 // third is whether it was "finished"
124 BOOL Expand(); // search deeper
125 BOOL Contract(); // unwind
128 // Generate the successors of a particular object
130 virtual IObjectQuery* Successors(ObjID obj) = 0;
132 public:
133 cDepthFirstObjectQuery(ObjID obj) : CurObj(obj) {};
134 virtual ~cDepthFirstObjectQuery()
136 while(!!Queries.Top())
138 Queries.Pop()->Release();
143 STDMETHOD_(BOOL,Done)();
144 STDMETHOD_(ObjID,Object)();
145 STDMETHOD(Next)();
149 ////////////////////////////////////////////////////////////
150 // Transitive Prioritized Link Query
153 class cTransitiveLinkObjQuery : public cDepthFirstObjectQuery
155 IRelation* Rel;
156 protected:
157 IObjectQuery* Successors(ObjID obj)
159 ILinkQuery* q = Rel->Query(obj,LINKOBJ_WILDCARD);
160 IObjectQuery* out = new cPriLinkDonorQuery(q);
161 SafeRelease(q);
162 return out;
165 public:
166 cTransitiveLinkObjQuery(ObjID obj, IRelation* rel)
167 : cDepthFirstObjectQuery(obj),Rel(rel)
168 { Rel->AddRef();};
170 ~cTransitiveLinkObjQuery() { SafeRelease(Rel);};
175 ////////////////////////////////////////////////////////////
176 // OBJECT QUERY FILTER
178 // Skips past things that are not intrinsic to the trait
181 class cFilterObjectQuery : public cBaseObjectQuery
183 IObjectQuery* Inner;
185 protected:
186 // overload this to filter differently
187 virtual BOOL Filter(ObjID ) { return TRUE;};
189 // call this in your constructor
190 void Skip()
192 for (; !Inner->Done(); Inner->Next())
193 if (Filter(Inner->Object()))
194 break;
197 public:
198 cFilterObjectQuery(IObjectQuery* inner)
199 : Inner(inner)
201 Inner->AddRef();
204 virtual ~cFilterObjectQuery()
206 SafeRelease(Inner);
209 STDMETHOD_(BOOL,Done)() { return Inner->Done();};
210 STDMETHOD_(ObjID,Object)() { return Inner->Object(); };
211 STDMETHOD(Next)()
213 Inner->Next();
214 Skip();
215 return S_OK;
219 ////////////////////////////////////////////////////////////
220 // CONCRETENESS FILTER
223 class cConcretenessFilterQuery : public cFilterObjectQuery
225 eObjConcreteness Which;
226 protected:
227 BOOL Filter(ObjID );
229 public:
230 cConcretenessFilterQuery(IObjectQuery* q, eObjConcreteness which)
231 : cFilterObjectQuery(q), Which(which) { Skip();}
232 ~cConcretenessFilterQuery() {};
236 #include <undbmem.h>
238 #endif // __TRAISRCH_H