summaryrefslogtreecommitdiffstats
path: root/firmware/os/algos/calibration/online_calibration/common_data/calibration_callback.h
blob: 62bc443d7f56a4895067a7efbbfe9422bec21113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * This module provides the callback functionality employed by the online sensor
 * calibration algorithms.
 */

#ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_
#define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_

#include "calibration/online_calibration/common_data/calibration_data.h"

namespace online_calibration {

/*
 * This codebase avoids Standard Template Library (STL) containers to maximize
 * its usefullness on embedded hardware with basic C++ compiler support. The
 * following uses type erasure to implement callback functionality to a user's
 * desired class method. The idea here is to store an object pointer by
 * instantiating a class template (Callback) which implements a virtual
 * interface function (CallbackInterface::Call).
 *
 * USAGE:
 * See unit testing for a simple example of how to use this for callback
 * functionality.
 */

// CalibrationType: Sets the calibration type (e.g., CalibrationDataThreeAxis).
template <class CalibrationType>
class CallbackInterface {
 public:
  // Interface function that is defined to implement the desired callback.
  virtual void Call(const CalibrationType& cal_data,
                    CalibrationTypeFlags cal_update_flags) = 0;
  virtual ~CallbackInterface() {}
};

// ClassCallerType: Sets the object's class type for the callback.
// CalibrationType: Sets the calibration type (e.g., CalibrationDataThreeAxis).
template <class ClassCallerType, class CalibrationType>
class Callback : public CallbackInterface<CalibrationType> {
 public:
  // Constructors.
  Callback() = delete;
  Callback(ClassCallerType* obj,
           void (ClassCallerType::*func)(const CalibrationType& cal_data,
                                         CalibrationTypeFlags cal_update_flags))
      : object_ptr_(obj), function_ptr_(func) {}

  // Implements the callback to the desired class-method.
  void Call(const CalibrationType& cal_data,
            CalibrationTypeFlags cal_update_flags) final {
    (object_ptr_->*function_ptr_)(cal_data, cal_update_flags);
  }

 private:
  // Pointer to the class that owns the callback method.
  ClassCallerType* object_ptr_;

  // Templated function pointer with the required function signature.
  // Calibration callbacks must accept:
  //   1. Constant reference to the calibration.
  //   2. Bitmask indicating which calibration components have been updated.
  void (ClassCallerType::*function_ptr_)(const CalibrationType& cal_data,
                                         CalibrationTypeFlags cal_update_flags);
};

}  // namespace online_calibration

#endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_CALIBRATION_CALLBACK_H_