---
title: llarp::util

---

# llarp::util



## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::util::DecayingHashSet](Classes/structllarp_1_1util_1_1DecayingHashSet.md)**  |
| struct | **[llarp::util::DecayingHashTable](Classes/structllarp_1_1util_1_1DecayingHashTable.md)**  |
| class | **[llarp::util::bind_socket_error](Classes/classllarp_1_1util_1_1bind__socket__error.md)**  |
| struct | **[llarp::util::FileHash](Classes/structllarp_1_1util_1_1FileHash.md)**  |
| class | **[llarp::util::Barrier](Classes/classllarp_1_1util_1_1Barrier.md)** <br>[Barrier]() class that blocks all threads until the high water mark of threads (set during construction) is reached, then releases them all.  |
| struct | **[llarp::util::NullMutex](Classes/structllarp_1_1util_1_1NullMutex.md)** <br>a mutex that does nothing  |
| struct | **[llarp::util::NullLock](Classes/structllarp_1_1util_1_1NullLock.md)** <br>a lock that does nothing  |
| class | **[llarp::util::Semaphore](Classes/classllarp_1_1util_1_1Semaphore.md)**  |
| struct | **[llarp::util::ContentionKiller](Classes/structllarp_1_1util_1_1ContentionKiller.md)**  |

## Types

