_carray.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2005
  3. * Francois Dumont
  4. *
  5. * This material is provided "as is", with absolutely no warranty expressed
  6. * or implied. Any use is at your own risk.
  7. *
  8. * Permission to use or copy this software for any purpose is hereby granted
  9. * without fee, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /* NOTE: This is an internal header file, included by other STL headers.
  16. * You should not attempt to use it directly.
  17. */
  18. #ifndef _STLP_CARRAY_H
  19. #define _STLP_CARRAY_H
  20. /* Purpose: Mimic a pur C array with the additionnal feature of
  21. * being able to be used with type not default constructible.
  22. */
  23. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  24. # include <stl/_construct.h>
  25. #endif
  26. _STLP_BEGIN_NAMESPACE
  27. _STLP_MOVE_TO_PRIV_NAMESPACE
  28. template <class _Tp, size_t _Nb>
  29. struct _CArray {
  30. _CArray (const _Tp& __val) {
  31. for (size_t __i = 0; __i < _Nb; ++__i) {
  32. _Copy_Construct(__REINTERPRET_CAST(_Tp*, _M_data + __i * sizeof(_Tp)), __val);
  33. }
  34. }
  35. ~_CArray() {
  36. _Destroy_Range(__REINTERPRET_CAST(_Tp*, _M_data + 0),
  37. __REINTERPRET_CAST(_Tp*, _M_data + _Nb * sizeof(_Tp)));
  38. }
  39. _Tp& operator [] (size_t __i) {
  40. _STLP_ASSERT(__i < _Nb)
  41. return *__REINTERPRET_CAST(_Tp*, _M_data + __i * sizeof(_Tp));
  42. }
  43. private:
  44. char _M_data[sizeof(_Tp) * _Nb];
  45. };
  46. _STLP_MOVE_TO_STD_NAMESPACE
  47. _STLP_END_NAMESPACE
  48. #endif //_STLP_CARRAY_H