Commits

Dan Buch  committed 435ff62

working through implementation of String type, though seem to be lacking some key parts (me? or the text?)

  • Participants
  • Parent commits 02e19d1

Comments (0)

Files changed (5)

 CC := gcc
-CFLAGS := -Wall -pipe -fPIC
+CFLAGS := -Wall -pipe
 BINDIR := $(PWD)/bin
 SRCDIR := src
 
 CH2_OBJECTS = $(patsubst %.c,%.o,$(wildcard $(SRCDIR)/ch2/*.c))
 EXECUTABLES = $(BINDIR)/ch1 $(BINDIR)/ch2
 
-QUIET_CC = @echo '    CC' $@;
-
-
 all: $(EXECUTABLES)
 
 
 	rm -f $(BINDIR)/*
 
 %.o: %.c %.h
-	$(QUIET_CC)$(CC) $(CFLAGS) -g -c -o $@ $<
+	$(CC) $(CFLAGS) -g -c -o $@ $<
 
 
 $(BINDIR)/ch1: $(CH1_OBJECTS)
-	$(QUIET_CC)$(CC) $(CFLAGS) -g -o $@ $^
+	$(CC) $(CFLAGS) -g -o $@ $^
 
 
 $(BINDIR)/ch2: $(CH2_OBJECTS)
-	$(QUIET_CC)$(CC) $(CFLAGS) -g -o $@ $^
+	$(CC) $(CFLAGS) -g -o $@ $^
 
 
 .PHONY: all clean

File src/ch2/Class.c

 #include "common.h"
 
-/* void * new (const void * _class, ...)
- * {
- *     const struct Class * class = _class;
- *     void * p = calloc(1, class -> size);
-
- *     assert(p);
-
- *     * (const struct Class **) p = class;
-
- *     if (class->ctor)
- *     {
- *         va_list ap;
-
- *         va_start(ap, _class);
- *         p = class->ctor(p, &ap);
- *         va_end(ap);
- *     }
- *     return p;
- * } */
-
 void *
 new(const void * _class, ...)
 {
     void * p = calloc(1, class -> size);
 
     assert(p);
+
     * (const struct Class **) p = class;
 
-    if (class -> ctor)
+    if (class->ctor)
     {
         va_list ap;
         va_start(ap, _class);
-        p = class -> ctor(p, & ap);
+        p = class->ctor(p, &ap);
         va_end(ap);
     }
     return p;
 }
 
 
-
 void
 delete(void * self)
 {
     const struct Class * const * cp = self;
 
     assert(self && *cp);
-    void * new_inst = new(self);
-    return new_inst;
+    return new(self);
 }
 
 

File src/ch2/String.c

 #include "common.h"
 
-const void * String = & _String;
+#define const_getself(a) const struct String * self = a;
+#define getself(a) struct String * self = a;
 
 
-/*
-static void * String_ctor (void * _self, va_list * app)
+static void * String_ctor(void * _self, va_list * app)
 {
-    struct String * self = _self;
+    getself(_self);
     const char * text = va_arg(* app, const char *);
 
     self->text = malloc(strlen(text) + 1);
     strcpy(self->text, text);
     return self;
 }
-*/
+
+
+static void * String_dtor(void * _self)
+{
+    getself(_self);
+
+    free(self->text);
+    self->text = 0;
+    return self;
+}
+
+
+static void * String_clone(const void * _self)
+{
+    const_getself(_self);
+    return new(String, self->text);
+}
+
+
+static int String_differ(const void * _self, const void * _b)
+{
+    const_getself(_self);
+    const struct String * b = _b;
+
+    if (self == b) {
+        return 0;
+    }
+
+    if (!b || b->class != String) {
+        return 1;
+    }
+
+    return strcmp(self->text, b->text);
+}
+
+
+static const struct Class _String = {
+    sizeof(struct String),
+    String_ctor,
+    String_dtor,
+    String_clone,
+    String_differ
+};
+
+const void * String = & _String;
 
 
 /* vim:filetype=c

File src/ch2/String.h

     char * text;
 };
 
-extern const void * String;
-static const size_t _String = sizeof(struct String);
+const void * String;
+
 
 #endif
 

File src/ch2/common.h

 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <string.h>
 #include <assert.h>
 
 #include "Set.h"