Advertisement
Guest User

human

a guest
May 15th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /*-
  2.  * Copyright (c) 2012 valsorym <[email protected]>.
  3.  * All rights reserved.
  4.  *
  5.  * Discussion on the forums.freebsd.org.
  6.  *  url: http://forums.freebsd.org/showthread.php?p=177208
  7.  */
  8.  
  9. /*
  10.  * Very simple class, for example OOD in ANSI C.
  11.  */
  12.  
  13. #ifndef HUMAN_H
  14. #define HUMAN_H
  15.  
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "objectmode.h"
  19.  
  20. /* NEW TYPES */
  21. /* ************************************************************************* */
  22. /*
  23.  * Class has information only human age and it name.
  24.  * For security, we use the methods of initialization parameters.
  25.  */
  26. typedef struct {
  27.     w_class *class; /* class description */
  28.    
  29.     int age;
  30.     char *name;
  31.    
  32.     int (*set_age)(void *, int );
  33.     char *(*set_name)(void *, char *);
  34. } w_human;
  35.  
  36. /* INTERFACE */
  37. /* ************************************************************************* */
  38. static int set_age(void *, int );
  39. static char *set_name(void *, char *);
  40.  
  41. /* IMPLEMENTATION */
  42. /* ************************************************************************* */
  43. /* Constructor and Destructor realisation. */
  44. static void
  45. *create(void *self, va_list *app)
  46. {
  47.     w_class *c = (w_class *)self;
  48.     w_human *p = (w_human *)malloc(c->size);
  49.  
  50.     if (p) {
  51.         p->class = c;
  52.         p->set_age = set_age;
  53.         p->set_name = set_name;
  54.        
  55.         p->set_age(p, (int)va_arg(*app, const int));
  56.         p->set_name(p, (char *)va_arg(*app, const char *));
  57.     }
  58.  
  59.     return p;
  60. }
  61.  
  62. static void
  63. *destroy(void *self)
  64. {
  65.     w_human *p = (w_human *)self;
  66.    
  67.     free(p);
  68.     return self;
  69. }
  70.  
  71. /*
  72.  * Change/Set age of human.
  73.  */
  74. static int
  75. set_age(void *self, int age)
  76. {
  77.     w_human *p = (w_human *)self;
  78.    
  79.     if (age < 0 || age > 110)
  80.         age = 0;
  81.        
  82.     if (self && p->set_age) {
  83.         p->age = age;
  84.         return 1;
  85.     }
  86.    
  87.     return 0;
  88. }
  89.  
  90. /*
  91.  * Change/Set name of human.
  92.  * Returns a new name.
  93.  */
  94. static char
  95. *set_name(void *self, char *name) {
  96.     w_human *p = (w_human *)self;
  97.     if (self && p->set_name) {
  98.         free(p->name);
  99.         p->name = (char *)malloc((strlen(name)+1)*sizeof(char));
  100.         if (p->name == NULL)
  101.             return NULL;
  102.        
  103.         strcpy(p->name, name);
  104.         return p->name;
  105.     }
  106.    
  107.     return NULL;
  108. }
  109.  
  110. /* The point of initialization. */
  111. static w_class _human = {
  112.    sizeof(w_human), create, destroy
  113. };
  114.  
  115. w_class *i_human = &_human;
  116.  
  117. #endif
  118.  
  119. /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement