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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - vnewv.cpp
  3.  * C++ VECTOR_NEW for virtual base classes.
  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 * consvNNC)(void near*, int);
  22. typedef void (near * consvNFC)(void far*, int);
  23. typedef void pascal (near * consvNNP)(void near*, int);
  24. typedef void pascal (near * consvNFP)(void far*, int);
  25. typedef void (far * consvFNC)(void near*, int);
  26. typedef void (far * consvFFC)(void far*, int);
  27. typedef void pascal (far * consvFNP)(void near*, int);
  28. typedef void pascal (far * consvFFP)(void far*, int);
  29.  
  30.  
  31. extern "C"
  32. void far * _vector_vnew_(void far* ptr,    // address of array, 0 means allocate it
  33.              size_t size,    // size of each object
  34.              int count,    // how many objects
  35.              int mode,    // actual type of constructor
  36.              ...
  37.              )
  38. /* This routine is used to initialize an array of class type.  If ptr is
  39.    NULL, it allocates the space for the array first.  Since the constructor
  40.    for the class may be of either memory model, and take an argument of any
  41.    memory model, we are forced to pass a mode parameter to tell us how to
  42.    cast it.  Since we must pass a near pointer for near functions and a
  43.    far call for far functions, we can't even know the argument type until
  44.    runtime, so we have to use varargs to get at it.  This version passes the
  45.    second argument needed for a class with virtual bases.
  46.  
  47.    The interpretation of the mode parameter is:
  48.     far function        0x01
  49.     pascal call        0x02
  50.     far pointer        0x04
  51. */
  52. {
  53.     va_list ap;        /* for access to parameters */
  54.     consvNNC np;    /* Near pointer version */
  55.     consvFNC fp;    /* Far pointer version */
  56.  
  57.     va_start(ap, mode);
  58.  
  59.     if (mode & 1)
  60.     fp = va_arg(ap, consvFNC);
  61.     else
  62.     np = va_arg(ap, consvNNC);
  63.  
  64.     if( ptr == 0 )
  65.     ptr = operator new(size * count);
  66.  
  67.     if( ptr != 0 ) {
  68.     for( char far* p = (char far*) ptr;  --count >= 0;  p += size )
  69.         switch (mode) {
  70.         case 0: np((void near *) p, 0); break;
  71.         case 1: fp((void near *) p, 0); break;
  72.         case 2: (*(consvNNP) np)((void near *) p, 0); break;
  73.         case 3: (*(consvFNP) fp)((void near *) p, 0); break;
  74.         case 4: (*(consvNFC) np)((void far  *) p, 0); break;
  75.         case 5: (*(consvFFC) fp)((void far  *) p, 0); break;
  76.         case 6: (*(consvNFP) np)((void far  *) p, 0); break;
  77.         default:(*(consvFFP) fp)((void far  *) p, 0); break;
  78.         }
  79.     }
  80.     return ptr;
  81. }
  82.