[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using C++ Member Function with rb_protect

Daniel Harple

8/8/2006 8:33:00 PM

I'm having problems using a C++ member function with rb_protect. Here
is the code:

class Sandbox {
public:
Sandbox() {
sandbox_ = rb_funcall(rb_const_get(rb_cObject, rb_intern
("Sandbox")),
rb_intern("safe"),
0);
}
virtual ~Sandbox() {
}
VALUE eval(char * code) {
int status = 0;
VALUE val = rb_protect(&Sandbox::eval_protect,
rb_str_new2(code),
&status);
if (status) { /* handle error... */ }
return val;
}
private:
VALUE eval_protect(VALUE code) {
return rb_funcall(sandbox_, rb_intern("eval"), 1, code);
}
VALUE sandbox_;
};

The compile fails with:

test.cpp: In member function ‘VALUE Sandbox::eval(char*)’:
test.cpp:18: error: cannot convert ‘VALUE (Sandbox::*)(VALUE)’ to
‘VALUE (*)(VALUE)’ for argument ‘1’ to ‘VALUE rb_protect(VALUE (*)
(VALUE), VALUE, int*)’

$ ruby1.8 -v
ruby 1.8.5 (2006-08-06) [powerpc-darwin8.7.0]
$ g++ --version
powerpc-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc.
build 5341)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

Any ideas?

Thanks,
-- Daniel


10 Answers

Daniel Harple

8/8/2006 8:58:00 PM

0

On Aug 8, 2006, at 4:45 PM, Francis Cianfrocca wrote:

> What happens if you remove the & in this line:
>
> VALUE val = rb_protect(&Sandbox::eval_protect,

I get this compile error:

test.cpp: In member function ‘VALUE Sandbox::eval(char*)’:
test.cpp:15: error: argument of type ‘VALUE (Sandbox::)(VALUE)’ does
not match ‘VALUE (*)(VALUE)’

-- Daniel


Caleb Tennis

8/8/2006 9:28:00 PM

0

>
> Any ideas?
>

I'm not sure if it will help, but for my C++ extensions, I've had to
do this type of trickery:

typedef VALUE (ruby_method)(...);


when doing this:

rb_define_method(some_ruby_object, "initialize", (ruby_method*)
&some_object_initialize, 0);


Caleb Tennis

8/8/2006 9:30:00 PM

0

>
> Any ideas?
>

Also, have you tried casting to a void * ?



Mauricio Fernández

8/8/2006 9:34:00 PM

0

On Wed, Aug 09, 2006 at 05:57:53AM +0900, Daniel Harple wrote:
> On Aug 8, 2006, at 4:45 PM, Francis Cianfrocca wrote:
>
> >What happens if you remove the & in this line:
> >
> >VALUE val = rb_protect(&Sandbox::eval_protect,
>
> I get this compile error:
>
> test.cpp: In member function ‘VALUE Sandbox::eval(char*)’:
> test.cpp:15: error: argument of type ‘VALUE (Sandbox::)(VALUE)’ does
> not match ‘VALUE (*)(VALUE)’

Of course it won't work that way, being a member function (it is given a
reference to this too...).

You'd have to do something like this (it's been a while without writing any
C++, so take this with a grain of salt):

class Sandbox {
public:
Sandbox() {
sandbox_ = rb_funcall(rb_const_get(rb_cObject, rb_intern("Sandbox")),
rb_intern("safe"),
0);
}
virtual ~Sandbox() {
}
VALUE eval(char * code) {
int status = 0;
VALUE args[2];
args[0] = (VALUE)this;
args[1] = rb_str_new2(code);
VALUE val = rb_protect(&call_eval_protect, (VALUE)args, &status);
if (status) { /* handle error... */ }
return val;
}
private:
static VALUE call_eval_protect(VALUE args) {
Sandbox *me = ((Sanbox *)args)[0];
VALUE code = ((VALUE *)args)[1];
return rb_funcall(me->sandbox_, rb_intern("eval"), 1, code);
}
VALUE sandbox_;
};


--
Mauricio Fernandez - http://eige... - singular Ruby

Daniel Harple

8/8/2006 9:58:00 PM

0

On Aug 8, 2006, at 5:34 PM, Mauricio Fernandez wrote:

> Of course it won't work that way, being a member function (it is
> given a
> reference to this too...).
>
> You'd have to do something like this (it's been a while without
> writing any
> C++, so take this with a grain of salt):

Thanks, I ended up with something similar.

class Sandbox
{
public:
Sandbox() {
sandbox_ = rb_funcall(rb_const_get(rb_cObject, rb_intern
("Sandbox")),
rb_intern("safe"),
0);
}
virtual ~Sandbox() { }
VALUE eval(char * code) {
int status = 0;
VALUE args[2];
args[0] = reinterpret_cast<VALUE>(this);
args[1] = rb_str_new2(code);
VALUE val = rb_protect(eval_protect,
reinterpret_cast<VALUE>(args),
&status);
if (status) { }
return val;
}

VALUE & sandbox() {
return sandbox_;
}
private:
VALUE sandbox_;
static VALUE eval_protect(VALUE data) {
VALUE *args = reinterpret_cast<VALUE *>(data);
return rb_funcall(reinterpret_cast<Sandbox *>(args[0])-
>sandbox(),
rb_intern("eval"),
1,
args[1]);
}
};

-- Daniel


MenTaLguY

8/9/2006 6:27:00 PM

0

On Wed, 2006-08-09 at 06:03 +0900, Francis Cianfrocca wrote:
> Is it possible to make Sandbox::eval_protect a static function? Because as
> an instance method, it's prototype is not VALUE(*)(VALUE) but rather
> something like VALUE(*)(Sandbox&, VALUE).

