Chemical Data Processing Library C++ API - Version 1.2.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 
55  template <typename ValueType, typename MatchFunc, typename ObjType1, typename ObjType2 = void>
56  class PropertyMatchExpression : public MatchExpression<ObjType1, ObjType2>
57  {
58 
59  public:
63  typedef std::shared_ptr<PropertyMatchExpression> SharedPointer;
64 
71  typedef std::function<ValueType(const ObjType1&, const ObjType2&)> PropertyFunction;
72 
79  value(), matchFunc(), propertyFunc(property_func), fixed(false) {}
80 
87  PropertyMatchExpression(const ValueType& value, const PropertyFunction& property_func):
88  value(value), matchFunc(), propertyFunc(property_func), fixed(true) {}
89 
104  bool operator()(const ObjType1& query_obj1, const ObjType2& query_obj2, const ObjType1& target_obj1,
105  const ObjType2& target_obj2, const Base::Any& aux_data) const;
106 
107  private:
108  ValueType value;
109  MatchFunc matchFunc;
110  PropertyFunction propertyFunc;
111  bool fixed;
112  };
113 
124  template <typename ValueType, typename MatchFunc, typename ObjType>
125  class PropertyMatchExpression<ValueType, MatchFunc, ObjType, void> : public MatchExpression<ObjType, void>
126  {
127 
128  public:
132  typedef std::shared_ptr<PropertyMatchExpression> SharedPointer;
133 
140  typedef std::function<ValueType(const ObjType&)> PropertyFunction;
141 
148  value(), matchFunc(), propertyFunc(property_func), fixed(false) {}
149 
156  PropertyMatchExpression(const ValueType& value, const PropertyFunction& property_func):
157  value(value), matchFunc(), propertyFunc(property_func), fixed(true) {}
158 
171  bool operator()(const ObjType& query_obj, const ObjType& target_obj, const Base::Any& aux_data) const;
172 
173  private:
174  ValueType value;
175  MatchFunc matchFunc;
176  PropertyFunction propertyFunc;
177  bool fixed;
178  };
179  } // namespace Chem
180 } // namespace CDPL
181 
182 
183 // Implementation
184 
185 template <typename ValueType, typename MatchFunc, typename ObjType1, typename ObjType2>
186 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType1, ObjType2>::operator()(const ObjType1& query_obj1, const ObjType2& query_obj2,
187  const ObjType1& target_obj1, const ObjType2& target_obj2,
188  const Base::Any&) const
189 {
190  if (fixed)
191  return matchFunc(propertyFunc(target_obj1, target_obj2), value);
192 
193  return matchFunc(propertyFunc(target_obj1, target_obj2), propertyFunc(query_obj1, query_obj2));
194 }
195 
196 
197 template <typename ValueType, typename MatchFunc, typename ObjType>
198 bool CDPL::Chem::PropertyMatchExpression<ValueType, MatchFunc, ObjType, void>::operator()(const ObjType& query_obj, const ObjType& target_obj,
199  const Base::Any&) const
200 {
201  if (fixed)
202  return matchFunc(propertyFunc(target_obj), value);
203 
204  return matchFunc(propertyFunc(target_obj), propertyFunc(query_obj));
205 }
206 
207 #endif // CDPL_CHEM_PROPERTYMATCHEXPRESSION_HPP
Definition of the class CDPL::Chem::MatchExpression.
A safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:59
A generic boolean expression interface for the implementation of query/target object equivalence test...
Definition: MatchExpression.hpp:75
PropertyMatchExpression(const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:147
PropertyMatchExpression(const ValueType &value, const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:156
std::function< ValueType(const ObjType &)> PropertyFunction
Type of the generic functor class used to store user-defined property accessor functions.
Definition: PropertyMatchExpression.hpp:140
std::shared_ptr< PropertyMatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated PropertyMatchExpression instances...
Definition: PropertyMatchExpression.hpp:132
PropertyMatchExpression.
Definition: PropertyMatchExpression.hpp:57
PropertyMatchExpression(const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:78
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:71
PropertyMatchExpression(const ValueType &value, const PropertyFunction &property_func)
Constructs a PropertyMatchExpression instance that performs query/target object equivalence tests bas...
Definition: PropertyMatchExpression.hpp:87
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:186
std::shared_ptr< PropertyMatchExpression > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated PropertyMatchExpression instances...
Definition: PropertyMatchExpression.hpp:63
The namespace of the Chemical Data Processing Library.