Xalan-C++ API Reference  1.12.0
XPathFactory.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XPATHFACTORY_HEADER_GUARD_1357924680)
19 #define XPATHFACTORY_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file. Must be first.
25 
27 
28 #include <cassert>
29 #include <functional>
30 
31 
32 
33 namespace XALAN_CPP_NAMESPACE {
34 
35 
36 
37 class XPath;
38 
39 
40 
42 {
43 public:
44 
45  explicit
46  XPathFactory();
47 
48  virtual
49  ~XPathFactory();
50 
51  /**
52  * Return an XPath to the factory.
53  *
54  * @param theXPath The XPath to be returned
55  * @return true if the object belongs to the factory, false if not.
56  */
57  bool
58  returnObject(const XPath* theXPath)
59  {
60  return doReturnObject(theXPath);
61  }
62 
63  /**
64  * Reset the instance. This invalidates all existing instances created
65  * with this XPathFactory.
66  */
67  virtual void
68  reset() = 0;
69 
70  /**
71  * Create an XPath. The XPath instance is owned by the factory, and should
72  * not be deleted. The factory will manage the lifetime.
73  *
74  */
75  virtual XPath*
76  create() = 0;
77 
78  /**
79  *
80  * A functor for use with stl algorithms.
81  *
82  */
84  {
85  public:
86 
88  XPathFactory& theFactoryInstance,
89  bool fInReset = false) :
90  m_factoryInstance(theFactoryInstance),
91  m_fInReset(fInReset)
92  {
93  }
94 
95  bool
96  operator()(const XPath* theXPath) const
97  {
98  if (m_fInReset == true)
99  {
100  return m_factoryInstance.doReturnObject(theXPath,
101  true);
102  }
103  else
104  {
105  return m_factoryInstance.returnObject(theXPath);
106  }
107  }
108 
109  private:
110 
111  XPathFactory& m_factoryInstance;
112 
113  const bool m_fInReset;
114  };
115 
116  friend struct DeleteXPathFunctor;
117 
118 protected:
119 
120  virtual bool
121  doReturnObject(
122  const XPath* theXPath,
123  bool fInReset = false) = 0;
124 };
125 
126 
127 
128 /**
129  * Manages the lifetime of an XPath instance.
130  */
132 {
133 public:
134 
135  /**
136  * Construct an XPathGuard instance from a factory object and an XPath.
137  *
138  * @param theFactory object that manages lifetime of XPaths
139  * @param theXPath pointer to XPath managed
140  */
142  XPathFactory& theFactory,
143  const XPath* theXPath) :
144  m_factory(&theFactory),
145  m_object(theXPath)
146  {
147  }
148 
149  // Note that copy construction transfers ownership, just
150  // as std::auto_ptr.
152  {
153  // Release the current object...
154  release();
155 
156  // Copy the factory and object pointers...
157  m_factory = theRHS.m_factory;
158  m_object = theRHS.m_object;
159 
160  // The source object no longer points to
161  // the object...
162  theRHS.m_factory = 0;
163  theRHS.m_object = 0;
164  }
165 
167  {
168  reset();
169  }
170 
171  /**
172  * Retrieve the object pointer (must not be null)
173  *
174  * @return pointer to XPath
175  */
176  const XPath*
177  operator->() const
178  {
179  assert(m_object != 0);
180 
181  return m_object;
182  }
183 
184  /**
185  * Retrieve the object pointer (may be null)
186  *
187  * @return pointer to XPath
188  */
189  const XPath*
190  get() const
191  {
192  return m_object;
193  }
194 
195  /**
196  * Return the referenced object to the factory and set pointers to null.
197  */
198  void
200  {
201  if (m_object != 0)
202  {
203  assert(m_factory != 0);
204 
205  m_factory->returnObject(m_object);
206 
207  m_object = 0;
208  }
209 
210  m_factory = 0;
211  }
212 
213  /**
214  * Transfers ownership of XPath to caller
215  *
216  * @return pointer to XPath
217  */
218  const XPath*
220  {
221  const XPath* const theTemp = m_object;
222 
223  m_object = 0;
224 
225  return theTemp;
226  }
227 
228 private:
229 
230  XPathGuard&
231  operator=(const XPathGuard&);
232 
233  bool
234  operator==(const XPathGuard&) const;
235 
236 
237  // Data members...
238  XPathFactory* m_factory;
239  const XPath* m_object;
240 };
241 
242 
243 
244 }
245 
246 
247 
248 #endif // XPATHFACTORY_HEADER_GUARD_1357924680
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XPathGuard::XPathGuard
XPathGuard(XPathGuard &theRHS)
Definition: XPathFactory.hpp:151
xalanc::XPathGuard
Manages the lifetime of an XPath instance.
Definition: XPathFactory.hpp:131
xalanc::XPathGuard::operator->
const XPath * operator->() const
Retrieve the object pointer (must not be null)
Definition: XPathFactory.hpp:177
xalanc::XPathGuard::release
const XPath * release()
Transfers ownership of XPath to caller.
Definition: XPathFactory.hpp:219
xalanc::XPathFactory::DeleteXPathFunctor
A functor for use with stl algorithms.
Definition: XPathFactory.hpp:83
xalanc::XPathGuard::XPathGuard
XPathGuard(XPathFactory &theFactory, const XPath *theXPath)
Construct an XPathGuard instance from a factory object and an XPath.
Definition: XPathFactory.hpp:141
xalanc::XPathGuard::~XPathGuard
~XPathGuard()
Definition: XPathFactory.hpp:166
xalanc::XPath
Definition: XPath.hpp:67
xalanc::XPathFactory
Definition: XPathFactory.hpp:41
xalanc::operator==
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)
Definition: XalanVector.hpp:1118
xalanc::XPathFactory::DeleteXPathFunctor::operator()
bool operator()(const XPath *theXPath) const
Definition: XPathFactory.hpp:96
XALAN_XPATH_EXPORT
#define XALAN_XPATH_EXPORT
Definition: XPathDefinitions.hpp:35
xalanc::XPathGuard::reset
void reset()
Return the referenced object to the factory and set pointers to null.
Definition: XPathFactory.hpp:199
xalanc::XPathGuard::get
const XPath * get() const
Retrieve the object pointer (may be null)
Definition: XPathFactory.hpp:190
XalanMemoryManagement.hpp
xalanc::XPathFactory::returnObject
bool returnObject(const XPath *theXPath)
Return an XPath to the factory.
Definition: XPathFactory.hpp:58
xalanc::XPathFactory::DeleteXPathFunctor::DeleteXPathFunctor
DeleteXPathFunctor(XPathFactory &theFactoryInstance, bool fInReset=false)
Definition: XPathFactory.hpp:87
XPathDefinitions.hpp