]>
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 | ||
28 | /// override operator== execute member function and compare result to value | |
29 | bool operator==(const C& object) const { | |
30 | return (object.*fpFct)()==fValue; | |
31 | } | |
32 | ||
33 | private: | |
34 | /// standard contructor prohibited | |
35 | AliHLTUnaryPredicate(); | |
36 | /// copy contructor prohibited | |
37 | AliHLTUnaryPredicate(const AliHLTUnaryPredicate&); | |
38 | /// assignment operator prohibited | |
39 | AliHLTUnaryPredicate& operator=(const AliHLTUnaryPredicate&); | |
40 | ||
41 | T (C::*fpFct)() const; //! pointer to member function | |
42 | T fValue; //! value to match | |
43 | }; | |
44 | ||
45 | template <class C, typename T> | |
46 | class AliHLTGetValue { | |
47 | public: | |
48 | /// constructor | |
49 | AliHLTGetValue(const C& object, T (C::*pFct)() const) | |
50 | : fObject(object), fpFct(pFct) { }; | |
51 | ||
52 | /// override operator== execute member function and compare result to value | |
53 | operator T() const { | |
54 | return (fObject.*fpFct)(); | |
55 | } | |
56 | ||
57 | private: | |
58 | /// standard contructor prohibited | |
59 | AliHLTGetValue(); | |
60 | /// copy contructor prohibited | |
61 | AliHLTGetValue(const AliHLTGetValue&); | |
62 | /// assignment operator prohibited | |
63 | AliHLTGetValue& operator=(const AliHLTGetValue&); | |
64 | ||
65 | const T& fObject; //! object | |
66 | T (C::*fpFct)() const; //! pointer to member function | |
67 | }; | |
68 | ||
69 | // operator== to be used as predicates for condition classes in stl algorithms | |
70 | // need to change the order of the to parameters in order to get | |
71 | // into the operator== of the condition class | |
72 | template <class T, class C> | |
73 | bool operator==(const T& p, const C& c) | |
74 | { | |
75 | return c==p; | |
76 | } | |
77 | ||
78 | // operator== to be used as predicates for condition classes in stl algorithms | |
79 | // template for maps, use value of the pair in the condition | |
80 | // need to change the order of the to parameters in order to get | |
81 | // into the operator== of the condition class | |
82 | template <class K, class T, class C> | |
83 | bool operator==(const std::pair<K, T>& p, const C& c) | |
84 | { | |
85 | return c==p.second; | |
86 | } | |
87 | ||
88 | // copy function for maps | |
89 | // stl algorithms can not be used here because pair.first can not be assigned | |
90 | // in maps but has to be used as index | |
91 | template <class InputIterator, class K, class V, class C> | |
92 | int copy_map_if ( InputIterator first, InputIterator last, | |
93 | std::map<K, V>& result, const C& value ) | |
94 | { | |
95 | for ( ; first != last; ++first) | |
96 | if ((*first == value)) result[first->first] = first->second; | |
97 | return 0; | |
98 | } | |
99 | ||
100 | // get the key of a pair | |
101 | class AliGetKey { | |
102 | public: | |
103 | template <typename T> | |
104 | typename T::first_type operator()(T pair) const { | |
105 | return pair.first; | |
106 | } | |
107 | }; | |
108 | ||
109 | } // end of namespace | |
110 | #endif |