| Thread overview | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 31, 2009 C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Is there a way to run a class's c'tor on a block of memory from a template function? For example:
C newClass(C, CtorArgs...)(CtorArgs args) {
// Allocate, initialize.
// Want to call the c'tor that takes type CtorArgs.
}
| ||||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote: > Is there a way to run a class's c'tor on a block of memory from a template > function? For example: > > C newClass(C, CtorArgs...)(CtorArgs args) { > // Allocate, initialize. > // Want to call the c'tor that takes type CtorArgs. > } ---- C newClass(C, CtorArgs...)(CtorArgs args) { return new C(args); } class Foo{ this(int a, string b) { /* do something */ } } auto foo = newClass!(Foo)(1, "bar"); ---- That seems to work here... or are you meaning you already have a block of memory allocated and want to turn it into an instance of the class? In which case something like: ---- C newClass(C, CtorArgs...)(CtorArgs args) { void* mem; // Allocate/initialize etc, store in inst of type C // Stolen/adapted from Object.d: if( inst.classinfo.flags & 8 && inst.classinfo.defaultConstructor ) { C delegate(CtorArgs) ctor; ctor.ptr = cast(void*)inst; ctor.funcptr = cast(C function(CtorArgs)) inst.classinfo.defaultConstructor; return ctor(args); } return null; } ---- Should work (untested). | |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | == Quote from Robert Clipsham (robert@octarineparrot.com)'s article
> dsimcha wrote:
> > Is there a way to run a class's c'tor on a block of memory from a template function? For example:
> >
> > C newClass(C, CtorArgs...)(CtorArgs args) {
> > // Allocate, initialize.
> > // Want to call the c'tor that takes type CtorArgs.
> > }
> ----
> C newClass(C, CtorArgs...)(CtorArgs args) {
> return new C(args);
> }
> class Foo{ this(int a, string b) { /* do something */ } }
> auto foo = newClass!(Foo)(1, "bar");
> ----
> That seems to work here... or are you meaning you already have a block of memory allocated and want to turn it into an instance of the class? In which case something like:
> ----
> C newClass(C, CtorArgs...)(CtorArgs args) {
> void* mem;
> // Allocate/initialize etc, store in inst of type C
> // Stolen/adapted from Object.d:
> if( inst.classinfo.flags & 8 && inst.classinfo.defaultConstructor )
> {
> C delegate(CtorArgs) ctor;
> ctor.ptr = cast(void*)inst;
> ctor.funcptr = cast(C function(CtorArgs))
> inst.classinfo.defaultConstructor;
> return ctor(args);
> }
> return null;
> }
> ----
> Should work (untested).
I meant the latter, but not just for the default constructor, for any constructor.
| |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote: > Is there a way to run a class's c'tor on a block of memory from a template > function? For example: > > C newClass(C, CtorArgs...)(CtorArgs args) { > // Allocate, initialize. > // Want to call the c'tor that takes type CtorArgs. > } Deeply hidden on the Digitalmars homepage, it hints _ctor() and shows how to correctly construct an object (if you're using dmd): http://www.digitalmars.com/techtips/class_objects.html | |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote:
> == Quote from Robert Clipsham (robert@octarineparrot.com)'s article
>> dsimcha wrote:
>>> Is there a way to run a class's c'tor on a block of memory from a template
>>> function? For example:
>>>
>>> C newClass(C, CtorArgs...)(CtorArgs args) {
>>> // Allocate, initialize.
>>> // Want to call the c'tor that takes type CtorArgs.
>>> }
>> ----
>> C newClass(C, CtorArgs...)(CtorArgs args) {
>> return new C(args);
>> }
>> class Foo{ this(int a, string b) { /* do something */ } }
>> auto foo = newClass!(Foo)(1, "bar");
>> ----
>> That seems to work here... or are you meaning you already have a block
>> of memory allocated and want to turn it into an instance of the class?
>> In which case something like:
>> ----
>> C newClass(C, CtorArgs...)(CtorArgs args) {
>> void* mem;
>> // Allocate/initialize etc, store in inst of type C
>> // Stolen/adapted from Object.d:
>> if( inst.classinfo.flags & 8 && inst.classinfo.defaultConstructor )
>> {
>> C delegate(CtorArgs) ctor;
>> ctor.ptr = cast(void*)inst;
>> ctor.funcptr = cast(C function(CtorArgs))
>> inst.classinfo.defaultConstructor;
>> return ctor(args);
>> }
>> return null;
>> }
>> ----
>> Should work (untested).
>
> I meant the latter, but not just for the default constructor, for any constructor.
I'm not sure that it's possible to access ctors other than the default one, I can't seem to find any mention of them in the abi or object.d, you might have better luck if you search yourself though, I only had a quick skim. If not it might be worth a feature request?
| |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to grauzone | == Quote from grauzone (none@example.net)'s article
> dsimcha wrote:
> > Is there a way to run a class's c'tor on a block of memory from a template function? For example:
> >
> > C newClass(C, CtorArgs...)(CtorArgs args) {
> > // Allocate, initialize.
> > // Want to call the c'tor that takes type CtorArgs.
> > }
> Deeply hidden on the Digitalmars homepage, it hints _ctor() and shows
> how to correctly construct an object (if you're using dmd):
> http://www.digitalmars.com/techtips/class_objects.html
Yeah, I had thought of this, but apparently it doesn't work anymore. This is called excessive encapsulation--making things so opaque that, when someone really knows what they're doing and has a good reason to mess around with the internals, they still can't. I'll file an enhancement requesting that this be put back.
| |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | == Quote from dsimcha (dsimcha@yahoo.com)'s article
> == Quote from grauzone (none@example.net)'s article
> > dsimcha wrote:
> > > Is there a way to run a class's c'tor on a block of memory from a template function? For example:
> > >
> > > C newClass(C, CtorArgs...)(CtorArgs args) {
> > > // Allocate, initialize.
> > > // Want to call the c'tor that takes type CtorArgs.
> > > }
> > Deeply hidden on the Digitalmars homepage, it hints _ctor() and shows
> > how to correctly construct an object (if you're using dmd):
> > http://www.digitalmars.com/techtips/class_objects.html
> Yeah, I had thought of this, but apparently it doesn't work anymore. This is called excessive encapsulation--making things so opaque that, when someone really knows what they're doing and has a good reason to mess around with the internals, they still can't. I'll file an enhancement requesting that this be put back.
Never mind, I realized I was doing two things wrong:
1. It changed from _ctor to __ctor.
2. The class needs to define a c'tor If it doesn't, then there's no default
__ctor() that takes no args. However, this is easy to check for in a static if
statement.
| |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote: > Is there a way to run a class's c'tor on a block of memory from a template > function? For example: > > C newClass(C, CtorArgs...)(CtorArgs args) { > // Allocate, initialize. > // Want to call the c'tor that takes type CtorArgs. > } After reading the ticket you made, I was wondering if what you were looking for was: http://www.digitalmars.com/d/1.0/class.html#allocators http://www.digitalmars.com/d/1.0/class.html#deallocators This allows you to use a custom allocator/deallocator and not worry about calling the correct ctor/dtor. If you want to change it for all classes, have a look at the _d_allocclass (or _d_newclass depending on your compiler/runtime) function in the runtime. If you change this you will need to recompile the runtime and it won't work for anyone else unless they use your modified runtime. | |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | == Quote from Robert Clipsham (robert@octarineparrot.com)'s article
> dsimcha wrote:
> > Is there a way to run a class's c'tor on a block of memory from a template function? For example:
> >
> > C newClass(C, CtorArgs...)(CtorArgs args) {
> > // Allocate, initialize.
> > // Want to call the c'tor that takes type CtorArgs.
> > }
> After reading the ticket you made, I was wondering if what you were
> looking for was:
> http://www.digitalmars.com/d/1.0/class.html#allocators
> http://www.digitalmars.com/d/1.0/class.html#deallocators
> This allows you to use a custom allocator/deallocator and not worry
> about calling the correct ctor/dtor. If you want to change it for all
> classes, have a look at the _d_allocclass (or _d_newclass depending on
> your compiler/runtime) function in the runtime. If you change this you
> will need to recompile the runtime and it won't work for anyone else
> unless they use your modified runtime.
Yes, but...
Unless I hack the compiler, only *runtime* type info gets passed to this function.
I need *compile time* type info so it can be passed to templates to generate/get
the bit mask for the type for my precise heap scanning scheme.
| |||
October 31, 2009 Re: C'tors from templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | dsimcha wrote:
> == Quote from dsimcha (dsimcha@yahoo.com)'s article
>> == Quote from grauzone (none@example.net)'s article
>>> dsimcha wrote:
>>>> Is there a way to run a class's c'tor on a block of memory from a template
>>>> function? For example:
>>>>
>>>> C newClass(C, CtorArgs...)(CtorArgs args) {
>>>> // Allocate, initialize.
>>>> // Want to call the c'tor that takes type CtorArgs.
>>>> }
>>> Deeply hidden on the Digitalmars homepage, it hints _ctor() and shows
>>> how to correctly construct an object (if you're using dmd):
>>> http://www.digitalmars.com/techtips/class_objects.html
>> Yeah, I had thought of this, but apparently it doesn't work anymore. This is
>> called excessive encapsulation--making things so opaque that, when someone really
>> knows what they're doing and has a good reason to mess around with the internals,
>> they still can't. I'll file an enhancement requesting that this be put back.
>
> Never mind, I realized I was doing two things wrong:
>
> 1. It changed from _ctor to __ctor.
> 2. The class needs to define a c'tor If it doesn't, then there's no default
> __ctor() that takes no args. However, this is easy to check for in a static if
> statement.
Walter and I talked a few times about the necessity of providing useful functions a la C++'s placement new in Phobos. std.algorithm offers swap and move, but we need some more. Please advise.
Andrei
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply