VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
UbComparators.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef UBCOMPARATORS_H
35#define UBCOMPARATORS_H
36
37#include <functional>
38
40{
41// type_traits
42template <typename T>
43struct MemberInfo; // not defined for correct compiler errors!
44
45// specialization for MemberFunctionsPtr
46// C - class with return T method
47template <typename T, typename C>
48struct MemberInfo<T C::*> {
49 using type = T;
50 using class_type = C;
51
52 static T &apply(C &c, T C::*ptr) { return c.*ptr; }
53 static const T &apply(const C &c, T C::*ptr) { return c.*ptr; }
54};
55// specialization for MemberFunctionsPtr
56// C - class with return T method
57template <typename T, typename C>
58struct MemberInfo<T (C::*)()> {
59 using type = T;
60 using class_type = C;
61
62 static T apply(C &c, T (C::*ptr)()) { return (c.*ptr)(); }
63};
64// specialization for const MemberFunctionsPtr
65// C - class with return T method
66template <typename T, typename C>
67struct MemberInfo<T (C::*)() const> {
68 using type = T;
69 using class_type = C;
70
71 static T apply(const C &c, T (C::*ptr)() const) { return (c.*ptr)(); }
72};
73
74// MemberComparative-Class
76class MemComp : private Comp // -> usage of Empty Base Class Optimization (EBCO)
77{
78 using C = typename MemberInfo<Ptr>::class_type;
79
80public:
81 MemComp(Ptr ptr, Comp c = Comp()) : Comp(c), mp_(ptr) {}
82
83 bool operator()(C &lhs, C &rhs)
84 {
85 return Comp::operator()(MemberInfo<Ptr>::apply(lhs, mp_), MemberInfo<Ptr>::apply(rhs, mp_));
86 }
87 bool operator()(C &lhs, C &rhs) const
88 {
89 return Comp::operator()(MemberInfo<Ptr>::apply(lhs, mp_), MemberInfo<Ptr>::apply(rhs, mp_));
90 }
91 bool operator()(const C &lhs, const C &rhs)
92 {
93 return Comp::operator()(MemberInfo<Ptr>::apply(lhs, mp_), MemberInfo<Ptr>::apply(rhs, mp_));
94 }
95 bool operator()(const C &lhs, const C &rhs) const
96 {
97 return Comp::operator()(MemberInfo<Ptr>::apply(lhs, mp_), MemberInfo<Ptr>::apply(rhs, mp_));
98 }
99
100private:
101 Ptr mp_;
102};
103
104// Factoryfunktionen
105template <typename Ptr>
107{
108 return MemComp<Ptr>(p);
109}
110
111template <typename Comp, typename Ptr>
116
117template <template <typename> class Comp, typename Ptr>
123
127// andere Variante (alerdings ist hier keine Deduction moeglich!!!)
129// Vergleichs-Templates:
130// Funktor zum "non-const" Methodenvergleich: liste.sort( compareMethods<Klasse, int, &Klasse::getVal1 );
131template <typename K /*Klasse*/, typename M /*MethodenRueckgabeTyp*/,
132 M (K::*fct)() /*MethodenPointer*/> // Allgemeiner Fall
134 bool operator()(K &r, K &l) const // da fct nicht const ist, kann auch K nicht const sein. das const hinter der
135 // deklaration besagt dass compareMethods const sein kann
136 {
137 return (r.*fct)() < (l.*fct)();
138 }
139};
141// Funktor zum "const" Methodenvergleich: liste.sort( compareMethods<Klasse, int, &Klasse::getVal1 );
142template <typename K /*Klasse*/, typename M /*MethodenRueckgabeTyp*/,
143 M (K::*fct)() const /*MethodenPointer*/> // <- hier const
146 const // hier koennen die K's auch const sein, muessen sie aber nicht (const hinzufuegen geht ja problemlos)
147 {
148 return (r.*fct)() < (l.*fct)();
149 }
150};
152// Funktor zum Membervergleich: lise.sort( compareMember<Klasse, int, &Klasse::member>() );
153template <typename K /*Klasse*/, typename M /*MemberTyp*/, M(K::*Member) /*MemberPointer*/> // <- hier const
157// Bsp:
158// class Klasse{
159// public:
160// Klasse(double val1, double val2 ) : val1(val1),val2(val2) {}
161// double getVal1() { return val1; }
162// double getVal2() const { return val2; } // <- hier const
163// double val1, val2;
164//};
165// int main(int argc, char** argv){
166// std::list<Klasse> l;
167// l.push_back( Klasse(10,10) );
168// l.push_back( Klasse(1,5) );
169// l.sort( compareMember<Klasse, double, &Klasse::val1 >() );
170// l.sort( compareMethods<Klasse, double, &Klasse::getVal1 >() );
171// l.sort( compareConstMethods<Klasse, double, &Klasse::getVal1 >() );
172//}
173
174} // namespace ub_comparators
175
176#endif // UBCOMPARATOR_H
177
178// example
179// #include <basics/utilities/ub_comparators.h"
180// #include <list>
181// using namespace std;
182// using namespace ub_comparators;
183//
184// struct S {
185// S(int i) :x(i) {}
186// int x;
187// float f() {return x;};
188// double g() const {return x;}
189// };
190//
191// struct intComp {
192// bool operator()(int l, int r) const
193// { return l > r; }
194// };
195//
196// struct dblComp {
197// bool operator()(double l, double r) const
198// { return l > r; }
199// };
200//
201// template <typename T>
202// struct genComp {
203// bool operator()(const T& l, const T& r) const
204// { return l > r; }
205// };
206//
207//
208// int main()
209// {
210// S a(1);
211// S b(2);
212// list<S> sList;
213// sList.push_back(a);
214// sList.push_back(b);
215// sList.sort(ub_comparators::membercomp(&S::x,intComp())); //calls overload (1)
216// sList.sort(ub_comparators::membercomp<intComp>(&S::x)); //same
217// sList.sort(ub_comparators::membercomp(&S::x)); //calls overload (5)
218// sList.sort(ub_comparators::membercomp<genComp>(&S::x)); //calls overload(3)
219// sList.sort(ub_comparators::membercomp(&S::x, genComp<int>())); //calls overload(1)
220// //same for nonconst function
221// sList.sort(ub_comparators::membercomp(&S::f, dblComp())); //overload(2)
222// sList.sort(ub_comparators::membercomp<dblComp>(&S::f)); //same
223// sList.sort(ub_comparators::membercomp(&S::f)); //overload(6)
224// sList.sort(ub_comparators::membercomp<genComp>(&S::f)); //overload(4)
225// //same for const function
226// sList.sort(ub_comparators::membercomp(&S::g, dblComp())); //overload(2)
227// sList.sort(ub_comparators::membercomp<dblComp>(&S::g)); //same
228// sList.sort(ub_comparators::membercomp(&S::g)); //overload(6)
229// sList.sort(ub_comparators::membercomp<genComp>(&S::g)); //overload(4)
230// }
231
bool operator()(const C &lhs, const C &rhs)
bool operator()(const C &lhs, const C &rhs) const
MemComp(Ptr ptr, Comp c=Comp())
bool operator()(C &lhs, C &rhs) const
bool operator()(C &lhs, C &rhs)
std::shared_ptr< T > SPtr
MemComp< Ptr > membercomp(Ptr p)
static const T & apply(const C &c, T C::*ptr)
static T & apply(C &c, T C::*ptr)
static T apply(const C &c, T(C::*ptr)() const)
static T apply(C &c, T(C::*ptr)())