home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / VNEWV.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.5 KB  |  101 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.  * NOTE: This is the old version used in TC++ 1.0 and BC++ 2.0
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1990, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21.  
  22. typedef void (near * consvNNC)(void near*, int);
  23. typedef void (near * consvNFC)(void far*, int);
  24. typedef void pascal (near * consvNNP)(void near*, int);
  25. typedef void pascal (near * consvNFP)(void far*, int);
  26. typedef void (far * consvFNC)(void near*, int);
  27. typedef void (far * consvFFC)(void far*, int);
  28. typedef void pascal (far * consvFNP)(void near*, int);
  29. typedef void pascal (far * consvFFP)(void far*, int);
  30.  
  31.  
  32. extern "C"
  33. void far * _vector_vnew_(void far* ptr, // address of array, 0 means allocate it
  34.                  size_t size,           // size of each object
  35.                  unsigned count,        // how many objects
  36.                  unsigned mode,         // actual type of constructor
  37.                  ...
  38.                  )
  39. /* This routine is used to initialize an array of class type.  If ptr is
  40.    NULL, it allocates the space for the array first.  Since the constructor
  41.    for the class may be of either memory model, and take an argument of any
  42.    memory model, we are forced to pass a mode parameter to tell us how to
  43.    cast it.  Since we must pass a near pointer for near functions and a
  44.    far call for far functions, we can't even know the argument type until
  45.    runtime, so we have to use varargs to get at it.  This version passes the
  46.    second argument needed for a class with virtual bases.
  47.  
  48.    The interpretation of the mode parameter is:
  49.        far function             0x01
  50.        pascal call              0x02
  51.        far pointer              0x04
  52.        stored count             0x10
  53. */
  54. {
  55.     va_list ap;         /* for access to parameters */
  56.     consvNNC np;        /* Near pointer version */
  57.     consvFNC fp;        /* Far pointer version */
  58.     int stored_count;
  59.  
  60.     va_start(ap, mode);
  61.  
  62.     if (mode & 1)
  63.         fp = va_arg(ap, consvFNC);
  64.     else
  65.         np = va_arg(ap, consvNNC);
  66.  
  67.     if( ptr == 0 )
  68.         {
  69.         stored_count = mode & 0x10;
  70.         if(stored_count)
  71.             ptr = operator new((size * count)+sizeof(count));
  72.         else
  73.             ptr = operator new(size * count);
  74.  
  75.         if (ptr == 0)
  76.             return 0;
  77.  
  78.         if (stored_count)
  79.             {
  80.             *(unsigned far *)ptr = count;
  81.             ptr = ((char far *)ptr + sizeof(count));
  82.             }
  83.         }
  84.  
  85.     mode &= 0x07;   // strip out all flags except call type
  86.  
  87.     for( char far* p = (char far*) ptr;  count-- > 0;  p += size )
  88.         switch (mode)
  89.             {
  90.             case 0: np((void near *) p, 0); break;
  91.             case 1: fp((void near *) p, 0); break;
  92.             case 2: (*(consvNNP) np)((void near *) p, 0); break;
  93.             case 3: (*(consvFNP) fp)((void near *) p, 0); break;
  94.             case 4: (*(consvNFC) np)((void far  *) p, 0); break;
  95.             case 5: (*(consvFFC) fp)((void far  *) p, 0); break;
  96.             case 6: (*(consvNFP) np)((void far  *) p, 0); break;
  97.             default:(*(consvFFP) fp)((void far  *) p, 0); break;
  98.             }
  99.     return ptr;
  100. }
  101.