Chemical Data Processing Library C++ API - Version 1.4.0
PropertyMatchExpression.hpp
Go to the documentation of this file.
1 /*
2  * PropertyMatchExpression.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_CHEM_PROPERTYMATCHEXPRESSION_HPP
30 #define CDPL_CHEM_PROPERTYMATCHEXPRESSION_HPP
31 
32 #include <functional>
33 
35 
36 
37 namespace CDPL
38 {
39 
40  namespace Chem
41  {
42 
63  template <typename ValueType, typename MatchFunc, typename ObjType1, typename ObjType2 = void>
64  class PropertyMatchExpression : public MatchExpression<ObjType1, ObjType2>
65  {
66 
67  public:
71  typedef std::shared_ptr<PropertyMatchExpression> SharedPointer;
72 
79  typedef std::function<ValueType(const ObjType1&, const ObjType2&)> PropertyFunction;
80 
87  value(), matchFunc(), propertyFunc(property_func), fixed(false) {}
88 
95  PropertyMatchExpression(const ValueType& value, const PropertyFunction& property_func):
96  value(value), matchFunc(), propertyFunc(property_func), fixed(true) {}
97 
112  bool operator()(const ObjType1& query_obj1, const ObjType2& query_obj2, const ObjType1& target_obj1,
113  const ObjType2& target_obj2, const Base::Any& aux_data) const;
114 
115  private:
116  ValueType value;
117  MatchFunc matchFunc;
118  PropertyFunction propertyFunc;
119  bool fixed;
120  };
121 
139  template <typename ValueType, typename MatchFunc, typename ObjType>
140  class PropertyMatchExpression<ValueType, MatchFunc, ObjType, void> : public MatchExpression<ObjType, void>
141  {
142 
143  public:
147  typedef std::shared_ptr<PropertyMatchExpression> SharedPointer;
148 
155  typedef std::function<ValueType(const ObjType&)> PropertyFunction;
156 
163  value(), matchFunc(), propertyFunc(property_func), fixed(false) {}
164 
171  PropertyMatchExpression(const ValueType& value, const PropertyFunction& property_func):
172  value(value), matchFunc(), propertyFunc(property_func), fixed(true) {}
173 
186  bool operator()(const ObjType& query_obj, const ObjType& target_obj, const Base::Any& aux_data) const;
187 
188  private:
189  ValueType value;
190  MatchFunc matchFunc;
191  PropertyFunction propertyFunc;
192  bool fixed;
193  };
194  } // namespace Chem
195 } // namespace CDPL
196 
197 
198 // Implementation
199 
200 template <typename ValueType, typename MatchFunc, typename ObjType1, typename ObjType2>
201 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType1, ObjType2>::operator()(const ObjType1& query_obj1, const ObjType2& query_obj2,
202  const ObjType1& target_obj1, const ObjType2& target_obj2,
203  const Base::Any&) const
204 {
205  if (fixed)
206  return matchFunc(propertyFunc(target_obj1, target_obj2), value);
207 
208  return matchFunc(propertyFunc(target_obj1, target_obj2), propertyFunc(query_obj1, query_obj2));
209 }
210 
211 
212 template <typename ValueType, typename MatchFunc, typename ObjType>
213 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType, void>::operator()(const ObjType& query_obj, const ObjType& target_obj,
214  const Base::Any&) const
215 {
216  if (fixed)
217  return matchFunc(propertyFunc(target_obj), value);
218 
219  return matchFunc(propertyFunc(target_obj), propertyFunc(query_obj));
220 }
221 
222 #endif // CDPL_CHEM_PROPERTYMATCHEXPRESSION_HPP
Definition of class CDPL::Chem::MatchExpression.
Safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:60
Generic boolean expression interface for the implementation of query/target object equivalence tests ...
Definition: MatchExpression.hpp:75
PropertyMatchExpression(const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:162
PropertyMatchExpression(const ValueType &value, const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:171
std::function< ValueType(const ObjType &)> PropertyFunction
Type of the generic functor class used to store user-defined property accessor functions.
Definition: PropertyMatchExpression.hpp:155
std::shared_ptr< PropertyMatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated PropertyMatchExpression instances...
Definition: PropertyMatchExpression.hpp:147
Generic Chem::MatchExpression specialization for the binary comparison between a query- and a target-...
Definition: PropertyMatchExpression.hpp:65
PropertyMatchExpression(const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:86
std::function< ValueType(const ObjType1 &, const ObjType2 &)> PropertyFunction
Type of the generic functor class used to store user-defined property accessor functions.
Definition: PropertyMatchExpression.hpp:79
PropertyMatchExpression(const ValueType &value, const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:95
bool operator()(const ObjType1 &query_obj1, const ObjType2 &query_obj2, const ObjType1 &target_obj1, const ObjType2 &target_obj2, const Base::Any &aux_data) const
Checks whether the value of the target object propery matches the query property value.
Definition: PropertyMatchExpression.hpp:201
std::shared_ptr< PropertyMatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated PropertyMatchExpression instances...
Definition: PropertyMatchExpression.hpp:71
The namespace of the Chemical Data Processing Library.