you could conditionally construct it like:

class ConditionalUnlocker {
    typedef v8::Unlocker RealUnlocker;

    union {
        char unlocker[sizeof(RealUnlocker)];
        double align;
    };
    bool istrue;

  public:
     ConditionalUnlocker(bool condition)
     : istrue(condition)
     {
         if (istrue) {
             new (unlocker) RealUnlocker();  // construct it within the
stack-allocated 'unlocker'
         }
     }

     ~ConditonalUnlocker()
     {
         if (istrue) {
              RealUnlocker& real = *(RealUnlocker*)unlocker;
              real.~RealUnlocker();   // destruct it
         }
     }
};


(no guarantees being made about the portability of this class etc)



2012/3/12 avasilev <[email protected]>

> Hi Stephan,
> Thanks
> Unfortunately this will do the job only if the flag is known at compile
> time. What I need is to be able to switch this at runtime.
> Thanks for the response.
> Best regards
> Alex
>
> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа:
>
>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]> wrote:
>>
>>> Unlocker classes? My use case is a conditional auto-unlocker object,
>>> that may or may not unlock v8, depending on a global setting, so I
>>>
>>
>> Here's a template which does that:
>>
>>     /**
>>         A sentry class which instantiates a v8::Unlocker
>>         if the boolean value is true or is a no-op if it is false.
>>     */
>>     template <bool> struct V8Unlocker {};
>>
>>     /**
>>         Equivalent to v8::Unlocker.
>>     */
>>     template <>
>>     struct V8Unlocker<true> : v8::Unlocker
>>     {
>>     };
>>
>> --
>> ----- stephan beal
>> http://wanderinghorse.net/**home/stephan/<http://wanderinghorse.net/home/stephan/>
>> http://gplus.to/sgbeal
>>
>>
> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа:
>
>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]> wrote:
>>
>>> Unlocker classes? My use case is a conditional auto-unlocker object,
>>> that may or may not unlock v8, depending on a global setting, so I
>>>
>>
>> Here's a template which does that:
>>
>>     /**
>>         A sentry class which instantiates a v8::Unlocker
>>         if the boolean value is true or is a no-op if it is false.
>>     */
>>     template <bool> struct V8Unlocker {};
>>
>>     /**
>>         Equivalent to v8::Unlocker.
>>     */
>>     template <>
>>     struct V8Unlocker<true> : v8::Unlocker
>>     {
>>     };
>>
>> --
>> ----- stephan beal
>> http://wanderinghorse.net/**home/stephan/<http://wanderinghorse.net/home/stephan/>
>> http://gplus.to/sgbeal
>>
>>
> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа:
>
>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]> wrote:
>>
>>> Unlocker classes? My use case is a conditional auto-unlocker object,
>>> that may or may not unlock v8, depending on a global setting, so I
>>>
>>
>> Here's a template which does that:
>>
>>     /**
>>         A sentry class which instantiates a v8::Unlocker
>>         if the boolean value is true or is a no-op if it is false.
>>     */
>>     template <bool> struct V8Unlocker {};
>>
>>     /**
>>         Equivalent to v8::Unlocker.
>>     */
>>     template <>
>>     struct V8Unlocker<true> : v8::Unlocker
>>     {
>>     };
>>
>> --
>> ----- stephan beal
>> http://wanderinghorse.net/**home/stephan/<http://wanderinghorse.net/home/stephan/>
>> http://gplus.to/sgbeal
>>
>>
> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа:
>
>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]> wrote:
>>
>>> Unlocker classes? My use case is a conditional auto-unlocker object,
>>> that may or may not unlock v8, depending on a global setting, so I
>>>
>>
>> Here's a template which does that:
>>
>>     /**
>>         A sentry class which instantiates a v8::Unlocker
>>         if the boolean value is true or is a no-op if it is false.
>>     */
>>     template <bool> struct V8Unlocker {};
>>
>>     /**
>>         Equivalent to v8::Unlocker.
>>     */
>>     template <>
>>     struct V8Unlocker<true> : v8::Unlocker
>>     {
>>     };
>>
>> --
>> ----- stephan beal
>> http://wanderinghorse.net/**home/stephan/<http://wanderinghorse.net/home/stephan/>
>> http://gplus.to/sgbeal
>>
>>
> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа:
>
>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]> wrote:
>>
>>> Unlocker classes? My use case is a conditional auto-unlocker object,
>>> that may or may not unlock v8, depending on a global setting, so I
>>>
>>
>> Here's a template which does that:
>>
>>     /**
>>         A sentry class which instantiates a v8::Unlocker
>>         if the boolean value is true or is a no-op if it is false.
>>     */
>>     template <bool> struct V8Unlocker {};
>>
>>     /**
>>         Equivalent to v8::Unlocker.
>>     */
>>     template <>
>>     struct V8Unlocker<true> : v8::Unlocker
>>     {
>>     };
>>
>> --
>> ----- stephan beal
>> http://wanderinghorse.net/**home/stephan/<http://wanderinghorse.net/home/stephan/>
>> http://gplus.to/sgbeal
>>
>>  --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to