2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file xmltooling/util/Predicates.h
20 * Useful XMLObject predicates for use with STL algorithms.
23 #ifndef __xmltooling_predicates_h__
24 #define __xmltooling_predicates_h__
26 #include <xmltooling/XMLObject.h>
30 namespace xmltooling {
33 * Predicate that checks the QName of an XMLObject.
41 * @param q the QName to check for
43 hasQName(const QName& q) : m_q(q) {
47 * Returns true iff the provided object's QName matches the constructor argument.
49 * @param xmlObject the object to examine
51 bool operator()(const XMLObject* xmlObject) const {
52 return xmlObject ? (xmlObject->getElementQName() == m_q) : false;
60 * Predicate that checks the xsi:type of an XMLObject.
68 * @param q the QName to check for
70 hasSchemaType(const QName& q) : m_q(q) {
74 * Returns true iff the provided object's xsi:type matches the constructor argument.
76 * @param xmlObject the object to examine
78 bool operator()(const XMLObject* xmlObject) const {
79 const QName* xsitype = xmlObject ? xmlObject->getSchemaType() : NULL;
80 return xsitype ? (*xsitype == m_q) : false;
88 * Template algorithm returns first pointer element from a container that matches a predicate.
90 * @param c read-only container of pointer-based objects
91 * @param p a predicate function
92 * @return the first object in the container matching the predicate, or NULL
94 template<typename Container, typename Predicate>
95 typename Container::value_type find_if(const Container& c, const Predicate& p) {
96 typename Container::const_iterator i = std::find_if(c.begin(), c.end(), p);
97 return (i!=c.end()) ? *i : NULL;
101 * Template algorithm returns first pointer element from a container that matches a predicate.
103 * @param c read-only container of pointer-based objects
104 * @param p a predicate function
105 * @return the first object in the container matching the predicate, or NULL
107 template<typename Container, typename Predicate>
108 typename Container::value_type find_if(Container& c, const Predicate& p) {
109 typename Container::iterator i = std::find_if(c.begin(), c.end(), p);
110 return (i!=c.end()) ? *i : NULL;
115 #endif /* __xmltooling_predicates_h__ */