Fix missing forward decl.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / Predicates.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * @file xmltooling/util/Predicates.h
23  * 
24  * Useful XMLObject predicates for use with STL algorithms. 
25  */
26
27 #ifndef __xmltooling_predicates_h__
28 #define __xmltooling_predicates_h__
29
30 #include <xmltooling/QName.h>
31 #include <xmltooling/XMLObject.h>
32
33 #include <functional>
34
35 namespace xmltooling {
36
37     /**
38      * Predicate that checks the QName of an XMLObject.
39      */
40     class hasQName
41     {
42     public:
43         /**
44          * Constructor.
45          * 
46          * @param q the QName to check for
47          */
48         hasQName(const QName& q) : m_q(q) {
49         }
50         
51         /**
52          * Returns true iff the provided object's QName matches the constructor argument.
53          * 
54          * @param xmlObject the object to examine
55          */
56         bool operator()(const XMLObject* xmlObject) const {
57             return xmlObject ? (xmlObject->getElementQName() == m_q) : false;
58         }
59         
60     private:
61         const QName& m_q;
62     };
63
64     /**
65      * Predicate that checks the xsi:type of an XMLObject.
66      */
67     class hasSchemaType
68     {
69     public:
70         /**
71          * Constructor.
72          * 
73          * @param q the QName to check for
74          */
75         hasSchemaType(const QName& q) : m_q(q) {
76         }
77         
78         /**
79          * Returns true iff the provided object's xsi:type matches the constructor argument.
80          * 
81          * @param xmlObject the object to examine
82          */
83         bool operator()(const XMLObject* xmlObject) const {
84             const QName* xsitype = xmlObject ? xmlObject->getSchemaType() : nullptr;
85             return xsitype ? (*xsitype == m_q) : false;
86         }
87         
88     private:
89         const QName& m_q;
90     };
91
92     /**
93      * Template algorithm returns first pointer element from a container that matches a predicate.
94      *
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
98      */
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;
103     }
104
105     /**
106      * Template algorithm returns first pointer element from a container that matches a predicate.
107      *
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
111      */
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;
116     }
117
118 };
119
120 #endif /* __xmltooling_predicates_h__ */