]> gitweb.factorcode.org Git - factor.git/commitdiff
bitwise equal? for struct objects
authorJoe Groff <arcata@gmail.com>
Wed, 26 Aug 2009 23:05:38 +0000 (18:05 -0500)
committerJoe Groff <arcata@gmail.com>
Wed, 26 Aug 2009 23:05:38 +0000 (18:05 -0500)
basis/classes/struct/struct-tests.factor
basis/classes/struct/struct.factor
basis/libc/libc.factor

index 6c7a4cf35d78a26dc62adebce9c339713c1524c2..d788b9bdb1d801fbb792911eba9c77d3a2c77491 100644 (file)
@@ -4,7 +4,7 @@ alien.structs.fields alien.syntax ascii classes.struct combinators
 destructors io.encodings.utf8 io.pathnames io.streams.string
 kernel libc literals math multiline namespaces prettyprint
 prettyprint.config see sequences specialized-arrays.ushort
-system tools.test ;
+system tools.test combinators.short-circuit ;
 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: 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
     { x int }
     { y int } ;
index 81252656a4a1835019dd94f375572473e665e557..07515bc8431f854ad810be221c07e7ff6d9c02ea 100644 (file)
@@ -2,10 +2,10 @@
 USING: accessors alien alien.c-types alien.structs
 alien.structs.fields arrays byte-arrays classes classes.parser
 classes.tuple classes.tuple.parser classes.tuple.private
-combinators combinators.smart fry generalizations generic.parser
-kernel kernel.private lexer libc macros make math math.order
-parser quotations sequences slots slots.private struct-arrays
-vectors words ;
+combinators combinators.short-circuit combinators.smart fry
+generalizations generic.parser kernel kernel.private lexer
+libc macros make math math.order parser quotations sequences
+slots slots.private struct-arrays vectors words ;
 FROM: slots => reader-word writer-word ;
 IN: classes.struct
 
@@ -28,6 +28,12 @@ PREDICATE: struct-class < tuple-class
 M: struct >c-ptr
     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 )
     over c-ptr? [ swap \ c-ptr bad-slot-value ] unless
     tuple-layout <tuple> [ 2 set-slot ] keep ;
index 926a6c4ec4932cadc11d94964bcf89680abe9427..4142e40c6840671b653248e783e9844f76affa3d 100644 (file)
@@ -83,6 +83,12 @@ PRIVATE>
 : memcpy ( dst src size -- )
     "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 )
     "size_t" "libc" "strlen" { "char*" } alien-invoke ;