test_object.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #if !defined(_TEST_OBJECT_H_) && 0
  23. #define _TEST_OBJECT_H_
  24. typedef struct person_s {
  25. TSK_DECLARE_OBJECT; // Mandatory
  26. char* name;
  27. struct person_s* girlfriend;
  28. }
  29. person_t;
  30. // (a student is a person)
  31. typedef struct student_s {
  32. struct person_s* person; // Must be the first element
  33. char* school;
  34. }
  35. student_t;
  36. //// (as a student is a person you can do)
  37. //student_t* s;
  38. //((person_t*)s)->name = tsk_strdup("bob");
  39. // constructor
  40. static tsk_object_t* person_create(tsk_object_t * self, va_list * app)
  41. {
  42. person_t *person = self;
  43. if(person) {
  44. person->name = tsk_strdup(va_arg(*app, const char *));
  45. }
  46. return self;
  47. }
  48. // destructor
  49. static tsk_object_t* person_destroy(tsk_object_t * self)
  50. {
  51. person_t *person = self;
  52. if(person) {
  53. TSK_FREE(person->name);
  54. tsk_object_unref(person->girlfriend);
  55. }
  56. return self; // return
  57. }
  58. // comparator
  59. static int person_cmp(const tsk_object_t *_p1, const tsk_object_t *_p2)
  60. {
  61. const person_t *p1 = _p1;
  62. const person_t *p1 = _p2;
  63. int ret;
  64. // do they have the same name?
  65. if((ret = tsk_stricmp(p1->name, p2->name))) {
  66. return ret;
  67. }
  68. // do they have the same girlfriend?
  69. if((ret = tsk_object_cmp(p1->girlfriend, p2->girlfriend))) {
  70. return ret;
  71. }
  72. // they are the same
  73. return 0;
  74. }
  75. //(Object defnition)
  76. static const tsk_object_def_t person_def_t = {
  77. sizeof(person_t),
  78. person_create,
  79. person_destroy,
  80. person_cmp
  81. };
  82. // create a person
  83. #define PERSON_CREATE(name) tsk_object_new(&person_def_t, (const char*)name)
  84. /* test object */
  85. void test_object()
  86. {
  87. // creates a person: will call the constructor
  88. person_t* bob = PERSON_CREATE("bob");
  89. // creates bob's girlfriend
  90. bob->girlfriend = PERSON_CREATE("alice");
  91. // deletes bob: will delete both bob and bob's girlfriend field by calling their destructors
  92. TSK_OBJECT_SAFE_FREE(bob);
  93. }
  94. #endif /* _TEST_OBJECT_H_ */