]>
Commit | Line | Data |
---|---|---|
9f80e690 | 1 | //-*- Mode: C++ -*- |
2 | // $Id$ | |
3 | #ifndef ALIHLTTTEMPLATES_H | |
4 | #define ALIHLTTTEMPLATES_H | |
5 | //* This file is property of and copyright by the ALICE HLT Project * | |
6 | //* ALICE Experiment at CERN, All rights reserved. * | |
7 | //* See cxx source for full Copyright notice * | |
8 | ||
9 | /// @file AliHLTTemplates.h | |
10 | /// @author Matthias Richter | |
11 | /// @date 2011-04-29 | |
12 | /// @brief A collection of HLT template definitions | |
13 | /// | |
14 | ||
15 | namespace HLT | |
16 | { | |
17 | // unary predicate | |
18 | // checks if return value (type T) of a specified member function of a class C | |
19 | // instance has a certain value | |
20 | // used together with stl algorithms to extract specific predicate from the class | |
21 | template <class C, typename T> | |
22 | class AliHLTUnaryPredicate { | |
23 | public: | |
24 | /// constructor | |
25 | AliHLTUnaryPredicate(T (C::*pFct)() const, T value) | |
26 | : fpFct(pFct), fValue(value) { }; | |
27 | ||
4fd9cf87 | 28 | /// copy contructor |
29 | AliHLTUnaryPredicate(const AliHLTUnaryPredicate& p) | |
30 | : fpFct(p.fpFct), fValue(p.fValue) { }; | |
31 | /// assignment operator | |
32 | AliHLTUnaryPredicate& operator=(const AliHLTUnaryPredicate& p) { | |
33 | fpFct=p.fpFct; fValue=p.fValue; return *this; | |
34 | } | |
35 | ||
9f80e690 | 36 | /// override operator== execute member function and compare result to value |
37 | bool operator==(const C& object) const { | |
4fd9cf87 | 38 | if (!fpFct) return false; |
9f80e690 | 39 | return (object.*fpFct)()==fValue; |
40 | } | |
41 | ||
42 | private: | |
43 | /// standard contructor prohibited | |
44 | AliHLTUnaryPredicate(); | |
9f80e690 | 45 | |
46 | T (C::*fpFct)() const; //! pointer to member function | |
47 | T fValue; //! value to match | |
48 | }; | |
49 | ||
50 | template <class C, typename T> | |
51 | class AliHLTGetValue { | |
52 | public: | |
53 | /// constructor | |
54 | AliHLTGetValue(const C& object, T (C::*pFct)() const) | |
55 | : fObject(object), fpFct(pFct) { }; | |
56 | ||
57 | /// override operator== execute member function and compare result to value | |
58 | operator T() const { | |
59 | return (fObject.*fpFct)(); | |
60 | } | |
61 | ||
62 | private: | |
63 | /// standard contructor prohibited | |
64 | AliHLTGetValue(); | |
65 | /// copy contructor prohibited | |
66 | AliHLTGetValue(const AliHLTGetValue&); | |
67 | /// assignment operator prohibited | |
68 | AliHLTGetValue& operator=(const AliHLTGetValue&); | |
69 | ||
70 | const T& fObject; //! object | |
71 | T (C::*fpFct)() const; //! pointer to member function | |
72 | }; | |
73 | ||
74 | // operator== to be used as predicates for condition classes in stl algorithms | |
75 | // need to change the order of the to parameters in order to get | |
76 | // into the operator== of the condition class | |
77 | template <class T, class C> | |
78 | bool operator==(const T& p, const C& c) | |
79 | { | |
80 | return c==p; | |
81 | } | |
82 | ||
83 | // operator== to be used as predicates for condition classes in stl algorithms | |
84 | // template for maps, use value of the pair in the condition | |
85 | // need to change the order of the to parameters in order to get | |
86 | // into the operator== of the condition class | |
87 | template <class K, class T, class C> | |
88 | bool operator==(const std::pair<K, T>& p, const C& c) | |
89 | { | |
90 | return c==p.second; | |
91 | } | |
92 | ||
93 | // copy function for maps | |
94 | // stl algorithms can not be used here because pair.first can not be assigned | |
95 | // in maps but has to be used as index | |
96 | template <class InputIterator, class K, class V, class C> | |
97 | int copy_map_if ( InputIterator first, InputIterator last, | |
98 | std::map<K, V>& result, const C& value ) | |
99 | { | |
100 | for ( ; first != last; ++first) | |
101 | if ((*first == value)) result[first->first] = first->second; | |
102 | return 0; | |
103 | } | |
104 | ||
105 | // get the key of a pair | |
106 | class AliGetKey { | |
107 | public: | |
108 | template <typename T> | |
109 | typename T::first_type operator()(T pair) const { | |
110 | return pair.first; | |
111 | } | |
112 | }; | |
113 | ||
114 | } // end of namespace | |
115 | #endif |