|                | Name           |
| -------------- | -------------- |
| using std::error_code | **[error_code_t](Namespaces/namespacellarp_1_1util.md#using-error-code-t)**  |
| template <typename T ,typename Container  =std::vector<T>\> <br>using std::priority_queue< T, Container, std::greater< typename Container::value_type > > | **[ascending_priority_queue](Namespaces/namespacellarp_1_1util.md#using-ascending-priority-queue)** <br>priority queue that uses operator > instead of operator <  |
| using nlohmann::json | **[StatusObject](Namespaces/namespacellarp_1_1util.md#using-statusobject)**  |
| using std::shared_timed_mutex | **[Mutex](Namespaces/namespacellarp_1_1util.md#using-mutex)** <br>Default mutex type, supporting shared and exclusive locks.  |
| using std::lock_guard< Mutex > | **[Lock](Namespaces/namespacellarp_1_1util.md#using-lock)** <br>Basic RAII lock type for the default mutex type.  |

## Functions

|                | Name           |
| -------------- | -------------- |
| std::string | **[slurp_file](Namespaces/namespacellarp_1_1util.md#function-slurp-file)**(const fs::path & filename)<br>Reads a binary file from disk into a string. Throws on error.  |
| size_t | **[slurp_file](Namespaces/namespacellarp_1_1util.md#function-slurp-file)**(const fs::path & filename, char * buffer, size_t buffer_size)<br>Reads a binary file from disk directly into a buffer.  |
| template <typename Char ,std::enable_if_t< sizeof(Char)==1 and not std::is_same_v< Char, char >, int >  =1\> <br>size_t | **[slurp_file](Namespaces/namespacellarp_1_1util.md#function-slurp-file)**(const fs::path & filename, Char * buffer, size_t buffer_size)<br>Same, but for some non-char but single-byte char type (e.g. byte_t, std::byte, unsigned char).  |
| void | **[dump_file](Namespaces/namespacellarp_1_1util.md#function-dump-file)**(const fs::path & filename, std::string_view contents)<br>Dumps binary string contents to disk.  |
| template <typename Char ,std::enable_if_t< sizeof(Char)==1, int >  =0\> <br>void | **[dump_file](Namespaces/namespacellarp_1_1util.md#function-dump-file)**(const fs::path & filename, const Char * buffer, size_t buffer_size)<br>Same as above, but works via char-like buffer.  |
| error_code_t | **[EnsurePrivateFile](Namespaces/namespacellarp_1_1util.md#function-ensureprivatefile)**(fs::path pathname)<br>Ensure that a file exists and has correct permissions return any error code or success.  |
| template <typename T \> <br>std::optional< T > | **[OpenFileStream](Namespaces/namespacellarp_1_1util.md#function-openfilestream)**(fs::path pathname, std::ios::openmode mode)<br>open a stream to a file and ensure it exists before open sets any permissions on creation  |
| template <typename PathVisitor \> <br>void | **[IterDir](Namespaces/namespacellarp_1_1util.md#function-iterdir)**(const fs::path & path, PathVisitor visit) |
| template <typename Return ,typename Class ,typename Derived ,typename... Arg,typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>\> <br>auto | **[memFn](Namespaces/namespacellarp_1_1util.md#function-memfn)**(Return(Class::*)(Arg...) f, Derived * self) |
| template <typename Return ,typename Class ,typename Derived ,typename... Arg,typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>\> <br>auto | **[memFn](Namespaces/namespacellarp_1_1util.md#function-memfn)**(Return(Class::*)(Arg...) const f, const Derived * self) |
| template <typename Return ,typename Class ,typename Derived ,typename... Arg,typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>\> <br>auto | **[memFn](Namespaces/namespacellarp_1_1util.md#function-memfn)**(Return(Class::*)(Arg...) f, std::shared_ptr< Derived > self) |
| template <typename... Mutex\> <br>auto | **[unique_locks](Namespaces/namespacellarp_1_1util.md#function-unique-locks)**(Mutex &... lockables)<br>Obtains multiple unique locks simultaneously and atomically.  |
| void | **[SetThreadName](Namespaces/namespacellarp_1_1util.md#function-setthreadname)**(const std::string & name) |
| pid_t | **[GetPid](Namespaces/namespacellarp_1_1util.md#function-getpid)**() |

## Attributes

|                | Name           |
| -------------- | -------------- |
| constexpr auto | **[erase_if](Namespaces/namespacellarp_1_1util.md#variable-erase-if)** <br>remove items from a container if a predicate is true return the number of items removed  |

## Types Documentation

### using error_code_t

```cpp
using llarp::util::error_code_t = typedef std::error_code;
```


### using ascending_priority_queue

```cpp
template <typename T ,
typename Container  =std::vector<T>>
using llarp::util::ascending_priority_queue = typedef std::priority_queue<T, Container, std::greater<typename Container::value_type> >;
```

priority queue that uses operator > instead of operator < 

### using StatusObject

```cpp
using llarp::util::StatusObject = typedef nlohmann::json;
```


### using Mutex

```cpp
using llarp::util::Mutex = typedef std::shared_timed_mutex;
```

Default mutex type, supporting shared and exclusive locks. 

### using Lock

```cpp
using llarp::util::Lock = typedef std::lock_guard<Mutex>;
```

Basic RAII lock type for the default mutex type. 


## Functions Documentation

### function slurp_file

```cpp
std::string slurp_file(
    const fs::path & filename
)
```

Reads a binary file from disk into a string. Throws on error. 

### function slurp_file

```cpp
size_t slurp_file(
    const fs::path & filename,
    char * buffer,
    size_t buffer_size
)
```

Reads a binary file from disk directly into a buffer. 

Throws a std::length_error if the file is bigger than the buffer. Returns the bytes copied on success. 


### function slurp_file

```cpp
template <typename Char ,
std::enable_if_t< sizeof(Char)==1 and not std::is_same_v< Char, char >, int >  =1>
inline size_t slurp_file(
    const fs::path & filename,
    Char * buffer,
    size_t buffer_size
)
```

Same, but for some non-char but single-byte char type (e.g. byte_t, std::byte, unsigned char). 

### function dump_file

```cpp
void dump_file(
    const fs::path & filename,
    std::string_view contents
)
```

Dumps binary string contents to disk. 

The file is overwritten if it already exists. Throws on error. 


### function dump_file

```cpp
template <typename Char ,
std::enable_if_t< sizeof(Char)==1, int >  =0>
inline void dump_file(
    const fs::path & filename,
    const Char * buffer,
    size_t buffer_size
)
```

Same as above, but works via char-like buffer. 

### function EnsurePrivateFile

```cpp
error_code_t EnsurePrivateFile(
    fs::path pathname
)
```

Ensure that a file exists and has correct permissions return any error code or success. 

### function OpenFileStream

```cpp
template <typename T >
std::optional< T > OpenFileStream(
    fs::path pathname,
    std::ios::openmode mode
)
```

open a stream to a file and ensure it exists before open sets any permissions on creation 

### function IterDir

```cpp
template <typename PathVisitor >
static void IterDir(
    const fs::path & path,
    PathVisitor visit
)
```


### function memFn

```cpp
template <typename Return ,
typename Class ,
typename Derived ,
typename... Arg,
typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>>
auto memFn(
    Return(Class::*)(Arg...) f,
    Derived * self
)
```


### function memFn

```cpp
template <typename Return ,
typename Class ,
typename Derived ,
typename... Arg,
typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>>
auto memFn(
    Return(Class::*)(Arg...) const f,
    const Derived * self
)
```


### function memFn

```cpp
template <typename Return ,
typename Class ,
typename Derived ,
typename... Arg,
typename  =std::enable_if_t<std::is_base_of<Class, Derived>::value>>
auto memFn(
    Return(Class::*)(Arg...) f,
    std::shared_ptr< Derived > self
)
```


### function unique_locks

```cpp
template <typename... Mutex>
auto unique_locks(
    Mutex &... lockables
)
```

Obtains multiple unique locks simultaneously and atomically. 

Returns a tuple of all the held locks. 


### function SetThreadName

```cpp
void SetThreadName(
    const std::string & name
)
```


### function GetPid

```cpp
inline pid_t GetPid()
```



## Attributes Documentation

### variable erase_if

```cpp
constexpr auto erase_if = [](auto& container, auto&& pred) -> std::size_t {
    std::size_t removed = 0;
    for (auto itr = container.begin(); itr != container.end();)
    {
      if (pred(*itr))
      {
        itr = container.erase(itr);
        removed++;
      }
      else
        itr++;
    }
    return removed;
  };
```

remove items from a container if a predicate is true return the number of items removed 




-------------------------------

Updated on 2023-01-23 at 22:05:56 +0000