/src/wrappers/common/library/wrapper.e

http://github.com/tybor/Liberty · Specman e · 137 lines · 64 code · 13 blank · 60 comment · 3 complexity · e77571d195e9a10b7d1c0ff5fe85edd2 MD5 · raw file

  1. note
  2. description:
  3. "Deferred class handling low-level C interfaces. Its heirs will fully expose each other their internals."
  4. copyright:
  5. "[
  6. Copyright (C) 2005-2017: ,2008 Paolo Redaelli
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public License
  9. as published by the Free Software Foundation; either version 2.1 of
  10. the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. 02110-1301 USA
  19. ]"
  20. deferred class WRAPPER
  21. -- Deferred class handling low-level C interfaces. Its heirs will fully
  22. -- expose each other their internals.
  23. --
  24. -- No assumption is made on memory handling; to create a proper wrapper for
  25. -- it the developer shall inherit both from this class and from classes
  26. -- providing memory handling behaviour, depending on how structure's memory
  27. -- shall be handled. This is decided case-by-case by C library.
  28. --
  29. -- Currently available memory handling classes are:
  30. --
  31. -- C_OWNED: memory is always handled by the underlying C library.
  32. --
  33. -- EIFFEL_OWNED: once created memory is fully handled by Eiffel runtime,
  34. -- usually with the Garbage Collector.
  35. --
  36. -- GLOBALLY_CACHED: Until disposed by Eiffel the wrapper registered in
  37. -- wrappers dictionary will be the unique wrapper used on the Eiffel side.
  38. --
  39. -- MIXED_MEMORY_HANDLING: whose memory can either by handled by the Eiffel
  40. -- library or by the underlying C code. Who handles memory is decided on a
  41. -- per-object based on the value of the flag `is_shared': handle will not
  42. -- be freed on dispose of the Eiffel wrapper object, when `is_shared'
  43. -- true.
  44. --
  45. -- REFERENCE_COUNTED: memory is handled throught reference counting, i.e.
  46. -- GObject
  47. --
  48. -- Previous design was a black-or-white design either Eiffel-owned or not:
  49. -- C_STRUCT's are "owned" by the Eiffel code, and the Eiffel side should
  50. -- keep then longest lived reference to this struct. This allows us to
  51. -- forget about wrapping for this objects. If you have to share this
  52. -- struct and/or will have pointers to it around that will outlive the
  53. -- wrapper, please use SHARED_C_STRUCT
  54. inherit
  55. DISPOSABLE
  56. -- we really should redefine is_equal and copy for all wrapper classes
  57. -- WRAPPER conforms to ANY, because sometimes we need to get them via a
  58. -- POINTER.to_any operation
  59. undefine is_equal, copy
  60. end
  61. ANY
  62. -- we really should redefine is_equal and copy for all wrapper classes
  63. undefine is_equal, copy
  64. end
  65. insert
  66. WRAPPER_HANDLER
  67. -- to access `null_or', `exceptions' strings and external calls
  68. POINTER_HANDLING
  69. -- to access `address_of' and `content_of'
  70. STDLIB_EXTERNALS
  71. STRING_EXTERNALS
  72. STANDARD_C_LIBRARY_TYPES
  73. export
  74. {} all -- hide all underlying low-level code
  75. end
  76. feature {WRAPPER, WRAPPER_HANDLER} -- Implementation
  77. from_external_pointer (a_ptr: POINTER)
  78. do
  79. handle := a_ptr
  80. ensure
  81. handle = a_ptr
  82. end
  83. -- is_null: BOOLEAN
  84. -- do
  85. -- Result := handle.is_null
  86. -- ensure
  87. -- definition: Result = handle.is_null
  88. -- end
  89. -- is_not_null: BOOLEAN
  90. -- do
  91. -- Result := handle.is_not_null
  92. -- ensure
  93. -- definition: Result = handle.is_not_null
  94. -- end
  95. set_handle (a_ptr: POINTER)
  96. -- Set a non-null handle. Raises an No_more_memory exception
  97. -- if a_ptr.is_null. Use this, if you want to check the
  98. -- result of some external allocation function.
  99. do
  100. if a_ptr.is_null then
  101. raise_exception(No_more_memory)
  102. end
  103. handle := a_ptr
  104. ensure
  105. definition: handle = a_ptr
  106. not_null: handle.is_not_null
  107. end
  108. handle: POINTER
  109. -- Pointer to the underlying C "thing" (i.e. a struct)
  110. reference: POINTER
  111. -- The address of `handle'. Usuful to be passed to C
  112. -- functions that asks for pointer to pointer to struct
  113. -- (i.e. "GError **error")
  114. do
  115. Result := address_of(handle)
  116. end
  117. feature {WRAPPER} -- Integer and natural convertions
  118. -- This section is currently necessary as Liberty does not have automatic type convertions.
  119. integer_to_size_t (an_integer: INTEGER): like size_t
  120. -- Comodity feature to cast an integer into an actual size_t
  121. external "C inline"
  122. alias "((size_t) ($an_integer))"
  123. end
  124. end -- class WRAPPER