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 #ifndef ANDROID_FXLAB_EFFECTDESCRIPTION_H 18 #define ANDROID_FXLAB_EFFECTDESCRIPTION_H 19 20 #include <functional> 21 #include <array> 22 #include <vector> 23 #include <string_view> 24 25 template<class iter_type> 26 using _ef = std::function<void(iter_type, iter_type)>; 27 28 // Only Effect Descriptions should use this namespace 29 namespace Effect { 30 31 class ParamType { 32 public: ParamType(std::string_view name,float minVal,float maxVal,float defVal)33 constexpr ParamType(std::string_view name, float minVal, float maxVal, float defVal) : 34 kName(name), 35 kMinVal(minVal), 36 kMaxVal(maxVal), 37 kDefVal(defVal) {} 38 39 constexpr ParamType(const ParamType &other) = delete; 40 41 ParamType &operator=(const ParamType &other) = delete; 42 43 constexpr ParamType(ParamType &&other) = default; 44 45 constexpr ParamType &operator=(ParamType &&other) = delete; 46 47 const std::string_view kName; 48 const float kMinVal, kMaxVal, kDefVal; 49 }; 50 51 52 // EffectType is the description subclass, N is num of params 53 // Function implementations in this class contain shared behavior 54 // Which can be shadowed. 55 template<class EffectType, size_t N> 56 class EffectDescription { 57 public: 58 // These methods will be shadowed by subclasses 59 getNumParams()60 static constexpr size_t getNumParams() { 61 return N; 62 } 63 getEmptyParams()64 static constexpr std::array<float, N> getEmptyParams() { 65 return std::array<float, EffectType::getNumParams()>(); 66 } 67 68 static constexpr std::string_view getName(); 69 70 static constexpr std::string_view getCategory(); 71 72 static constexpr std::array<ParamType, N> getParams(); 73 74 template<class iter_type> 75 static _ef<iter_type> buildEffect(std::array<float, N> paramArr); 76 77 78 template<class iter_type> buildDefaultEffect()79 static _ef<iter_type> buildDefaultEffect() { 80 auto params = EffectType::getEmptyParams(); 81 int i = 0; 82 for (ParamType &mParam: EffectType::getParams()) { 83 params[i++] = mParam.kDefVal; 84 } 85 return EffectType::template buildEffect<iter_type>(params); 86 } 87 88 // The default behavior is new effect, can be shadowed 89 template<class iter_type> modifyEffect(_ef<iter_type>,std::array<float,N> paramArr)90 static _ef<iter_type> modifyEffect( 91 _ef<iter_type> /* effect */, std::array<float, N> paramArr) { 92 return EffectType::template buildEffect<iter_type>(std::move(paramArr)); 93 } 94 template <class iter_type> modifyEffectVec(_ef<iter_type> effect,std::vector<float> paramVec)95 static _ef<iter_type> modifyEffectVec( 96 _ef<iter_type> effect, std::vector<float> paramVec) { 97 std::array<float, N> arr; 98 std::copy_n(paramVec.begin(), N, arr.begin()); 99 return EffectType::template modifyEffect<iter_type>( 100 std::move(effect), std::move(arr)); 101 } 102 }; 103 104 } // namespace effect 105 106 107 #endif //ANDROID_FXLAB_EFFECTDESCRIPTION_H 108