Jump to page: 1 2
Thread overview
RAII
Feb 23, 2017
ketmar
Feb 23, 2017
kinke
Feb 23, 2017
Adam D. Ruppe
Feb 23, 2017
kinke
Feb 23, 2017
kinke
Feb 24, 2017
ketmar
Feb 23, 2017
Adrian Matoga
Feb 23, 2017
cym13
February 23, 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;
}
```
February 23, 2017
Arun Chandrasekaran wrote:

> 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?
why not static method or free function that returns struct? due to NRVO[0] it won't even be copied.

 auto lock = MyWrapper();

`MyWrapper()` may return voldemort type, so user won't create your struct accidentally.


[0] https://dlang.org/glossary.html#nrvo
February 23, 2017
On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:
> Arun Chandrasekaran wrote:
>> Is there an elegant way to achieve this in D?
> why not static method or free function that returns struct? due to NRVO[0] it won't even be copied.

That's not elegant. You need a factory function for each type containing one of these structs then.
February 23, 2017
On Thursday, 23 February 2017 at 09:52:26 UTC, Arun Chandrasekaran wrote:
> 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?
>

static opCall() is the way to emulate an argumentless struct constructor. You still need to call it explicitly, though:

```
import std.stdio : writefln;
struct Foo {
	int a;
	static Foo opCall() {
		Foo foo;
		foo.a = 42;
		return foo;
	}
	~this() {
		writefln("destroyed with a=%d", a);
	}
	@disable this(this);
	@disable void opAssign(Foo);
}

void main()
{
	auto foo1 = Foo(); // ok
	Foo foo2;          // == Foo.init
}
```


February 23, 2017
On Thursday, 23 February 2017 at 10:48:38 UTC, kinke wrote:
> That's not elegant. You need a factory function for each type containing one of these structs then.

A constructor is just a factory function with a special name... it is almost equal amount of work.
February 23, 2017
On Thursday, 23 February 2017 at 14:24:14 UTC, Adam D. Ruppe wrote:
> On Thursday, 23 February 2017 at 10:48:38 UTC, kinke wrote:
>> That's not elegant. You need a factory function for each type containing one of these structs then.
>
> A constructor is just a factory function with a special name... it is almost equal amount of work.

Sorry but this is just completely wrong. RAII is all about me NOT having to call special functions all over the place.

struct S
{
    this() { /* initialize */ }
}

S[123] myArray; // hooray, no need to invoke a factory in a loop

struct Container
{
    S s;
    // hooray, implicit this() automatically calls s.this()
}
February 23, 2017
On Thursday, 23 February 2017 at 18:46:58 UTC, kinke wrote:
>> A constructor is just a factory function with a special name...

Wrt. the special name, that's obviously extremely useful for generic templates (containers etc.).
February 23, 2017
On Thursday, 23 February 2017 at 09:52:26 UTC, Arun Chandrasekaran wrote:
> 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;
> }
> ```

It reminds me of https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/ which isn't what you want but may be interesting anyway.
February 24, 2017
On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:
> Arun Chandrasekaran wrote:
>
>> 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?
> why not static method or free function that returns struct? due to NRVO[0] it won't even be copied.
>
>  auto lock = MyWrapper();
>
> `MyWrapper()` may return voldemort type, so user won't create your struct accidentally.
>
>
> [0] https://dlang.org/glossary.html#nrvo

Thanks for your help. NRVO looks interesting. However this may not be RAII after all. Or may be too much of C++ has spoiled me. I am not familiar enough with D to appreciate/question the language design choice. I'll accept this and move forward and see how my code evolves. Nevertheless, I'm still learning something new. :)
February 24, 2017
On Thursday, 23 February 2017 at 21:05:48 UTC, cym13 wrote:
> It reminds me of https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/ which isn't what you want but may be interesting anyway.

It is interesting, indeed, thanks.
« First   ‹ Prev
1 2