From 4a5cb3aac3f2b85818a74fbc3bf18acd6f2ba4a4 Mon Sep 17 00:00:00 2001
From: Jeremy Hughes <jedahu@gmail.com>
Date: Sat, 11 Jul 2009 00:08:40 +1200
Subject: [PATCH] alien.inline: added with-c-library word

---
 basis/alien/inline/inline-docs.factor |  8 +++++++-
 basis/alien/inline/inline.factor      | 10 +++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/basis/alien/inline/inline-docs.factor b/basis/alien/inline/inline-docs.factor
index bce3f2530c..58eca558ea 100644
--- a/basis/alien/inline/inline-docs.factor
+++ b/basis/alien/inline/inline-docs.factor
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009 Jeremy Hughes.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: help.markup help.syntax kernel strings effects ;
+USING: help.markup help.syntax kernel strings effects quotations ;
 IN: alien.inline
 
 : $binding-note ( x -- )
@@ -198,6 +198,12 @@ HELP: delete-inline-library
 { $description "Delete the shared library file corresponding to " { $snippet "name" } "." }
 { $notes "Must be executed in the vocabulary where " { $snippet "name" } " is defined. " } ;
 
+HELP: with-c-library
+{ $values
+    { "name" string } { "quot" quotation }
+}
+{ $description "Calls " { $link define-c-library } ", then the quotation, then " { $link compile-c-library } ", then sets all variables bound by " { $snippet "define-c-library" } " to " { $snippet "f" } "." } ;
+
 ARTICLE: "alien.inline" "Inline C"
 { $vocab-link "alien.inline" }
 ;
diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor
index 37e01b5209..1df77d6600 100644
--- a/basis/alien/inline/inline.factor
+++ b/basis/alien/inline/inline.factor
@@ -6,7 +6,7 @@ generalizations grouping io.directories io.files
 io.files.info io.files.temp kernel lexer math math.order
 math.ranges multiline namespaces sequences source-files
 splitting strings system vocabs.loader vocabs.parser words
-alien.c-types alien.structs make parser ;
+alien.c-types alien.structs make parser continuations ;
 IN: alien.inline
 
 <PRIVATE
@@ -15,6 +15,10 @@ SYMBOL: library-is-c++
 SYMBOL: compiler-args
 SYMBOL: c-strings
 
+: cleanup-variables ( -- )
+    { c-library library-is-c++ compiler-args c-strings }
+    [ off ] each ;
+
 : function-types-effect ( -- function types effect )
     scan scan swap ")" parse-tokens
     [ "(" subseq? not ] filter swap parse-arglist ;
@@ -114,6 +118,10 @@ PRIVATE>
     c-library-name [ remove-library ]
     [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ;
 
+: with-c-library ( name quot -- )
+    [ [ define-c-library ] dip call compile-c-library ]
+    [ cleanup-variables ] [ ] cleanup ; inline
+
 SYNTAX: C-LIBRARY: scan define-c-library ;
 
 SYNTAX: COMPILE-AS-C++ t library-is-c++ set ;