bitwise equal? for struct objects

db4
Joe Groff 2009-08-26 18:05:38 -05:00
parent 469e4b526f
commit f4c90fdabe
3 changed files with 36 additions and 5 deletions

View File

@ -4,7 +4,7 @@ alien.structs.fields alien.syntax ascii classes.struct combinators
destructors io.encodings.utf8 io.pathnames io.streams.string destructors io.encodings.utf8 io.pathnames io.streams.string
kernel libc literals math multiline namespaces prettyprint kernel libc literals math multiline namespaces prettyprint
prettyprint.config see sequences specialized-arrays.ushort prettyprint.config see sequences specialized-arrays.ushort
system tools.test ; system tools.test combinators.short-circuit ;
IN: classes.struct.tests IN: classes.struct.tests
<< <<
@ -138,6 +138,25 @@ UNION-STRUCT: struct-test-float-and-bits
} }
} ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test } ] [ "struct-test-float-and-bits" c-type fields>> ] unit-test
STRUCT: struct-test-equality-1
{ x int } ;
STRUCT: struct-test-equality-2
{ y int } ;
[ t ] [
[
struct-test-equality-1 <struct> 5 >>x
struct-test-equality-1 malloc-struct &free 5 >>x =
] with-destructors
] unit-test
[ f ] [
[
struct-test-equality-1 <struct> 5 >>x
struct-test-equality-2 malloc-struct &free 5 >>y =
] with-destructors
] unit-test
STRUCT: struct-test-ffi-foo STRUCT: struct-test-ffi-foo
{ x int } { x int }
{ y int } ; { y int } ;

View File

@ -2,10 +2,10 @@
USING: accessors alien alien.c-types alien.structs USING: accessors alien alien.c-types alien.structs
alien.structs.fields arrays byte-arrays classes classes.parser alien.structs.fields arrays byte-arrays classes classes.parser
classes.tuple classes.tuple.parser classes.tuple.private classes.tuple classes.tuple.parser classes.tuple.private
combinators combinators.smart fry generalizations generic.parser combinators combinators.short-circuit combinators.smart fry
kernel kernel.private lexer libc macros make math math.order generalizations generic.parser kernel kernel.private lexer
parser quotations sequences slots slots.private struct-arrays libc macros make math math.order parser quotations sequences
vectors words ; slots slots.private struct-arrays vectors words ;
FROM: slots => reader-word writer-word ; FROM: slots => reader-word writer-word ;
IN: classes.struct IN: classes.struct
@ -28,6 +28,12 @@ PREDICATE: struct-class < tuple-class
M: struct >c-ptr M: struct >c-ptr
2 slot { c-ptr } declare ; inline 2 slot { c-ptr } declare ; inline
M: struct equal?
{
[ [ class ] bi@ = ]
[ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
} 2&& ;
: memory>struct ( ptr class -- struct ) : memory>struct ( ptr class -- struct )
over c-ptr? [ swap \ c-ptr bad-slot-value ] unless over c-ptr? [ swap \ c-ptr bad-slot-value ] unless
tuple-layout <tuple> [ 2 set-slot ] keep ; tuple-layout <tuple> [ 2 set-slot ] keep ;

View File

@ -83,6 +83,12 @@ PRIVATE>
: memcpy ( dst src size -- ) : memcpy ( dst src size -- )
"void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ; "void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ;
: memcmp ( a b size -- cmp )
"int" "libc" "memcmp" { "void*" "void*" "ulong" } alien-invoke ;
: memory= ( a b size -- ? )
memcmp 0 = ;
: strlen ( alien -- len ) : strlen ( alien -- len )
"size_t" "libc" "strlen" { "char*" } alien-invoke ; "size_t" "libc" "strlen" { "char*" } alien-invoke ;