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_COLLECTION_FUNCTION_NAME_SYMBOL "SIMO_COLLECTION_FUNCTION"
26
27#define SIMO_COLLECTION_FUNCTION_NAME_DEFAULT "simo_get_collection"
28
29namespace Simo {
30class Module;
31class Parameters;
32
33namespace SIMO_PUBLIC Collections {
34struct Factory {
35 [[nodiscard]] std::string_view get_name() const noexcept;
36
37 [[nodiscard]] Module* get_module_unsafe() const;
38
39 [[nodiscard]] Parameters* get_parameters_unsafe() const;
40
41 [[nodiscard]] std::unique_ptr<Module> get_module() const;
42
43 [[nodiscard]] std::unique_ptr<Parameters> get_parameters() const;
44
45 const char* name;
46 Module* (*get_module_function)();
47 Parameters* (*get_parameters_function)();
48};
49
50struct SIMO_PUBLIC SimoCollectionVersion {
51 unsigned major;
52 unsigned minor;
53 unsigned patch;
54
55 bool operator==(const SimoCollectionVersion& simo_collection_version) const;
56};
57
58struct SIMO_PUBLIC SimoCollection {
59 const char* name;
61 const Factory* factory_list;
62 unsigned factory_list_size;
63
64 void check() const noexcept;
65
66 [[nodiscard]] const Factory* get_factory(
67 const std::string_view& factory_name) const noexcept;
68
69 [[nodiscard]] std::vector<std::pair<std::string_view, const Factory*>>
70 get_factory_pairs() const noexcept;
71};
72
76class SIMO_PUBLIC CollectionWithLib {
77 public:
78 CollectionWithLib(const std::filesystem::path& path,
79 const SimoCollection* collection,
80 void* lib_handle) noexcept;
81
82 CollectionWithLib(const CollectionWithLib&) = delete;
83 CollectionWithLib& operator=(const CollectionWithLib&) = delete;
84
85 CollectionWithLib(CollectionWithLib&& other) noexcept;
86
87 CollectionWithLib& operator=(CollectionWithLib&& other) noexcept;
88
89 ~CollectionWithLib();
90
91 [[nodiscard]] const SimoCollection* get_collection() const noexcept;
92
93 private:
94 std::filesystem::path path;
95 const SimoCollection* collection;
96 void* lib_handle = nullptr;
97};
98
99enum struct SIMO_PUBLIC GET_COLLECTION_ERROR : std::uint8_t {
100 DLOPEN_FAIL,
101 NO_FUNCTION_NAME,
102 NOT_A_DIRECTORY,
103 ALREADY_LOADED_LIBRARY
104};
105
106struct SIMO_PUBLIC GetCollectionError {
107 GET_COLLECTION_ERROR error_code;
108 std::string error_message;
109};
110
112[[nodiscard]] std::expected<CollectionWithLib, GetCollectionError> SIMO_PUBLIC
113simo_get_collection(const std::filesystem::path& path_to_collection);
114
115[[nodiscard]] std::expected<std::vector<CollectionWithLib>, GetCollectionError>
116 SIMO_PUBLIC
117 simo_get_collection_from_folder(const std::filesystem::path& folder_path);
118} // namespace SIMO_PUBLIC Collections
119} // namespace Simo
120
121#endif // SIMO_COLLECTION_HH
Definition Module.h:83
Collects parameters that are then passed to a Module instance.
Definition Module.h:30
Definition Collection.h:34
Definition Collection.h:106
Definition Collection.h:58