summaryrefslogtreecommitdiffhomepage
path: root/include/CompilerKit/Utilities/DLL.h
blob: 36c511a3037e3e9d52e21749522a763c1ad5a0ac (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
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (See accompanying
// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
// Official repository: https://github.com/nekernel-org/nectar

#ifndef NECTAR_COMPILERKIT_UTILITIES_DLL_H
#define NECTAR_COMPILERKIT_UTILITIES_DLL_H

#include <CompilerKit/Detail/Config.h>
#include <CompilerKit/Ref.h>
#include <dlfcn.h>
#include <mutex>

namespace CompilerKit {

#ifdef CK_POSIX
class ModuleLoader final {
 public:
  using EntryT  = Int32 (*)(Int32 argc, char const* argv[]);
  using HandleT = VoidPtr;
  using MutexT  = std::mutex;

  EntryT fEntrypoint{nullptr};

 private:
  HandleT mDLL{nullptr};
  MutexT  mMutex;

 public:
  explicit operator bool() { return this->mDLL != nullptr; }

  ModuleLoader& operator()(const std::string& path, const std::string& entrypoint) {
    if (path.empty() || entrypoint.empty()) return *this;

    std::lock_guard<MutexT> lock(this->mMutex);

    if (this->mDLL) {
      this->Reset();
    }

    this->mDLL = ::dlopen(path.data(), RTLD_LAZY);

    if (!this->mDLL) {
      return *this;
    }

    this->fEntrypoint = reinterpret_cast<EntryT>(::dlsym(this->mDLL, entrypoint.data()));

    if (!this->fEntrypoint) {
      this->Reset();
      return *this;
    }

    return *this;
  }

  NECTAR_COPY_DELETE(ModuleLoader)

  ModuleLoader() = default;
  ~ModuleLoader() { this->Reset(); }

  void Reset() noexcept {
    if (this->mDLL) {
      ::dlclose(this->mDLL);
      this->mDLL = nullptr;
    }

    this->fEntrypoint = nullptr;
  }
};

using StrongDLLRef = StrongRef<ModuleLoader>;
using WeakDLLRef   = WeakRef<ModuleLoader>;
#else
#error No ModuleLoader defined.
#endif

}  // namespace CompilerKit

#endif  // NECTAR_COMPILERKIT_UTILITIES_DLL_H