# SharedLibrary

<a id="sharedlibrary"></a>

# SharedLibrary

```cpp
#include <icy/sharedlibrary.h>

struct SharedLibrary
```

[Defined in src/base/include/icy/sharedlibrary.h:24](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L24)

Loads a shared library at runtime and resolves exported symbols.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
| `bool` | [`open`](#open-1) `inline` | Opens a shared library. The filename is in utf-8. Returns true on success and false on error. Call [`SharedLibrary::error()`](#error-7) to get the error message. |
| `void` | [`close`](#close-4) `inline` | Closes the shared library. |
| `bool` | [`sym`](#sym) `inline` | Retrieves a data pointer from a dynamic library. It is legal for a symbol to map to nullptr. Returns 0 on success and -1 if the symbol was not found. |
| `void` | [`setError`](#seterror) `inline` | Reads the last libuv dynamic-linker error, stores it in _error, and throws a std::runtime_error with the combined prefix and error message. |
| `std::string` | [`error`](#error-7) `const` `inline` | Returns the last error message recorded by [setError()](#seterror). Empty if no error has occurred. |

---

<a id="open-1"></a>

### open

`inline`

```cpp
inline bool open(const std::string & path)
```

[Defined in src/base/include/icy/sharedlibrary.h:29](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L29)

Opens a shared library. The filename is in utf-8. Returns true on success and false on error. Call [`SharedLibrary::error()`](#error-7) to get the error message.

---

<a id="close-4"></a>

### close

`inline`

```cpp
inline void close()
```

[Defined in src/base/include/icy/sharedlibrary.h:39](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L39)

Closes the shared library.

---

<a id="sym"></a>

### sym

`inline`

```cpp
inline bool sym(const char * name, void ** ptr)
```

[Defined in src/base/include/icy/sharedlibrary.h:47](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L47)

Retrieves a data pointer from a dynamic library. It is legal for a symbol to map to nullptr. Returns 0 on success and -1 if the symbol was not found.

---

<a id="seterror"></a>

### setError

`inline`

```cpp
inline void setError(const std::string & prefix)
```

[Defined in src/base/include/icy/sharedlibrary.h:60](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L60)

Reads the last libuv dynamic-linker error, stores it in _error, and throws a std::runtime_error with the combined prefix and error message.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `prefix` | `const std::string &` | Human-readable context string prepended to the error detail. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::runtime_error` | always. |

---

<a id="error-7"></a>

### error

`const` `inline`

```cpp
inline std::string error() const
```

[Defined in src/base/include/icy/sharedlibrary.h:72](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L72)

Returns the last error message recorded by [setError()](#seterror). Empty if no error has occurred. 
#### Returns
Last error string.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| `uv_lib_t` | [`_lib`](#_lib)  |  |
| `std::string` | [`_error`](#_error-1)  |  |

---

<a id="_lib"></a>

### _lib

```cpp
uv_lib_t _lib
```

[Defined in src/base/include/icy/sharedlibrary.h:75](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L75)

---

<a id="_error-1"></a>

### _error

```cpp
std::string _error
```

[Defined in src/base/include/icy/sharedlibrary.h:76](https://github.com/auscaster/icey/blob/main/src/base/include/icy/sharedlibrary.h#L76)
