# shiva::dll

## plugins\_registry:

### **Description**

This class allows you to store plugin creation symbols through its template ***CreatorSignature*** parameter.\
Through this class you are able to retrieve the number of plugins, apply a functor on each of the symbols (as creates the plugin for example).

### Diagram

![](https://2476737171-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFnbb28gNFkF1LVN4Y3%2F-LGOq0sF8ukNlZ_a3pTb%2F-LGOt2054IVfVF9V7Q4G%2Fplugins_registry.png?alt=media\&token=9a5551ac-4fcb-4374-85cb-2d87e3fe6ef4)

### plugins\_registry API

{% tabs %}
{% tab title="Class Signature" %}

```cpp
template <typename CreatorSignature>
class plugins_registry;
```

{% endtab %}

{% tab title="Constructor" %}

```cpp
explicit plugins_registry(shiva::fs::path &&plugins_directory, const std::string library_pattern_matching) noexcept;
```

**plugins\_registry**

**Parameters**

* **plugins\_directory** represents the path to the directory containing the plugins
* **library\_pattern\_matching** represents a basic pattern to respect for libraries to load

{% hint style="warning" %}
For example if the pattern is \[**shiva-system**] then only libraries that have \[**shiva-system**] in their names will be loaded. \[**libshiva-system-plugin.so**] will be loaded \[**libother-things.so**] will not be loaded.
{% endhint %}
{% endtab %}

{% tab title="Functions" %}

| Functions Name                                     | Description                                                       |
| -------------------------------------------------- | ----------------------------------------------------------------- |
| [load\_all\_symbols](#load_all_symbols)            | load all symbols for plugins creation from the plugins\_directory |
| [nb\_plugins](#nb_plugins)                         | return the number of plugins                                      |
| [apply\_on\_each\_symbols](#apply_on_each_symbols) | apply a functor on each symbols                                   |
| {% endtab %}                                       |                                                                   |
| {% endtabs %}                                      |                                                                   |

#### load\_all\_symbols

```cpp
bool load_all_symbols() noexcept;
```

**Return value**

| Possible name | Description                                                                                                                                                                                                                                                                                       |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ***success*** | <p><code>boolean</code> </p><ul><li><strong>true</strong> if all the symbols of all the libraries have been correctly loaded</li><li><strong>false</strong> if a symbol from one of the libraries was not loaded</li><li><strong>false</strong> if the plugins\_directory doesn't exist</li></ul> |

**Example**

```cpp
bool success = plugins_registry.load_all_symbols();
if (!success) {
   /* The loading of symbols failed */
}
```

**Notes**

This function allows you to load symbols from the template to create the object.\
The symbols are **loaded recursively** from the folder you specified in the object's constructor.

{% hint style="warning" %}
If one of the symbols could not be correctly loaded the **function does not stop** and proceeds to load the next plugin.\
If when browsing folders a corrupted file is spotted the function switch to **loading the next plugin**.
{% endhint %}

#### **nb\_plugins**

```cpp
size_t nb_plugins() const noexcept;
```

**Return value**

| Possible name     | Description                                                      |
| ----------------- | ---------------------------------------------------------------- |
| ***nb\_plugins*** | <p><code>size\_t</code> </p><ul><li>numbers of plugins</li></ul> |

**Example**

```cpp
size_t nb_plugins = plugins_registry.nb_plugins();
if (nb_plugins < 4) {
    //! Oh no, i was exepcting at least 4 plugins
}
```

#### **apply\_on\_each\_symbols**

```cpp
template <typename TFunctor>
void apply_on_each_symbols(TFunctor &&functor);
```

**Template parameters**

| Name          | Description                                                                                                   |
| ------------- | ------------------------------------------------------------------------------------------------------------- |
| ***functor*** | <p><code>TFunctor</code> </p><ul><li>represents the functor to apply on each of the loaded symbols.</li></ul> |

**Example**

```cpp
auto functor = [this](auto &&symbol) {
                auto res = symbol(/* parmeters for the creation of the object */);
                // res -> object created by the symbol creator
                // manipulate res or put it in a container for example.
                this->container_object.push_back(std::move(res));
            };

plugins_registry_.apply_on_each_symbols(functor);
```

**Notes**

* This function applies the **functor** as a parameter to each of the symbols that were previously loaded by the [load\_all\_symbols ](https://shiva.gitbook.io/project/shiva/modules/shiva-dll#load_all_symbols)function.

## helpers

| Functions Name                            | Description                                  |
| ----------------------------------------- | -------------------------------------------- |
| [is\_shared\_library](#is_shared_library) | check if a specific path is a shared library |

#### &#x20;is\_shared\_library

```cpp
static inline bool is_shared_library(const fs::path &path) noexcept;
```

#### Parameters

| Name       | Description                                                          |
| ---------- | -------------------------------------------------------------------- |
| ***path*** | <p><code>fs::path</code> </p><ul><li>the path to be tested</li></ul> |

#### Return value

| Possible name | Description                                                                                                                                   |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| ***result***  | <p><code>boolean</code> </p><ul><li><strong>true</strong> if the path is a shared\_library</li><li><strong>false</strong> otherwise</li></ul> |

**Example**

```cpp
// result is true
bool result = shiva::helpers::is_shared_library(shiva::fs::path("path/libfoo.so"));

// result is false
result = shiva::helpers::is_shared_library(shiva::fs::path("path/libbar"));

if (!result) {
   /* The path given as parameter doesn't seem's to be a shared_library */
}
```

#### Notes

* check if the **path** passed in parameter is a shared library
