SourceForge.net Logo
ContextHelpers.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001, 2008,
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004, 2011,
5  * Oracle and/or its affiliates. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef CONTEXTHELPERS_HPP
21 #define CONTEXTHELPERS_HPP
22 
23 #include <xqilla/framework/XQillaExport.hpp>
25 
26 class XQILLA_API AutoNodeSetOrderingReset
27 {
28 public:
30  {
31  context_ = context;
32  if(context_) {
33  ordering_ = context->getNodeSetOrdering();
34  context->setNodeSetOrdering(ordering);
35  }
36  }
37 
39  {
40  if(context_) {
41  context_->setNodeSetOrdering(ordering_);
42  }
43  }
44 
45 protected:
48 };
49 
50 class XQILLA_API AutoContextItemTypeReset
51 {
52 public:
54  {
55  context_ = context;
56  if(context_) {
57  sType_ = context->getContextItemType();
58  }
59  }
60 
62  {
63  context_ = context;
64  if(context_) {
65  sType_ = context->getContextItemType();
66  context->setContextItemType(sType);
67  }
68  }
69 
71  {
72  if(context_) {
73  context_->setContextItemType(sType_);
74  }
75  }
76 
77 protected:
80 };
81 
82 class XQILLA_API AutoNsScopeReset
83 {
84 public:
85  AutoNsScopeReset(StaticContext* context, XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* newResolver)
86  {
87  context_=context;
88  if(context_) {
89  _oldNSResolver=context_->getNSResolver();
90  _defaultElementAndTypeNS=context->getDefaultElementAndTypeNS();
91  context_->setNSResolver(newResolver);
92  }
93  }
94 
96  {
97  if(context_) {
98  context_->setNSResolver(_oldNSResolver);
99  context_->setDefaultElementAndTypeNS(_defaultElementAndTypeNS);
100  }
101  }
102 
103 protected:
105  const XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* _oldNSResolver;
107 
108 };
109 
110 class XQILLA_API AutoContextInfoReset
111 {
112 public:
114  : oldContextItem(context->getContextItem()),
115  oldContextPosition(context->getContextPosition()),
116  oldContextSize(context->getContextSize()),
117  context_(context)
118  {
119  }
120 
121  AutoContextInfoReset(DynamicContext *context, const Item::Ptr &contextItem, size_t contextPosition = 0, size_t contextSize = 0)
122  : oldContextItem(context->getContextItem()),
123  oldContextPosition(context->getContextPosition()),
124  oldContextSize(context->getContextSize()),
125  context_(context)
126  {
127  context->setContextItem(contextItem);
128  context->setContextPosition(contextPosition);
129  context->setContextSize(contextSize);
130  }
131 
133  {
134  resetContextInfo();
135  }
136 
137  void resetContextInfo()
138  {
139  context_->setContextItem(oldContextItem);
140  context_->setContextPosition(oldContextPosition);
141  context_->setContextSize(oldContextSize);
142  }
143 
147 
148 private:
149  DynamicContext* context_;
150 };
151 
152 class XQILLA_API AutoDocumentCacheReset
153 {
154 public:
156  : oldDC(const_cast<DocumentCache*>(context->getDocumentCache())),
157  context_ (context)
158  {
159  }
160 
162  {
163  context_->setDocumentCache(oldDC);
164  }
165 
167 
168 protected:
170 };
171 
172 class XQILLA_API AutoVariableStoreReset
173 {
174 public:
176  {
177  context_ = context;
178  _oldVarStore = context_->getVariableStore();
179  if(store)
180  context_->setVariableStore(store);
181  }
182 
184  {
185  context_->setVariableStore(_oldVarStore);
186  }
187 
188  void reset()
189  {
190  context_->setVariableStore(_oldVarStore);
191  }
192 
193 protected:
196 };
197 
198 class XQILLA_API AutoRegexGroupStoreReset
199 {
200 public:
202  {
203  context_ = context;
204  _oldRegexStore = context_->getRegexGroupStore();
205  if(store)
206  context_->setRegexGroupStore(store);
207  }
208 
210  {
211  context_->setRegexGroupStore(_oldRegexStore);
212  }
213 
214  void reset()
215  {
216  context_->setRegexGroupStore(_oldRegexStore);
217  }
218 
219 protected:
222 };
223 
224 class XQILLA_API AutoMessageListenerReset
225 {
226 public:
228  {
229  context_ = context;
230  if(context_) {
231  listener_ = context->getMessageListener();
232  context->setMessageListener(listener);
233  }
234  }
235 
237  {
238  if(context_) {
239  context_->setMessageListener(listener_);
240  }
241  }
242 
243 protected:
246 };
247 
248 class XQILLA_API AutoStackFrameReset
249 {
250 public:
252  {
253  context_ = context;
254  _oldFrame = context_->getStackFrame();
255  context_->setStackFrame(frame);
256  }
257 
259  {
260  context_->setStackFrame(_oldFrame);
261  }
262 
263  void reset()
264  {
265  context_->setStackFrame(_oldFrame);
266  }
267 
268 protected:
271 };
272 
273 template<typename T> class XQILLA_API AutoReset
274 {
275 public:
276  AutoReset(T &orig)
277  : orig_(orig)
278  {
279  old_ = orig;
280  }
281 
283  {
284  reset();
285  }
286 
287  void reset()
288  {
289  orig_ = old_;
290  }
291 
292 protected:
293  T &orig_;
294  T old_;
295 };
296 
297 #endif