elements.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. // Collection implementation helpers
  11. /*
  12. * The short story:
  13. * this file contains inline functions that make up the building blocks
  14. * for implementing the string versions of standard parameterized
  15. * collection shapes
  16. *
  17. * The long story:
  18. * Because the implementation of collection classes moves objects around
  19. * in various ways, it is very inefficient to use only generic C++ constructs.
  20. * For example, in order to grow an array of FOO objects by one element,
  21. * you would be forced to allocate a new array of appropriate size, calling
  22. * the FOO constructor on every element. Then copy the original array, element
  23. * by element using a possibly overloaded assignment operator. Finally destroy
  24. * the original array element by element.
  25. * For built-in data types (WORD, DWORD, pointer types), this is complete
  26. * overkill. For non-trivial classes (eg: CString in particular) this is
  27. * a terrible implementation.
  28. *
  29. * The bottom line: we have two special routines for doing construction
  30. * and destruction of arrays of special elements - in particular CStrings.
  31. * The standard templates are parameterized on 'HAS_CREATE' which is
  32. * non-zero if the collection implementation requires a special
  33. * construct and destruct function.
  34. *
  35. * Please note that these are inline overloaded operators, and do not have
  36. * any form of runtime polymorphism (i.e. nothing is 'virtual').
  37. */
  38. /////////////////////////////////////////////////////////////////////////////
  39. // Special implementations for CStrings
  40. // it is faster to bit-wise copy a CString than to call an official
  41. // constructor - since an empty CString can be bit-wise copied
  42. static inline void ConstructElement(CString* pNewData)
  43. {
  44. memcpy(pNewData, &afxEmptyString, sizeof(CString));
  45. }
  46. static inline void DestructElement(CString* pOldData)
  47. {
  48. pOldData->~CString();
  49. }
  50. static inline void CopyElement(CString* pSrc, CString* pDest)
  51. {
  52. *pSrc = *pDest;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////