FreeBASIC  0.91.0
array_clearobj.c
Go to the documentation of this file.
1 /* ERASE for static arrays of objects: re-init the elements */
2 
3 #include "fb.h"
4 
5 void fb_hArrayCtorObj( FBARRAY *array, FB_DEFCTOR ctor, size_t base_idx )
6 {
7  size_t i, elements, element_len;
8  FBARRAYDIM *dim;
9  const char *this_;
10 
11  if( array->ptr == NULL )
12  return;
13 
14  dim = &array->dimTB[0];
15  elements = dim->elements - base_idx;
16  ++dim;
17 
18  for( i = 1; i < array->dimensions; i++, dim++ )
19  elements *= dim->elements;
20 
21  /* call ctors */
22  element_len = array->element_len;
23  this_ = (const char *)array->ptr;
24 
25  while( elements > 0 ) {
26  /* !!!FIXME!!! check exceptions (only if rewritten in C++) */
27  ctor( this_ );
28  this_ += element_len;
29  --elements;
30  }
31 }
32 
34  (
35  FBARRAY *array,
36  FB_DEFCTOR ctor,
37  FB_DEFCTOR dtor,
38  int dofill /* legacy */
39  )
40 {
41  /* destruct all objects in the array
42  (dtor can be NULL if there only is a ctor) */
43  if( dtor )
44  fb_ArrayDestructObj( array, dtor );
45 
46  if( dofill ) {
47  /* re-initialize (ctor can be NULL if there only is a dtor) */
48  if( ctor )
49  /* if a ctor exists, it should handle the whole initialization */
50  fb_hArrayCtorObj( array, ctor, 0 );
51  else
52  /* otherwise, just clear */
53  fb_ArrayClear( array, 0 );
54  }
55 
56  return fb_ErrorSetNum( FB_RTERROR_OK );
57 }