Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[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/XMLObject.h>
31
32 #include <functional>
33
34 namespace xmltooling {
35
36     /**
37      * Predicate that checks the QName of an XMLObject.
38      */
39     class hasQName
40     {
41     public:
42         /**
43          * Constructor.
44          * 
45          * @param q the QName to check for
46          */
47         hasQName(const QName& q) : m_q(q) {
48         }
49         
50         /**
51          * Returns true iff the provided object's QName matches the constructor argument.
52          * 
53          * @param xmlObject the object to examine
54          */
55         bool operator()(const XMLObject* xmlObject) const {
56             return xmlObject ? (xmlObject->getElementQName() == m_q) : false;
57         }
58         
59     private:
60         const QName& m_q;
61     };
62
63     /**
64      * Predicate that checks the xsi:type of an XMLObject.
65      */
66     class hasSchemaType
67     {
68     public:
69         /**
70          * Constructor.
71          * 
72          * @param q the QName to check for
73          */
74         hasSchemaType(const QName& q) : m_q(q) {
75         }
76         
77         /**
78          * Returns true iff the provided object's xsi:type matches the constructor argument.
79          * 
80          * @param xmlObject the object to examine
81          */
82         bool operator()(const XMLObject* xmlObject) const {
83             const QName* xsitype = xmlObject ? xmlObject->getSchemaType() : nullptr;
84             return xsitype ? (*xsitype == m_q) : false;
85         }
86         
87     private:
88         const QName& m_q;
89     };
90
91     /**
92      * Template algorithm returns first pointer element from a container that matches a predicate.
93      *
94      * @param c read-only container of pointer-based objects
95      * @param p a predicate function
96      * @return  the first object in the container matching the predicate, or nullptr
97      */
98     template<typename Container, typename Predicate>
99     typename Container::value_type find_if(const Container& c, const Predicate& p) {
100         typename Container::const_iterator i = std::find_if(c.begin(), c.end(), p);
101         return (i!=c.end()) ? *i : nullptr;
102     }
103
104     /**
105      * Template algorithm returns first pointer element from a container that matches a predicate.
106      *
107      * @param c read-only container of pointer-based objects
108      * @param p a predicate function
109      * @return  the first object in the container matching the predicate, or nullptr
110      */
111     template<typename Container, typename Predicate>
112     typename Container::value_type find_if(Container& c, const Predicate& p) {
113         typename Container::iterator i = std::find_if(c.begin(), c.end(), p);
114         return (i!=c.end()) ? *i : nullptr;
115     }
116
117 };
118
119 #endif /* __xmltooling_predicates_h__ */