Skip to main content

Questions tagged [raii]

Resource Acquisition Is Initialization (RAII) is a common idiom used in C++ to manage the lifetime of resources, including memory allocations, file handles or database connections. In brief, every resource should be wrapped in an owning class, whose lifetime controls the lifetime of the resource.

2 votes
2 answers
159 views

Is this a good pattern for handling late-initialized of class type?

Are there any issues with this class template? It is supposed to provide an easy-to-use object solution to initialize an object of class type after the declaration of the variable. One nuisance is the ...
Bolpat's user avatar
  • 233
4 votes
2 answers
321 views

A memory leak-free RAII wrapper around two raw pointers

As a practice of implementing RAII-enabled class, I prepared a class called DynamicWallet that wraps around two raw pointers. I am well aware that using smart ...
D.J. Elkind's user avatar
5 votes
1 answer
847 views

Implement scope_exit

I want to use the scope_exit class but my compiler/standard library (clang++-16 with libc++) don't support it. Until they do I wanted an implementation. I found a ...
nebuch's user avatar
  • 155
0 votes
1 answer
117 views

Data Wrapper Class with Automatic Saving and Locked Read/Write Accessors

This is a wrapper for a synchronized data structure that: Saves periodically Keeps track of dirty flag automatically (set when a write access is requested) Maintains a lock on data Only allows access ...
CaptainCodeman's user avatar
2 votes
1 answer
152 views

std::unique_ptr adapter class template for libgit2 objects

I'm working on a personal project to build an open-source gui for git. I'm hoping to learn a lot from the project, and maybe produce something useful for folks as I do. I'm making use of libgit2 to ...
Stephen Hern's user avatar
1 vote
1 answer
359 views

C++ RAII helper classes for malloc arrays and files

I'm going to use these classes in a program I'm working on, so I want to see if they're correct or could be improved. https://pastebin.com/qx7ccteT ...
my_stack_exchange_account's user avatar
0 votes
1 answer
554 views

Vector Implementation C++ using RAII

I have attempted to implement a similar version of the STL Vector; several functions are missing but I'd like a few words of advice on whether I am indeed on the right track or whether I should change ...
PeePeePooPoo's user avatar
4 votes
1 answer
762 views

Boost.Asio Server and RAII

I am trying to implement a network server application in C++ using Boost.Asio. Here are the requirements I am trying to meet: The Application creates only one instance of ...
MarekR's user avatar
  • 141
5 votes
2 answers
84 views

Concisely, robustly, temporarily stashing c++ iostream formats

I got tired of the boilerplate and tedium restoring formatting context, so I made an RAII stasher that relies on the destroy-at-end-of-full-statement temporary semantics. With C++17 I can get it down ...
jthill's user avatar
  • 151
4 votes
1 answer
133 views

Unique and shared resource owner

I need a unique_ptr and shared_ptr like structure, but instead of pointers, I would like to store some kind of reference to a ...
Adam's user avatar
  • 63
13 votes
3 answers
3k views

Correctly applying the "rule of five" to a RAII socket wrapper

I was trying to create a simple RAII wrapper with rule of 5 for a TCP POSIX socket. My aim was to try learn how to apply rule of five in different situations, but this one was somehow tricky. ...
Afshin's user avatar
  • 295
3 votes
3 answers
803 views

RAII wrapper for SQLite transactions

I'm using this nice C++ wrapper to work with SQLite3 in my project. I wanted to handle errors gracefully and automatically rollback if piece of code fails, so wrote this simple helper class: ...
val - disappointed in SE's user avatar
1 vote
1 answer
364 views

SDL Initialization and Management wrappers

I am using SDL2 for window management and rendering, but it can be a little verbose. And because of how SDL2 works under the hood, the SDL_Renderer is tied to image resource loading. So, I am ...
Acorn's user avatar
  • 283
4 votes
0 answers
173 views

Generalizing std::shared_ptr for all handle-types, even non-pointers

I created this shared pointer mainly to use it as RAII for handles that have to be shared between classes or threads. For pointers: It supports raw pointers and pointer to classes and arrays and also ...
dev65's user avatar
  • 313
5 votes
1 answer
1k views

My unique_ptr implementation

This is my simple unique_ptr implementation. Anything that could be improved upon or should be added? ...
devptr's user avatar
  • 51

15 30 50 per page