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 */ 17 18 #ifndef ANDROID_FXLAB_FUNCTIONLIST_H 19 #define ANDROID_FXLAB_FUNCTIONLIST_H 20 21 #include <vector> 22 #include <functional> 23 #include <array> 24 25 template<class iter_type> 26 class FunctionList { 27 std::vector<std::pair<std::function<void(iter_type, iter_type)>, bool>> functionList; 28 bool muted = false; 29 public: 30 FunctionList() = default; 31 32 FunctionList(const FunctionList &) = delete; 33 34 FunctionList &operator=(const FunctionList &) = delete; 35 36 operator()37 void operator()(iter_type begin, iter_type end) { 38 for (auto &f : functionList) { 39 if (f.second == true) std::get<0>(f)(begin, end); 40 } 41 if (muted) std::fill(begin, end, 0); 42 } 43 addEffect(std::function<void (iter_type,iter_type)> f)44 void addEffect(std::function<void(iter_type, iter_type)> f) { 45 functionList.emplace_back(std::move(f), true); 46 } 47 removeEffectAt(unsigned int index)48 void removeEffectAt(unsigned int index) { 49 if (index < functionList.size()) { 50 functionList.erase(std::next(functionList.begin(), index)); 51 } 52 } 53 rotateEffectAt(unsigned int from,unsigned int to)54 void rotateEffectAt(unsigned int from, unsigned int to) { 55 auto &v = functionList; 56 if (from >= v.size() || to >= v.size()) return; 57 if (from <= to) { 58 std::rotate(v.begin() + from, v.begin() + from + 1, v.begin() + to + 1); 59 } else { 60 from = v.size() - 1 - from; 61 to = v.size() - 1 - to; 62 std::rotate(v.rbegin() + from, v.rbegin() + from + 1, v.rbegin() + to + 1); 63 } 64 } 65 modifyEffectAt(size_t index,std::function<void (iter_type,iter_type)> fun)66 void modifyEffectAt(size_t index, std::function<void(iter_type, iter_type)> fun) { 67 functionList[index] = {std::move(fun), functionList[index].second}; 68 } 69 enableEffectAt(size_t index,bool enable)70 void enableEffectAt(size_t index, bool enable) { 71 functionList[index].second = enable; 72 } 73 mute(bool toMute)74 void mute(bool toMute) { 75 muted = toMute; 76 } 77 getType()78 auto getType() { 79 return iter_type(); 80 } 81 82 }; 83 84 #endif //ANDROID_FXLAB_FUNCTIONLIST_H 85 86