RAII
Arun Chandrasekaran via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 23 01:52:26 PST 2017
I'm trying to write an RAII wrapper on Linux.
I understand struct in D doesn't have default constructor (for
.init reasons).
I don't want to use `scope`.
Is there an elegant way to achieve this in D?
```
import core.sys.posix.pthread;
import core.sys.posix.sys.types;
/// Makes pthread_mutexattr_t cleanup easy when using exceptions
struct mutexattr_wrapper
{
/// Constructor
this(bool _)
{
if (pthread_mutexattr_init(&m_attr) != 0 ||
pthread_mutexattr_setpshared(&m_attr,
PTHREAD_PROCESS_SHARED)!= 0)
// may be I will add few more methods here
throw custom_exception("pthread_mutexattr_xxxx failed");
}
/// Destructor
~this() { pthread_mutexattr_destroy(&m_attr); }
/// This allows using mutexattr_wrapper as pthread_mutexattr_t
alias m_attr this;
pthread_mutexattr_t m_attr;
}
```
More information about the Digitalmars-d-learn
mailing list