Knoodle
Knoodle is a Free Open-Source alternative for graph-based texture designers. It is a tool for creating procedural textures using a node-based graph editor. It is designed to be easy to use, to be able to create complex textures with a minimum of effort, and to be pluggable to any game engine and front-end.
 
Loading...
Searching...
No Matches
os.hpp
1/**************************************************************************/
2/* os.hpp */
3/**************************************************************************/
4/* This file is part of: */
5/* Knoodle */
6/* https://knoodlegraph.org */
7/**************************************************************************/
8/* Copyright (c) 2025 Knoodle contributors (vide AUTHORS.md) */
9/* */
10/* Permission is hereby granted, free of charge, to any person obtaining */
11/* a copy of this software and associated documentation files (the */
12/* "Software"), to deal in the Software without restriction, including */
13/* without limitation the rights to use, copy, modify, merge, publish, */
14/* distribute, sublicense, and/or sell copies of the Software, and to */
15/* permit persons to whom the Software is furnished to do so, subject to */
16/* the following conditions: */
17/* */
18/* The above copyright notice and this permission notice shall be */
19/* included in all copies or substantial portions of the Software. */
20/* */
21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28/**************************************************************************/
29
30#pragma once
31
32#include <filesystem>
33#include "common.hpp"
34#include "core_api.hpp"
35
36namespace kn {
37class os {
38 public:
39 /**
40 * Represents a dynamic library.
41 */
42 class Library {
43 friend class os;
44
45 explicit Library(void* handle, std::filesystem::path path) : _handle(handle), _path(std::move(path)) {}
46
47 public:
48 Library(const Library&) = delete;
49 Library& operator=(const Library&) = delete;
50
51 ~Library() {
52 if (is_loaded()) {
53 os::free_library(_handle);
54 }
55 }
56
57 template <typename T>
58 [[nodiscard]] inline T get_function(const char* name) const {
59 return reinterpret_cast<T>(get_function_impl(_handle, name));
60 }
61
62 /** Returns the native handle of the library. */
63 [[nodiscard]] inline void* get_os_handle() const { return _handle; }
64
65 /** Returns the path of the library. */
66 [[nodiscard]] inline std::filesystem::path get_path() const { return _path; }
67
68 /** Checks if the library is loaded. */
69 [[nodiscard]] inline bool is_loaded() const { return _handle != nullptr; }
70
71 explicit operator bool() const { return is_loaded(); }
72
73 private:
74 void* _handle;
75 std::filesystem::path _path;
76 };
77
78 /**
79 * Loads a dynamic library.
80 * @param[in] path The path to the dynamic library.
81 * @return pointer to the loaded library.
82 */
83 [[nodiscard]] inline static Library* load_library(std::filesystem::path path) {
84 return load_library_impl(std::move(path));
85 }
86
87 /**
88 * Frees a dynamic library.
89 * @param[in] library The library to free.
90 */
91 inline static bool free_library(void* library) { return free_library_impl(library); }
92
93 private:
94 static KN_CORE_API Library* load_library_impl(const std::filesystem::path& path);
95 static KN_CORE_API bool free_library_impl(void* library);
96 static KN_CORE_API void* get_function_impl(void* library, const char* name);
97};
98} // namespace kn
Definition os.hpp:42
std::filesystem::path get_path() const
Definition os.hpp:66
void * get_os_handle() const
Definition os.hpp:63
bool is_loaded() const
Definition os.hpp:69
Definition os.hpp:37
static bool free_library(void *library)
Definition os.hpp:91
static Library * load_library(std::filesystem::path path)
Definition os.hpp:83