3269231a0160b3f2dfa2f52c74698ff812d484e2
[shibboleth/cpp-xmltooling.git] / xmltooling / util / Predicates.h
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file xmltooling/util/Predicates.h
19  * 
20  * Useful XMLObject predicates for use with STL algorithms. 
21  */
22
23 #ifndef __xmltooling_predicates_h__
24 #define __xmltooling_predicates_h__
25
26 #include <xmltooling/XMLObject.h>
27
28 #include <functional>
29
30 namespace xmltooling {
31
32     /**
33      * Predicate that checks the QName of an XMLObject.
34      */
35     class hasQName
36     {
37     public:
38         /**
39          * Constructor.
40          * 
41          * @param q the QName to check for
42          */
43         hasQName(const QName& q) : m_q(q) {
44         }
45         
46         /**
47          * Returns true iff the provided object's QName matches the constructor argument.
48          * 
49          * @param xmlObject the object to examine
50          */
51         bool operator()(const XMLObject* xmlObject) const {
52             return xmlObject ? (xmlObject->getElementQName() == m_q) : false;
53         }
54         
55     private:
56         const QName& m_q;
57     };
58
59     /**
60      * Predicate that checks the xsi:type of an XMLObject.
61      */
62     class hasSchemaType
63     {
64     public:
65         /**
66          * Constructor.
67          * 
68          * @param q the QName to check for
69          */
70         hasSchemaType(const QName& q) : m_q(q) {
71         }
72         
73         /**
74          * Returns true iff the provided object's xsi:type matches the constructor argument.
75          * 
76          * @param xmlObject the object to examine
77          */
78         bool operator()(const XMLObject* xmlObject) const {
79             const QName* xsitype = xmlObject ? xmlObject->getSchemaType() : nullptr;
80             return xsitype ? (*xsitype == m_q) : false;
81         }
82         
83     private:
84         const QName& m_q;
85     };
86
87     /**
88      * Template algorithm returns first pointer element from a container that matches a predicate.
89      *
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 nullptr
93      */
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 : nullptr;
98     }
99
100     /**
101      * Template algorithm returns first pointer element from a container that matches a predicate.
102      *
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 nullptr
106      */
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 : nullptr;
111     }
112
113 };
114
115 #endif /* __xmltooling_predicates_h__ */