home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / VNEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.0 KB  |  81 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - vnew.cpp
  3.  * C++ VECTOR_NEW
  4.  * Called internally by the compiler to allocate arrays of classes
  5.  * having constructors
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1990 by Borland International              |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20.  
  21. typedef void (near * constNNC)(void near*);
  22. typedef void (near * constNFC)(void far*);
  23. typedef void pascal (near * constNNP)(void near*);
  24. typedef void pascal (near * constNFP)(void far*);
  25. typedef void (far * constFNC)(void near*);
  26. typedef void (far * constFFC)(void far*);
  27. typedef void pascal (far * constFNP)(void near*);
  28. typedef void pascal (far * constFFP)(void far*);
  29.  
  30. extern "C"
  31. void far * _vector_new_(void far *ptr,    // address of array, 0 means allocate it
  32.             size_t size,     // size of each object
  33.             int count,    // how many objects
  34.             int mode,    // actual type of constructor
  35.             ...
  36.                )
  37. /* This routine is used to initialize an array of class type.  If ptr is
  38.    NULL, it allocates the space for the array first.  Since the constructor
  39.    for the class may be of either memory model, and take an argument of any
  40.    memory model, we are forced to pass a mode parameter to tell us how to
  41.    cast it.  Since we must pass a near pointer for near functions and a
  42.    far call for far functions, we can't even know the argument type until
  43.    runtime, so we have to use varargs to get at it.
  44.  
  45.    The interpretation of the mode parameter is:
  46.     far function        0x01
  47.     pascal call        0x02
  48.     far pointer        0x04
  49. */
  50. {
  51.     va_list ap;        /* for access to parameters */
  52.     constNNC np;    /* Near pointer version */
  53.     constFNC fp;    /* Far pointer version */
  54.  
  55.     va_start(ap, mode);
  56.  
  57.     if (mode & 1)
  58.     fp = va_arg(ap, constFNC);
  59.     else
  60.     np = va_arg(ap, constNNC);
  61.  
  62.     if( ptr == 0 )
  63.     ptr = operator new(size * count);
  64.  
  65.     if( ptr != 0 ) {
  66.     for( char far*p = (char far*) ptr;  --count >= 0;  p += size )
  67.         switch (mode) {
  68.         case 0: np((void near *) p); break;
  69.         case 1: fp((void near *) p); break;
  70.         case 2: (*(constNNP) np)((void near *) p); break;
  71.         case 3: (*(constFNP) fp)((void near *) p); break;
  72.         case 4: (*(constNFC) np)((void far  *) p); break;
  73.         case 5: (*(constFFC) fp)((void far  *) p); break;
  74.         case 6: (*(constNFP) np)((void far  *) p); break;
  75.         default:(*(constFFP) fp)((void far  *) p); break;
  76.         }
  77.     }
  78.  
  79.     return ptr;
  80. }
  81.