scoop.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * File: scoop.c $XConsortium: scoop.c /main/3 1995/10/26 15:38:19 rswiston $
  25. * Language: C
  26. *
  27. * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
  28. *
  29. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  30. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  31. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  32. * (c) Copyright 1993, 1994 Novell, Inc. *
  33. */
  34. #include <bms/sbport.h> /* This must be the first file included */
  35. #include <bms/scoop.h>
  36. static void system_class_init (object_clasp to_do, object_clasp on_behalf_of);
  37. static void system_object_init (object_clasp c, object *p);
  38. static void root_class_init (object_clasp c);
  39. static void root_object_init (object *p);
  40. static object *root_clone (object *this_ptr, object *clone);
  41. static object *root_new (object_clasp c);
  42. static void root_free (object *p);
  43. /*--------------------------------------------------------------------------+*/
  44. void object_destroy (object *p)
  45. /*--------------------------------------------------------------------------+*/
  46. {
  47. memf (p->, free_obj, (p)) ;
  48. }
  49. /*--------------------------------------------------------------------------+*/
  50. object *object_create (object_clasp c)
  51. /*--------------------------------------------------------------------------+*/
  52. {
  53. object *p ;
  54. if (! c->init) { system_class_init (c, c) ; }
  55. p = (*(c->new_obj))(c) ;
  56. if (p)
  57. object_init (c, p) ;
  58. return p ;
  59. }
  60. /*--------------------------------------------------------------------------+*/
  61. void object_init (object_clasp c, object *p)
  62. /*--------------------------------------------------------------------------+*/
  63. {
  64. if (! c->init) { system_class_init (c, c) ; }
  65. p->class_ptr = c ;
  66. system_object_init (c, p) ;
  67. }
  68. /*--------------------------------------------------------------------------+*/
  69. static void system_class_init (object_clasp to_do, object_clasp on_behalf_of)
  70. /*--------------------------------------------------------------------------+*/
  71. {
  72. object_clasp base ;
  73. if ( on_behalf_of && to_do && ! on_behalf_of->init )
  74. { }
  75. else
  76. return ;
  77. if (to_do == on_behalf_of && to_do != root_class)
  78. { on_behalf_of->base = *((object_clasp *) (on_behalf_of->base)) ;
  79. } ;
  80. base = to_do->base ;
  81. system_class_init (base, base) ;
  82. system_class_init (base, on_behalf_of) ;
  83. if (to_do == on_behalf_of)
  84. { to_do->object_init = root_object_init ;
  85. to_do->new_obj = root_new ;
  86. to_do->free_obj = root_free ;
  87. } ;
  88. #ifdef GLSDEBUG
  89. printf ("-> '%s' class for '%s'\n",
  90. to_do->name, on_behalf_of->name) ;
  91. #endif /* GLSDEBUG */
  92. (*(to_do->class_init))(on_behalf_of);
  93. if (to_do == on_behalf_of) on_behalf_of->init = TRUE ;
  94. }
  95. /*--------------------------------------------------------------------------+*/
  96. static void system_object_init (object_clasp c, object *p)
  97. /*--------------------------------------------------------------------------+*/
  98. {
  99. if (!c) return ;
  100. #ifdef GLSDEBUG
  101. printf (" '%s' object for '%s'\n", c->name, p->class_ptr->name) ;
  102. #endif /* GLSDEBUG */
  103. system_object_init (c->base, p) ;
  104. #ifdef GLSDEBUG
  105. printf ("-> '%s' object for '%s'\n", c->name, p->class_ptr->name) ;
  106. #endif /* GLSDEBUG */
  107. (*(c->object_init))(p) ;
  108. }
  109. /*--------------------------------------------------------------------------+*/
  110. static void root_class_init (object_clasp c)
  111. /*--------------------------------------------------------------------------+*/
  112. {
  113. c->clone = root_clone ;
  114. }
  115. /*--------------------------------------------------------------------------+*/
  116. static void root_object_init (object * UNUSED_PARM(p))
  117. /*--------------------------------------------------------------------------+*/
  118. {
  119. }
  120. /*
  121. The clone (second) argument to the clone method may be either NULL or an
  122. object pointer. If NULL, a space of the size necessary for an object of
  123. the same class as the primary object (this) will be allocated. If the
  124. input argument is not null, it is assumed that the caller has verified
  125. there is sufficient space for a copy of the original object.
  126. If the clone procedure does allocate the space for the clone, it only
  127. allocates the space. It does not do object initialization.
  128. */
  129. /*--------------------------------------------------------------------------+*/
  130. static object *root_clone (object *this_ptr, object *clone)
  131. /*--------------------------------------------------------------------------+*/
  132. {
  133. XeString orig = (XeString) this_ptr ;
  134. XeString copy ;
  135. object_clasp o_class = this_ptr->class_ptr ;
  136. OSizeType size = o_class->object_size ;
  137. if ( ! clone )
  138. clone = (*(o_class->new_obj))(o_class) ;
  139. if (clone) {
  140. copy = (XeString) clone ;
  141. while (size--)
  142. *copy++ = *orig++ ;
  143. }
  144. return clone ;
  145. }
  146. /*--------------------------------------------------------------------------+*/
  147. static object *root_new (object_clasp c)
  148. /*--------------------------------------------------------------------------+*/
  149. { object *p = (object *)malloc((unsigned) c->object_size) ;
  150. /* Don't use XeMalloc here, we want to be able to grab our Edit */
  151. /* widget without the rest of the world being pulled in. */
  152. if (!p)
  153. {
  154. fprintf(stderr, "scoop: malloc in root_new failed, out of memory!\n");
  155. exit(1);
  156. }
  157. return p ;
  158. }
  159. /*--------------------------------------------------------------------------+*/
  160. static void root_free (object *p)
  161. /*--------------------------------------------------------------------------+*/
  162. {
  163. if (p) free ((char *)p) ;
  164. }
  165. struct root_class root_class_struct = {
  166. (object_clasp) NULL, /* root has no base class */
  167. "root", /* class name */
  168. root_class_init,
  169. sizeof (object),
  170. 0,
  171. } ;
  172. object_clasp root_class = & root_class_struct ;