summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/base/c_callback_support.h
blob: aee0ce5346592ac327a29063790cdfabbd722f4d (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
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#ifndef LIB_JXL_BASE_C_CALLBACK_SUPPORT_H_
#define LIB_JXL_BASE_C_CALLBACK_SUPPORT_H_

#include <utility>

namespace jxl {
namespace detail {

template <typename T>
struct MethodToCCallbackHelper {};

template <typename T, typename R, typename... Args>
struct MethodToCCallbackHelper<R (T::*)(Args...)> {
  template <R (T::*method)(Args...)>
  static R Call(void *opaque, Args... args) {
    return (reinterpret_cast<T *>(opaque)->*method)(
        std::forward<Args>(args)...);
  }
};

}  // namespace detail
}  // namespace jxl

#define METHOD_TO_C_CALLBACK(method) \
  ::jxl::detail::MethodToCCallbackHelper<decltype(method)>::Call<method>

#endif  // LIB_JXL_BASE_C_CALLBACK_SUPPORT_H_