/src/wrappers/common/library/wrapper_handler.e

http://github.com/tybor/Liberty · Specman e · 101 lines · 80 code · 12 blank · 9 comment · 7 complexity · dae8112654366778dcded9be72ec6986 MD5 · raw file

  1. note
  2. description:
  3. "A class that can handle WRAPPER's internals"
  4. copyright:
  5. "[
  6. Copyright (C) 2005-2017: 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_HANDLER
  21. -- A class that can access some internal representation of a WRAPPER.
  22. insert
  23. ANY
  24. undefine is_equal, copy, out_in_tagged_out_memory, fill_tagged_out_memory
  25. end
  26. EXCEPTIONS
  27. export {} all
  28. undefine is_equal, copy, out_in_tagged_out_memory, fill_tagged_out_memory
  29. end
  30. feature {} -- Utility features
  31. null_or (a_wrapper: WRAPPER): POINTER
  32. -- The handle of `a_wrapper', or the default_pointer if
  33. -- `a_wrapper' is Void
  34. do
  35. if a_wrapper /= Void then
  36. Result := a_wrapper.handle
  37. end
  38. ensure
  39. definition: Result = default_pointer or else a_wrapper /= Void and then Result = a_wrapper.handle
  40. end
  41. null_or_string (a_string: ABSTRACT_STRING): POINTER
  42. -- A pointer to a memory area containing the content of `a_string' or
  43. -- default_pointer if `a_string' is Void. The memory area may be the
  44. -- internal buffer of `a_string' or a newly allocated one.
  45. do
  46. if a_string /= Void then
  47. Result := a_string.to_external
  48. else
  49. check
  50. Result.is_null
  51. end
  52. end
  53. ensure
  54. definition: Result = default_pointer or (a_string /= Void implies Result = a_string.to_external)
  55. end
  56. null_or_array (a_collection: WRAPPER_COLLECTION[WRAPPER]): POINTER
  57. -- A pointer to the contenct of `a_collection' or NULL (default_pointer) if `a_collection' is Void
  58. do
  59. if a_collection /= Void then
  60. Result := a_collection.as_c_array.to_pointer
  61. end
  62. ensure
  63. definition: a_collection = Void implies Result.is_null and a_collection /= Void implies Result.is_not_null
  64. end
  65. collection_to_c_array (a_collection: COLLECTION[WRAPPER]): FAST_ARRAY[POINTER]
  66. -- An array containing the pointers to the objects wrapped by `a_collection' wrappers.
  67. -- TODO: avoid creating a new array whenever possible.
  68. require
  69. a_collection /= Void
  70. not a_collection.is_empty
  71. local
  72. i: ITERATOR[WRAPPER]
  73. do
  74. create Result.with_capacity(a_collection.count)
  75. from
  76. i := a_collection.new_iterator
  77. i.start
  78. until
  79. i.is_off
  80. loop
  81. Result.add_last(i.item.handle)
  82. i.next
  83. end
  84. end
  85. feature {} -- Wrapper related exceptions
  86. pointer_to_unwrapped_deferred_object: STRING "A C function returned a pointer to an unwrapped object which is wrapped by a deferred class. It is not possible to create a correct wrapper."
  87. retrieved_object_mismatch: STRING "Retrieved_object_mismatch: the Eiffel wrapper associated with a pointer is not an actual wrapper for the object referred by that pointer "
  88. copying_an_uncopyable: STRING "Trying to copy an uncopyable wrapper: such objects are usually shortly lived"
  89. end -- class WRAPPER_HANDLER