Simo 0.0.1
Loading...
Searching...
No Matches
Collection.h
1// Copyright 2026 Matteo Fusi and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SIMO_COLLECTION_HH
16#define SIMO_COLLECTION_HH
17#include <dlfcn.h>
18
19#include <expected>
20#include <filesystem>
21#include <vector>
22
23#include "Simo/module/Module.h"
24
25#define SIMO_STRINGIFY_IMPL(x) #x
26#define SIMO_STRINGIFY(x) SIMO_STRINGIFY_IMPL(x)
27
28#define SIMO_COLLECTION_FUNCTION_NAME_SYMBOL SIMO_COLLECTION_FUNCTION
29#define SIMO_COLLECTION_FUNCTION_NAME_SYMBOL_STR \
30 SIMO_STRINGIFY(SIMO_COLLECTION_FUNCTION_NAME_SYMBOL)
31
32#define SIMO_COLLECTION_FUNCTION_NAME_DEFAULT simo_get_collection
33#define SIMO_COLLECTION_FUNCTION_NAME_DEFAULT_STR \
34 SIMO_STRINGIFY(SIMO_COLLECTION_FUNCTION_NAME_DEFAULT)
35
36#define SIMO_COLLECTION_SECTION_STR ".Simo.collection"
37
38#if defined(__APPLE__)
39#define SIMO_COLLECTION_SECTION "__TEXT," SIMO_COLLECTION_SECTION_STR
40#else
41#define SIMO_COLLECTION_SECTION SIMO_COLLECTION_SECTION_STR
42#endif
43
44#define SIMO_COLLECTION_DECLARATION_NAME(name) \
45 extern SIMO_PUBLIC __attribute__((used, section(SIMO_COLLECTION_SECTION))) \
46 const char SIMO_COLLECTION_FUNCTION_NAME_SYMBOL[] = SIMO_STRINGIFY(name);
47
48#define SIMO_COLLECTION_DECLARATION \
49 SIMO_COLLECTION_DECLARATION_NAME(SIMO_COLLECTION_FUNCTION_NAME_DEFAULT)
50
51namespace Simo {
52class Module;
53class Parameters;
54
55namespace SIMO_PUBLIC Collections {
56struct Factory {
57 [[nodiscard]] std::string_view get_name() const noexcept;
58
59 [[nodiscard]] Module* get_module_unsafe() const;
60
61 [[nodiscard]] Parameters* get_parameters_unsafe() const;
62
63 [[nodiscard]] std::unique_ptr<Module> get_module() const;
64
65 [[nodiscard]] std::unique_ptr<Parameters> get_parameters() const;
66
67 const char* name;
68 Module* (*get_module_function)();
69 Parameters* (*get_parameters_function)();
70};
71
72struct SIMO_PUBLIC SimoCollectionVersion {
73 unsigned major;
74 unsigned minor;
75 unsigned patch;
76
77 bool operator==(const SimoCollectionVersion& simo_collection_version) const;
78};
79
80struct SIMO_PUBLIC SimoCollection {
81 const char* name;
83 const Factory* factory_list;
84 unsigned factory_list_size;
85
86 void check() const noexcept;
87
88 [[nodiscard]] const Factory* get_factory(
89 const std::string_view& factory_name) const noexcept;
90
91 [[nodiscard]] std::vector<std::pair<std::string_view, const Factory*>>
92 get_factory_pairs() const noexcept;
93};
94
98class SIMO_PUBLIC CollectionWithLib {
99 public:
100 CollectionWithLib(const std::filesystem::path& path,
101 const SimoCollection* collection,
102 void* lib_handle) noexcept;
103
104 CollectionWithLib(const CollectionWithLib&) = delete;
105 CollectionWithLib& operator=(const CollectionWithLib&) = delete;
106
107 CollectionWithLib(CollectionWithLib&& other) noexcept;
108
109 CollectionWithLib& operator=(CollectionWithLib&& other) noexcept;
110
111 ~CollectionWithLib();
112
113 [[nodiscard]] const SimoCollection* get_collection() const noexcept;
114
115 private:
116 std::filesystem::path path;
117 const SimoCollection* collection;
118 void* lib_handle = nullptr;
119};
120
121enum struct SIMO_PUBLIC GET_COLLECTION_ERROR : std::uint8_t {
122 DLOPEN_FAIL,
123 NO_FUNCTION_NAME,
124 NOT_A_DIRECTORY,
125 ALREADY_LOADED_LIBRARY,
126 NO_SIMO_COLLECTION_SECTION,
127};
128
129struct SIMO_PUBLIC GetCollectionError {
130 GET_COLLECTION_ERROR error_code;
131 std::string error_message;
132};
133
135[[nodiscard]] std::expected<CollectionWithLib, GetCollectionError> SIMO_PUBLIC
136simo_get_collection(const std::filesystem::path& path_to_collection);
137
138[[nodiscard]] std::expected<std::vector<CollectionWithLib>, GetCollectionError>
139 SIMO_PUBLIC
140 simo_get_collection_from_folder(const std::filesystem::path& folder_path);
141} // namespace SIMO_PUBLIC Collections
142} // namespace Simo
143
144#endif // SIMO_COLLECTION_HH
Definition Module.h:90
Collects parameters that are then passed to a Module instance.
Definition Module.h:33
General namespace for Simo.
Definition Collection.h:51
Definition Collection.h:56
Definition Collection.h:129
Definition Collection.h:80