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 
137  template <typename ValueType, typename MatchFunc, typename ObjType>
138  class PropertyMatchExpression<ValueType, MatchFunc, ObjType, void> : public MatchExpression<ObjType, void>
139  {
140 
141  public:
145  typedef std::shared_ptr<PropertyMatchExpression> SharedPointer;
146 
153  typedef std::function<ValueType(const ObjType&)> PropertyFunction;
154 
161  value(), matchFunc(), propertyFunc(property_func), fixed(false) {}
162 
169  PropertyMatchExpression(const ValueType& value, const PropertyFunction& property_func):
170  value(value), matchFunc(), propertyFunc(property_func), fixed(true) {}
171 
184  bool operator()(const ObjType& query_obj, const ObjType& target_obj, const Base::Any& aux_data) const;
185 
186  private:
187  ValueType value;
188  MatchFunc matchFunc;
189  PropertyFunction propertyFunc;
190  bool fixed;
191  };
192  } // namespace Chem
193 } // namespace CDPL
194 
195 
196 // Implementation
197 
198 template <typename ValueType, typename MatchFunc, typename ObjType1, typename ObjType2>
199 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType1, ObjType2>::operator()(const ObjType1& query_obj1, const ObjType2& query_obj2,
200  const ObjType1& target_obj1, const ObjType2& target_obj2,
201  const Base::Any&) const
202 {
203  if (fixed)
204  return matchFunc(propertyFunc(target_obj1, target_obj2), value);
205 
206  return matchFunc(propertyFunc(target_obj1, target_obj2), propertyFunc(query_obj1, query_obj2));
207 }
208 
209 
210 template <typename ValueType, typename MatchFunc, typename ObjType>
211 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType, void>::operator()(const ObjType& query_obj, const ObjType& target_obj,
212  const Base::Any&) const
213 {
214  if (fixed)
215  return matchFunc(propertyFunc(target_obj), value);
216 
217  return matchFunc(propertyFunc(target_obj), propertyFunc(query_obj));
218 }
219 
220 #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:160
PropertyMatchExpression(const ValueType &value, const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:169
std::function< ValueType(const ObjType &)> PropertyFunction
Type of the generic functor class used to store user-defined property accessor functions.
Definition: PropertyMatchExpression.hpp:153
std::shared_ptr< PropertyMatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated PropertyMatchExpression instances...
Definition: PropertyMatchExpression.hpp:145
Generic Chem::MatchExpression specialisation that evaluates a binary comparison between a query- and ...
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:199
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.