From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Prohibiting dynamic allocation for the given type
Date: Wed, 19 Mar 2008 20:48:18 -0400
Date: 2008-03-19T20:48:18-04:00 [thread overview]
Message-ID: <wccprtqmd9p.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: f126d5ac-bc9d-4ebb-a449-3da5df84adbc@c19g2000prf.googlegroups.com
Eric Hughes <eric.eh9@gmail.com> writes:
> If you want to control lifetime, you can control either the allocation
> lifetime or the functional lifetime; the second is a subset of the
> first. As you've pointed out, you don't get a lot of control over
> allocation lifetime. But functional lifetime you can control. Here's
> how I would approach it.
>
> First, make the utility of your type depend for its function on the
> existence of some other object. This likely goes into a constructor
> function, but need not. Your type would contain some kind of a weak
> accessor, one that doesn't enforce the existence of its referent.
> Gate all utility upon the existence of this referent. If the referent
> is absent, do nothing or throw an exception, whatever. This check is
> cheap; it's that some access value is not null. Use finalization in
> the referent to update the accessor value in your type. This gives
> you a dependency between objects, regardless of how the referent
> object is allocated.
>
> Second, when instantiating the object, make it refer to a sentry
> object with block scope. When the sentry object goes out of scope,
> your original object will stop working. I frequently use nested
> declare/begin/end blocks within subprogram definitions to trigger
> sentry finalization.
>
> You'll still have to document that if you want scope lifetime for your
> object's utility, you'll have to make it dependent upon a scoped
> variable. But at that point the code explicitly captures an intention
> for scope lifetime.
I don't understand this method. Maybe you could give an outline of the
code. In particular, how do you ensure that the "sentry" is not
heap-allocated? If you trust the client on that, then why not trust
the client to avoid heap allocation for the "real" object in the
first place? I must be missing something...
- Bob
next prev parent reply other threads:[~2008-03-20 0:48 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-18 21:30 Prohibiting dynamic allocation for the given type Maciej Sobczak
2008-03-19 3:06 ` gpriv
2008-03-19 8:24 ` Maciej Sobczak
2008-03-19 11:31 ` Georg Bauhaus
2008-03-19 13:13 ` gpriv
2008-03-19 13:54 ` Maciej Sobczak
2008-03-19 16:37 ` Eric Hughes
2008-03-20 0:48 ` Robert A Duff [this message]
2008-03-20 21:35 ` Eric Hughes
2008-03-19 22:17 ` Georg Bauhaus
2008-03-19 23:40 ` gpriv
2008-03-20 21:11 ` Maciej Sobczak
2008-03-19 14:38 ` Adam Beneschan
2008-03-19 15:43 ` gpriv
2008-03-19 14:12 ` Pascal Obry
2008-03-19 23:08 ` Randy Brukardt
2008-03-20 20:26 ` Simon Wright
2008-03-20 22:03 ` Eric Hughes
2008-03-21 1:04 ` Randy Brukardt
2008-03-20 0:43 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox