Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file FunctionBinding.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Christian Roessel
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @date Fri, 29.04.2005
20 : ///
21 : // Function type template
22 : /****************************************************************************/
23 : #pragma once
24 : #include <config.h>
25 :
26 : #include <utils/common/ValueSource.h>
27 : #include "CastingFunctionBinding.h"
28 :
29 :
30 : // ===========================================================================
31 : // class definitions
32 : // ===========================================================================
33 : /**
34 : * @class FunctionBinding
35 : */
36 : template< class T, typename R >
37 : class FunctionBinding : public ValueSource<R> {
38 : public:
39 : /// Type of the function to execute.
40 : typedef R(T::* Operation)() const;
41 :
42 0 : FunctionBinding(T* const source, Operation operation, const R scale = 1) :
43 0 : mySource(source),
44 0 : myOperation(operation),
45 0 : myScale(scale) {}
46 :
47 : /// Destructor.
48 0 : ~FunctionBinding() {}
49 :
50 0 : R getValue() const {
51 0 : return myScale * (mySource->*myOperation)();
52 : }
53 :
54 0 : ValueSource<R>* copy() const {
55 0 : return new FunctionBinding<T, R>(mySource, myOperation, myScale);
56 : }
57 :
58 0 : ValueSource<double>* makedoubleReturningCopy() const {
59 0 : return new CastingFunctionBinding<T, double, R>(mySource, myOperation, myScale);
60 : }
61 :
62 : private:
63 : /// The object the action is directed to.
64 : T* mySource;
65 :
66 : /// The object's operation to perform.
67 : Operation myOperation;
68 :
69 : /// The scale to apply.
70 : const R myScale;
71 :
72 : private:
73 : /// @brief invalidated assignment operator
74 : FunctionBinding<T, R>& operator=(const FunctionBinding<T, R>&);
75 :
76 : };
77 :
78 : template<class T>
79 : class FunctionBindingString : public ValueSource<std::string> {
80 : public:
81 : /// Type of the function to execute.
82 : typedef std::string(T::* Operation)() const;
83 :
84 0 : FunctionBindingString(T* const source, Operation operation) :
85 0 : mySource(source),
86 0 : myOperation(operation)
87 : {}
88 :
89 : /// Destructor.
90 0 : ~FunctionBindingString() {}
91 :
92 0 : std::string getValue() const {
93 0 : return (mySource->*myOperation)();
94 : }
95 :
96 0 : ValueSource<std::string>* copy() const {
97 0 : return new FunctionBindingString<T>(mySource, myOperation);
98 : }
99 :
100 0 : ValueSource<double>* makedoubleReturningCopy() const {
101 0 : return nullptr;
102 : }
103 :
104 : private:
105 : /// The object the action is directed to.
106 : T* mySource;
107 :
108 : /// The object's operation to perform.
109 : Operation myOperation;
110 :
111 : private:
112 : /// @brief invalidated assignment operator
113 : FunctionBindingString<T>& operator=(const FunctionBindingString<T>&);
114 :
115 : };
|