chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Question on Chickens C++ interface.


From: Matthias Heiler
Subject: [Chicken-users] Question on Chickens C++ interface.
Date: 26 Apr 2004 09:49:16 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Hi,

I'm currently trying to integrate a C++ library into Chicken.  This
library works a lot with passing around C-arrays containing C++
objects.  I don't currently understand how to build and pass around
such arrays from Chicken.

A simplified source code example is:

===
declare (uses extras lolevel srfi-4 tinyclos))

; Include into generated code, but don't parse:
#>
#include <cassert>
#include <iostream>

using namespace std; 

class SomeClass {
public:
  SomeClass(int i) : i(i) { }
  virtual int data() { return i; }
private:
  int i;
};

class SomeContainer {
public:
  SomeContainer() { }

  void pass_ptr(SomeClass* p, int sz) {
     ptr = p;
     size = sz;
     for (int i = 0; i < size; ++i) {
        cout << ptr[i].data() << ' ';
     }
     cout << endl;
  }

  void check() {
     for (int i = 0; i < size; ++i) {
        assert(ptr[i].data() == i);
     }
  }

private:
  SomeClass* ptr;
  int size;
};
<#

; Parse but don't embed:
#>?
<#

; Parse and embed:
#>!
SomeClass* make_some_class(int i) {
  return (new SomeClass(i));
}

void print_some_class(SomeClass* ptr) {
  cout << "SC: " << ptr->data() << endl;
}

SomeContainer* make_sc() {
  return (new SomeContainer);
}

void pass_ptr_sc(SomeContainer* obj, SomeClass* ptr, int sz) {
  obj->pass_ptr(ptr, sz);
}

void check_sc(SomeContainer* obj) {
  obj->check();
}

int ptr2int(void* ptr) {    // HACK
   cout << ptr << endl;
   return (reinterpret_cast<int>(ptr));
}

void* int2ptr(int i) {      // HACK
   return (reinterpret_cast<void*>(i));
}

<#

(define obj (make_sc))

(define vec (make-s32vector 1024 0))

(define sc5 (make_some_class 5))

(print_some_class sc5)

(print 'check1)

(do ((i 0 (+ 1 i)))
    ((= i 10))
  (let ((sc (make_some_class i)))          ; create object
    (print_some_class sc)                  ; show it works
    (s32vector-set! vec i (ptr2int sc))))  ; put it into s32vector

(print 'check2)
(do ((i 0 (+ 1 i)))             ; check if objects in vector still work
    ((= i 10))
  (print_some_class (int2ptr (s32vector-ref vec i))))

(print 'check3)
(print (object->pointer vec))
(print (object->pointer (make-locative vec)))

; the following line segfaults:
(pass_ptr_sc obj (make-locative vec) 10) ; pass vector to container

(print 'check4)
(check_sc obj)

(print 'done)
===

Here I essentially store the pointers on C++ objects in a s32vector
and try to pass a pointer to a container class. Unfortunately, this
doesn't work (but segfaults).

So my questions are:

1.) How do I create a C array of C++ objects and pass it around properly?
2.) How do I make sure it doesn't get garbage collected while the C++ 
    container is still using it?  Is it dangerous if the C++ container
    deletes the vector?  (Unfortunately, the C++ code I have to use does
    many things a little "unconventionally".)
3.) What are the differences between locatives, pointers, and addresses 
    in Chicken?  When do I use which?

Thanks a lot,

  Matthias





reply via email to

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