guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: smob pointer moving


From: Steve Tell
Subject: Re: smob pointer moving
Date: Sun, 2 Feb 2003 01:38:31 -0500 (EST)

On Sat, 1 Feb 2003, Noah Roberts wrote:

> I have been trying to figure out how to get a C++ class working in guile 
> through smobs.  I have suceeded in getting it to create an instance, and 
> the print call works appropriately.  However, when I am calling a 
> specific method (only one tried so far) the pointer to the object has 
> been altered so that it no longer points at the correct 'this'.  Here is 
> some relevant pieces of code:

> SCM db_open(SCM obj)
> {
>   Database *db = DB(db);

I think you mean DB(obj) above.   Typo in email, or perhaps your real
problem?

One other thing wrong that I see here:  You're not checking that 
obj really is a database smob before casting it and chasing the
(possibly random) pointer.

Following the example of some guile-based code, I always define a C macro
for this (named MyType_P in the scheme style), and sometimes also a
"VALIDATE_ARG" macro that does the comparison and throws a guile error if
my produre is called with some other scheme value.

Assuming db_id is the value you got back from scm_make_smob_type(), I'd
write somthing like this (adapted on the fly to your example, may have
minor errors):

#define Database_P(X) (SCM_NIMP(X) && SCM_CAR(X) == (SCM)db_id)

#define VALIDATE_ARG_Database(pos,scm) \
  do { \
  if (!Database_P(scm)) scm_wrong_type_arg(FUNC_NAME,pos,scm); \
  } while (0)

(recent docs indicate that SCM_SMOB_PREDICATE() and SCM_ASSERT() are
available for this, probably a better choice.)

>   printf("Database is at %p\n", db); 
>       db->open();
>   printf("db_open called.\n");
>   return SCM_EOL;
> }

See also:  "Defining New Types (Smobs)" in the guile reference
manual (section 18.3 in the guile-1.6.3 manual), and note

"18.2.6 Signalling Type Errors:
"Every function visible at the Scheme level should aggressively check the
types of its arguments, to avoid misinterpreting a value, and perhaps
causing a segmentation fault. Guile provides some macros to make this
easier."

I'm sure someone will (please) correct me if I've misstated anything above
:-)

Steve

--
Steve Tell  address@hidden 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]