Specifically, VALUE (Sandbox::*)(VALUE). Pointers to non-static methods
are actually a totally separate family of types to function pointers and
cannot be called without first using the .* or ->* operators to "bind"
them. This is similar to the difference between UnboundMethod and
Proc/Method in Ruby.

-mental

Satish

5/24/2013 5:59:00 AM

0

On May 23, 8:57 pm, rst0 <rst0w...@yahoo.com> wrote:
> There is a girl (old woman by now) who kept all of us high school
> friends in contact and send pictures, which one had died, which one in
> a hospital,...
>
> We'll thinking doing another class-reunion in 2015.

rst0/7/9, I hope you aren't the only one who will get his catheter and
diaper changed periodically at the reunion.

rst9

5/24/2013 5:14:00 PM

0

On May 23, 10:59 pm, Satish <sk.c...@gmail.com> wrote:
> On May 23, 8:57 pm, rst0 <rst0w...@yahoo.com> wrote:
>
> > There is a girl (old woman by now) who kept all of us high school
> > friends in contact and send pictures, which one had died, which one in
> > a hospital,...
>
> > We'll thinking doing another class-reunion in 2015.
>
> rst0/7/9, I hope you aren't the only one who will get his catheter and
> diaper changed periodically at the reunion.

Are you having fun masturbating yourself, Satish?

Satish

5/24/2013 5:33:00 PM

0

On May 24, 10:14 am, rst9 <rst9w...@yahoo.com> wrote:
> On May 23, 10:59 pm, Satish <sk.c...@gmail.com> wrote:
>
> > On May 23, 8:57 pm, rst0 <rst0w...@yahoo.com> wrote:
>
> > > There is a girl (old woman by now) who kept all of us high school
> > > friends in contact and send pictures, which one had died, which one in
> > > a hospital,...
>
> > > We'll thinking doing another class-reunion in 2015.
>
> > rst0/7/9, I hope you aren't the only one who will get his catheter and
> > diaper changed periodically at the reunion.
>
> Are you having fun masturbating yourself, Satish?

You seem fixated on masturbation - not surprising for a 5-year old man
who needs help to get his catheter and his soiled diapers changed.


Your flippancy might earn you 50 cents from the CCP dictatorship but
not much else.

Look once again at the the photo you have posted from your younger
days. You were a CCP fan even in those days.


http://img16.imageshack.us/img16/3928/chin...

That might earn you 50 cents from the CCP dictatorship but not much
else.

http://img16.imageshack.us/img16/3928/chin... ;

You have no mind of your own - your mind is a slave to CCP's
imperialist designs against China's neighbors. You have sold your mind
not for the proverbial "thirty pieces of silver" but for 50 cents per
post from the CCP:

*****************
http://en.wikipedia.org/wiki/50_...

The 50 Cent Party are Internet commentators (?????, ?????, wanglù
pínglùn yuán) hired by the government of the People's Republic of
China (both local and central) or the Communist Party to post comments
favorable towards party policies in an attempt to shape and sway
public opinion on various Internet message boards. The commentators
are said to be paid for every post that either steers a discussion
away from anti-party or sensitive content on domestic websites,
bulletin board systems, and chatrooms, or that advances the Communist
party line.

*******************


rst0/9, you know which side of the bread is buttered. You know enough
of the life under the Beijing regime not to relocate to the land of
your birth to live under CCP dictatorship. You will rather shack up
with your gf in USA and live off US social security checks. But you
have no qualms about turning yourself into a stooge and cheerleader of
the CCP dictatorship in Beijing. Mr. Quisling would have been proud of
you.



PRC is a one-party state. The Communist Party has a monopoly of power.
No other party/opinion is allowed to coexist.

The CCP dictatorship is much like the military dictatorship in
Myanmar. The Military had a monopoly on power. When a General retired
as the top honcho of the state, another General succeeded him.

The CCP dictatorship is no different.

And your step mother knew that too. That is why, as soon as the CCP
captured power in 1949, she had her 11 year old step son (you) flee
Mainland China. You sought refuge in USA with a "father" whom you had
never met before.

BTW, how did your absentee father impregnate your mother? Did he send
his seeds to your mother by courier service?


Before bad mouthing USA, remember, if USA had not taken on Tojo's
Japan, China would have become a part of the Japanese "co-prosperity
sphere".


PRC's colonial claim against all neighbors of the Beijing regime rest
firmly on imperialist actions of the past. It cannot be justified.
Before callingthe kettle black, the pot should look at itself more
critically. The way the CCP dictatorship is advancing its imperialist
ambitions is truly breath-taking. It has taken a lot of chutzpah for
it to carve out that U-shaped claim on the South China Sea for itself.
The whole purpose of the CCP regime's obscene claim is to claim most
of the South China Sea as its territorial waters for the purpose of
usurping the natural resources in the sea and on the sea-bed and under
for itself.

China, for example, has a long history of imperial campaigns against
Vietnam. Ho Chi Minh had led the struggle for many many years against
the French and the USA. But he well knew who were the worst enemy.That
is why he had commented at the height of his fight against French
colonial rulers, "It is better to sniff French dung for a while than
to eat Chinese dung all our lives".

CCP's imperialist campaign against its neighbors well proves how right
Ho Chi Minh was.

rst9

5/24/2013 5:42:00 PM

0

Are you having fun masturbating yourself, Satish?
What does a filthy dirty smelly dark skin Indian like you doing in
Overland Park, Kansas, Satish?
There is nothing in Kansas except tornadoes.