1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 * http://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 * This module provides the callback functionality employed by the online sensor 19 * calibration algorithms. 20 */ 21 22 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_ 23 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_ 24 25 #include "calibration/online_calibration/common_data/calibration_data.h" 26 27 namespace online_calibration { 28 29 /* 30 * This codebase avoids Standard Template Library (STL) containers to maximize 31 * its usefullness on embedded hardware with basic C++ compiler support. The 32 * following uses type erasure to implement callback functionality to a user's 33 * desired class method. The idea here is to store an object pointer by 34 * instantiating a class template (Callback) which implements a virtual 35 * interface function (CallbackInterface::Call). 36 * 37 * USAGE: 38 * See unit testing for a simple example of how to use this for callback 39 * functionality. 40 */ 41 42 // CalibrationType: Sets the calibration type (e.g., CalibrationDataThreeAxis). 43 template <class CalibrationType> 44 class CallbackInterface { 45 public: 46 // Interface function that is defined to implement the desired callback. 47 virtual void Call(const CalibrationType& cal_data, 48 CalibrationTypeFlags cal_update_flags) = 0; ~CallbackInterface()49 virtual ~CallbackInterface() {} 50 }; 51 52 // ClassCallerType: Sets the object's class type for the callback. 53 // CalibrationType: Sets the calibration type (e.g., CalibrationDataThreeAxis). 54 template <class ClassCallerType, class CalibrationType> 55 class Callback : public CallbackInterface<CalibrationType> { 56 public: 57 // Constructors. 58 Callback() = delete; Callback(ClassCallerType * obj,void (ClassCallerType::* func)(const CalibrationType & cal_data,CalibrationTypeFlags cal_update_flags))59 Callback(ClassCallerType* obj, 60 void (ClassCallerType::*func)(const CalibrationType& cal_data, 61 CalibrationTypeFlags cal_update_flags)) 62 : object_ptr_(obj), function_ptr_(func) {} 63 64 // Implements the callback to the desired class-method. Call(const CalibrationType & cal_data,CalibrationTypeFlags cal_update_flags)65 void Call(const CalibrationType& cal_data, 66 CalibrationTypeFlags cal_update_flags) final { 67 (object_ptr_->*function_ptr_)(cal_data, cal_update_flags); 68 } 69 70 private: 71 // Pointer to the class that owns the callback method. 72 ClassCallerType* object_ptr_; 73 74 // Templated function pointer with the required function signature. 75 // Calibration callbacks must accept: 76 // 1. Constant reference to the calibration. 77 // 2. Bitmask indicating which calibration components have been updated. 78 void (ClassCallerType::*function_ptr_)(const CalibrationType& cal_data, 79 CalibrationTypeFlags cal_update_flags); 80 }; 81 82 } // namespace online_calibration 83 84 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_ 85