https://github.com/mik90/wrapped_var
C++ variable wrapped by a mutex like Rust's std::sync::Mutex
https://github.com/mik90/wrapped_var
cpp
Last synced: about 1 month ago
JSON representation
C++ variable wrapped by a mutex like Rust's std::sync::Mutex
- Host: GitHub
- URL: https://github.com/mik90/wrapped_var
- Owner: mik90
- License: mit
- Created: 2021-01-31T03:22:27.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-02T21:54:04.000Z (over 5 years ago)
- Last Synced: 2025-02-23T11:13:58.821Z (over 1 year ago)
- Topics: cpp
- Language: CMake
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wrapped_var

Header only mutex variable wrappers like Rust's `std::sync::Mutex` but for C++
### Requirements
- Requires C++11 or newer
- Header-only so conan isn't strictly required to pull this in as a dependency. You could just pull down the header.
I haven't uploaded the recipe to bincrafters or anywhere so that's kind of the only option.
### Usage
Create a `std::string` and protect it underneath a mutex
```cpp
wrapped_var wrapped_string{"hello there"};
```
Create a `std::vector` and protect it underneath a mutex.
```cpp
using vector_type = std::vector;
constexpr vector_type::size_type size = 5;
constexpr vector_type::value_type value = 12;
wrapped_var wrapped_string(size, value);
```
Modify a mutex-protected `std::string`
```cpp
wrapped_var wrapped_string{"modify me"};
{
auto string_accessor = wrapped_string.get();
string_accessor.get_ref() = "you are modified";
}
const auto const_string_accessor = wrapped_string.get();
ASSERT_EQ(const_string_accessor.get_cref(), "you are modified");
```
### Running unit tests
- Requires conan
```sh
git clone git@github.com:mik90/wrapped_var.git
mkdir build && cd build
cmake ..
cmake --build . --target UnitTest
ctest
```
#### Running tests with sanitizers
Configure the cmake build for release and enable one of the sanitizer flags.
Release is more likely to cause problems than debug (or so I read).
```sh
-DCMAKE_BUILD_TYPE=Release -DENABLE_SANITIZER_ADDRESS=ON
```
### Test packaging with conan
```sh
conan test . mik90/testing
```