/src/wrappers/common/library/c_struct.e

http://github.com/tybor/Liberty · Specman e · 92 lines · 66 code · 11 blank · 15 comment · 6 complexity · 6576abbde61af04e61c4743c245ed760 MD5 · raw file

  1. note
  2. description: "Wrapper for a generic C structure"
  3. copyright:
  4. "[
  5. (C) 2005,2008 Paolo Redaelli, Raphael Mack <mail@raphael-mack.de>
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public License
  8. as published by the Free Software Foundation; either version 2.1 of
  9. the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301 USA
  18. ]"
  19. license: "LGPL v2 or later"
  20. deferred class C_STRUCT
  21. -- Wrapper for a data structure implemented in C programming language.
  22. inherit
  23. WRAPPER
  24. feature {} -- Initialization
  25. from_external_copy (other: POINTER)
  26. do
  27. --dispose
  28. if other.is_not_null then
  29. handle := malloc(struct_size)
  30. handle := memcpy(handle, other, struct_size)
  31. else
  32. handle := default_pointer
  33. end
  34. end
  35. allocate
  36. -- Allocate an uninitialized structure.
  37. -- Memory is allocated but NOT set to zero. This may not be what you want. See implementation
  38. do
  39. handle := malloc(struct_size)
  40. -- This feature used to invoke calloc to set the allocated memory to zero.
  41. -- calloc actually has different signatures on 32 and 64 bits machines.
  42. -- so we cannot write correct code as long as we rely on SmartEiffel or as long as we do not have (automatic) convertions in classes.
  43. -- Please note that initializing struct memory to zero may not what
  44. -- we want, for example I don't know if binary 0 also means zero
  45. -- when representing a floating point number. Paolo 2010-06-19
  46. if handle.is_null then
  47. raise_exception(No_more_memory)
  48. end
  49. ensure
  50. memory_allocated: handle.is_not_null
  51. end
  52. feature {ANY} -- Copying
  53. copy (other: like Current)
  54. do
  55. dispose
  56. if other.handle.is_not_null then
  57. handle := malloc(struct_size)
  58. handle := memcpy(handle, other.handle, struct_size)
  59. else
  60. handle := default_pointer
  61. end
  62. end
  63. is_equal (another: like Current): BOOLEAN
  64. do
  65. Result := Current.handle = another.handle
  66. end
  67. feature {}
  68. -- Access to C features
  69. -- struct_size should be exported to WRAPPER, to be able to check size
  70. -- before copying
  71. struct_size: like size_t
  72. deferred
  73. end
  74. feature {WRAPPER_HANDLER} -- Destroying
  75. free_handle
  76. -- release the external memory
  77. do
  78. free(handle)
  79. -- Note free(NULL) is a harmless non-operation.
  80. end
  81. end -- class C_STRUCT