This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: implementing data tags via object properties -- help with debugging?


 > From: thi <ttn@mingle.glug.org>
 > Date: Fri  May  7,  9:24am
 >
 > one immediately apparent problem is using `gh_eval_str'.  you probably
 > want to simply save the return value of `gh_new_procedure', an SCM.

Well, doing that defeats the purpose of trying not to have global variables!
Perhaps I was too idealist to believe that I could avoid use of global
variables in Guile.  It was so easy in X-Windows!
 > 
 > however, there may be a bigger problem: the stuff below looks like you
 > are re-implementing smobs.  why?

I am following the many examples presented in example-smob and on the SWIG
web pages.  The only difference is that, instead of associating a struct
with the "CDR", I'm associating a generic pointer that can be used in
any old way in C.  As I said before, I cannot afford the time and
error-prone-ness to smob-ify all the data structures in this program.
(Especially without access to SWIG.)

This seems like it would be fairly frequent need.  I mean, do programs like
scwm, GUSH, etc. use global variables?  Or are those projects not starting
with a C program and building a Scheme interface to it?

 > thi

Thanks for responding.

 > ------------------------------------
 > Robert Brown writes:
 > 
 >  > struct genericptr {
 >  >   void* ptr;
 >  >   SCM name;
 >  > };
 >  > 
 >  > static SCM
 >  > mark_genericptr (SCM genericptr_smob)
 >  > {
 >  >   struct genericptr *genericptr = (struct genericptr *) SCM_CDR (genericptr_smob);
 >  > 
 >  >   scm_gc_mark (genericptr->name);
 >  >   return SCM_UNSPECIFIED;
 >  > }
 >  > 
 >  > static scm_sizet
 >  > free_genericptr (SCM genericptr_smob)
 >  > {
 >  >   struct genericptr *genericptr = (struct genericptr *) SCM_CDR (genericptr_smob);
 >  >   scm_sizet size = sizeof (struct genericptr);
 >  > 
 >  >   free (genericptr);
 >  > 
 >  >   return size;
 >  > }
 >  > 
 >  > SCM gh_ptr2scm(void* ptr)
 >  > {
 >  >   struct genericptr* newlib;
 >  >   SCM genericptr_smob;
 >  >   static scm_smobfuns genericptr_funs = {
 >  >     mark_genericptr, free_genericptr, 0, 0
 >  >   };
 >  > 
 >  >   newlib=(struct genericptr *)scm_must_malloc(sizeof(struct genericptr), "genericptr");
 >  >   newlib->ptr=ptr;
 >  >   newlib->name=gh_str02scm("genericptr");
 >  >   SCM_NEWCELL (genericptr_smob);
 >  >   SCM_SETCDR (genericptr_smob, newlib);
 >  >   SCM_SETCAR (genericptr_smob, scm_newsmob(&genericptr_funs));
 >  >   return genericptr_smob;
 >  > }
 >  > 
 >  > void* gh_scm2ptr(SCM ptr)
 >  > {
 >  >   return (void*)SCM_CDR(ptr);
 >  > }
 >  > 
 >  > void gh_set_ext_data(SCM scm, void* ptr)
 >  > {
 >  >   scm_set_object_property_x(scm, gh_symbol2scm("genericptr"), gh_ptr2scm(ptr));
 >  > }
 >  > 
 >  > void* gh_get_ext_data(SCM scm)
 >  > {
 >  >   return gh_scm2ptr(scm_object_property(scm, gh_symbol2scm("genericptr")));
 >  > }
 >  > 
 >  > void* gh_get_tag_data(const char* procname)
 >  > {
 >  >   return gh_scm2ptr(scm_object_property(gh_eval_str((char*)procname), gh_symbol2scm("genericptr")));
 >  > }
 >  > 
 >  > SCM ServerReadSymbols(SCM scmx)
 >  > {
 >  > 
 >  >      .
 >  >      .
 >  >      .
 >  > 
 >  >   ReadSymbols(x, (Library*)gh_get_tag_data("ReadSymbols"));
 >  > 
 >  >      .
 >  >      .
 >  >      .
 >  > 
 >  > }
 >  > 
 >  >      .
 >  >      .
 >  >      .
 >  > 
 >  >   gh_set_ext_data(gh_new_procedure1_0("ReadSymbols", ServerReadSymbols),
 >  >                   (void*)lib);

-- 
Robert Brown                        |         Continuum Software, Inc.
email:robertb@continuumsi.com       | 800 West Cummings Park, Ste 4950
http://www.continuumsi.com/~robertb |            Woburn, MA 01801-6504
voice: 781-932-8400 x100            |                fax: 781-932-2558


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]