2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/Predicates.h
24 * Useful XMLObject predicates for use with STL algorithms.
27 #ifndef __xmltooling_predicates_h__
28 #define __xmltooling_predicates_h__
30 #include <xmltooling/QName.h>
31 #include <xmltooling/XMLObject.h>
35 namespace xmltooling {
38 * Predicate that checks the QName of an XMLObject.
46 * @param q the QName to check for
48 hasQName(const QName& q) : m_q(q) {
52 * Returns true iff the provided object's QName matches the constructor argument.
54 * @param xmlObject the object to examine
56 bool operator()(const XMLObject* xmlObject) const {
57 return xmlObject ? (xmlObject->getElementQName() == m_q) : false;
65 * Predicate that checks the xsi:type of an XMLObject.
73 * @param q the QName to check for
75 hasSchemaType(const QName& q) : m_q(q) {
79 * Returns true iff the provided object's xsi:type matches the constructor argument.
81 * @param xmlObject the object to examine
83 bool operator()(const XMLObject* xmlObject) const {
84 const QName* xsitype = xmlObject ? xmlObject->getSchemaType() : nullptr;
85 return xsitype ? (*xsitype == m_q) : false;
93 * Template algorithm returns first pointer element from a container that matches a predicate.
95 * @param c read-only container of pointer-based objects
96 * @param p a predicate function
97 * @return the first object in the container matching the predicate, or nullptr
99 template<typename Container, typename Predicate>
100 typename Container::value_type find_if(const Container& c, const Predicate& p) {
101 typename Container::const_iterator i = std::find_if(c.begin(), c.end(), p);
102 return (i!=c.end()) ? *i : nullptr;
106 * Template algorithm returns first pointer element from a container that matches a predicate.
108 * @param c read-only container of pointer-based objects
109 * @param p a predicate function
110 * @return the first object in the container matching the predicate, or nullptr
112 template<typename Container, typename Predicate>
113 typename Container::value_type find_if(Container& c, const Predicate& p) {
114 typename Container::iterator i = std::find_if(c.begin(), c.end(), p);
115 return (i!=c.end()) ? *i : nullptr;
120 #endif /* __xmltooling_predicates_h__ */