SimpleRAII

SimpleRAII is a tiny garbage collection helper class that eases the use of the RAII idiom (Resource Acquisition Is Initialization).

Usage

When creating a new class, simply subclass the "SimpleRAII " class and encapsulate all "new ..." statements with "manage(new ...)". Do not use any delete statements!

Background & Example

In short the RAII idiom states that it's a good thing when dealing with resource allocation and deallocation in constructors and destructors. In the latter case, code that frees memory is always guaranteed to be executed, when the object is deleted from the stack. So, suppose you write a class MyClass that looks like this:

class MyClass {
public:
MyClass(void) {
  s = new std::string();
}

~MyClass(void) {
  delete s;
}

private:
  std::string * s;
}
 

The pointer variable s is initialized as a string objected on the heap which will deleted when the instance of MyClass runs out of scope. To safe yourself some writing whenever you want to do just that, you can use the SimpleRAII helper class to achieve that same thing. MyClass would then look like this:

class MyClass : public SimpleRAII {
public:
MyClass(void){
  s = manage(new std::string());
}

private:
  std::string * s;
}
 

Here, no destructor is needed anymore.

More information can be obtained from the Readme.txt in the download package at sourceforge.

Blog

NanoNet - simple Java neural network library
Using backpropagation and sigmoid activation function. | 4/14/2019

NanoNet is a very simple Java neural network library using backpropagation and sigmoid ... Read More

Java async/await nonblocking code library
Write sequential style nonblocking code using a fixed threadpool with Async-Complete for Java | 4/1/2019

Async-Complete (aka Async-Await) is a library for writing asynchronous code in a more ... Read More

SDCLib/J contribution fork
Contributing to IEEE 11073 SDC Family java webservice stack | 2/2/2019

As former main author of the official project in my last job at SurgiTAIX AG, I will maintain ... Read More

More Blog Entries