1 /*
2  * Copyright  2019 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ANDROID_FXLAB_DRIVECONTROL_H
17 #define ANDROID_FXLAB_DRIVECONTROL_H
18 
19 
20 #include <functional>
21 
22 template <class iter_type>
23 
24 class DriveControl {
25 public:
26 
DriveControl(std::function<void (iter_type,iter_type)> function,double scale)27     DriveControl(std::function<void(iter_type, iter_type)> function, double scale):
28         mFunction(function), kScale(scale) {}
29 
operator()30     void operator() (iter_type beg, iter_type end) {
31         std::for_each(beg, end, [this](auto &x){x *= kScale;});
32         mFunction(beg, end);
33         std::for_each(beg, end, [this](auto &x){x *= recip;});
34     }
35 
36 private:
37     std::function<void(iter_type, iter_type)> mFunction;
38     const double kScale;
39     const double recip = 1 / kScale;
40 };
41 #endif //ANDROID_FXLAB_DRIVECONTROL_H
42