unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Hans Aberg <haberg@math.su.se>
To: guile-user@gnu.org
Subject: C++ wrap
Date: Mon, 22 Feb 2010 15:01:29 +0100	[thread overview]
Message-ID: <B4C8C91F-D69A-4AF1-9432-A02CD813604A@math.su.se> (raw)

I have written a bit on a C++ wrap - has this been done? I use  
templates to get a static typed style similar to that of Haskell,  
which can be overridden at need. Some example code below.

   Hans


----
#include "src/guile.hh"

#include <iostream>
#include <list>
#include <utility>


int inner_main(std::pair<int, char**>* argp) {
   for (int i = 0; i < argp->first; ++i)
     std::cout << argp->second[i] << std::endl;

   using namespace scm;

   integer x = 2567575364397498ull;
   integer y = (uint64_t)2567575364397500ull;

   out << "-x = " << -x << "\ny = " << y << newline;

   // Checking object references and mutative operations:
   object w = x;
   ++x;
   out << "w = " << w << newline;
   out << "x = " << x << newline;

   function2<integer, integer, integer> f("-");
   // Using C++ output:
   std::cout << "f(x, y) = " << (char*)string(f(x, y), 10) << std::endl;

   // Loading a module, and using a function in it:

   object("(use-modules (ice-9 pretty-print))");
   function<> pretty("pretty-print");

   const char* text =
   "'(define (foo) (lambda (x) \
   (cond ((zero? x) #t) ((negative? x) -x) (else \
   (if (= x 1) 2 (* x x x))))))";

   out << "Function:\n";
   pretty(object(text));

   list<> ks(std::list<integer>(5, 6));
   out << "std::list<integer>(5, 6) = " << ks << newline << flush;

   function1<number, number> inc("(lambda (x) (+ x 1))");
   out << "inc(integer(3)) = " << inc(integer(3)) << newline;

   return EXIT_SUCCESS;
}


int main(int argc, char **argv) {
   std::pair<int, char**> arg(argc, argv);
   return scm::with_guile<std::pair<int, char**>*, int>(inner_main) 
(&arg);
}
----
$ g++ test.cc -lguile -o test && ./test
./test

-x = -2567575364397498
y = 2567575364397500
f(x, y) = -2
Function:
(define (foo)
   (lambda (x)
     (cond ((zero? x) #t)
           ((negative? x) -x)
           (else (if (= x 1) 2 (* x x x))))))
std::list<integer>(5, 6) = (6 6 6 6 6)
inc(integer(3)) = 4
----





             reply	other threads:[~2010-02-22 14:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-22 14:01 Hans Aberg [this message]
2010-02-22 19:06 ` C++ wrap Andy Wingo
2010-02-23 14:10   ` Hans Aberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B4C8C91F-D69A-4AF1-9432-A02CD813604A@math.su.se \
    --to=haberg@math.su.se \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).