summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/NeKit/Function.h
blob: 55a4836ffce47e006b0518e634beeaa413f386a1 (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
// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/ne-foss-org/nekernel

#ifndef NEKIT_FUNCTION_H
#define NEKIT_FUNCTION_H

#include <NeKit/Config.h>
#include <NeKit/ErrorOr.h>

namespace Kernel {

/// @brief Function Pointer Container.
template <typename T, typename... Args>
class Function final {
 public:
  Function()        = delete;
  Function(nullPtr) = delete;

  ~Function() = default;
  Function(T (*Fn)(Args... args)) : fFn(Fn) { MUST_PASS(fFn); }

  Function& operator=(const Function&) = delete;
  Function(const Function&)            = delete;

  T operator()(Args&&... args) { return fFn(forward<Args...>(args)...); }

  explicit operator bool() { return fFn; }
  bool     operator!() { return !fFn; }

 private:
  T (*fFn)(Args... args){nullptr};
};

template <typename T, typename... Args>
using FunctionOr = ErrorOr<Function<T, Args...>>;

template <typename T, typename... Args>
using FunctionRef = Ref<Function<T, Args...>>;

}  // namespace Kernel

#endif