/src/wrappers/common/library/pointer_handling.e

http://github.com/tybor/Liberty · Specman e · 81 lines · 61 code · 8 blank · 12 comment · 2 complexity · 66fd66f6b0e2978c2c98d9bae4cfcba1 MD5 · raw file

  1. note
  2. description:
  3. "Utility class to reference and dereference C pointers."
  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 POINTER_HANDLING
  21. insert
  22. ANY
  23. undefine copy, is_equal, fill_tagged_out_memory
  24. end
  25. feature {WRAPPER} -- Pointer referencing and de-referencing
  26. address_of (a_pointer: POINTER): POINTER
  27. external "C inline"
  28. alias "(& ($a_pointer))"
  29. end
  30. content_of (a_pointer: POINTER): POINTER
  31. -- The pointer referenced by `a_pointer' which has to be a
  32. -- pointer to a pointer (i.e.: void **). Note: the type
  33. -- cannot be checked by Eiffel AFAIK. Paolo 2006-05-08q.
  34. external "C inline"
  35. alias "(* ($a_pointer))"
  36. end
  37. feature {WRAPPER} -- Dealing with "char **" return types
  38. strings_array_from (a_pointer: POINTER): FAST_ARRAY[FIXED_STRING]
  39. -- Build a FAST_ARRAY of STRINGs from `a_pointer' which must be of a
  40. -- NULL-terminated array of C strings, that is a "char**" String
  41. -- contents and array itself are copied.
  42. local l: INTEGER_32; n: NATIVE_ARRAY[POINTER]
  43. do
  44. -- Find actual array size to avoid several reallocation of Result
  45. n := n.from_pointer(a_pointer) -- remember that NATIVE_ARRAY is an expanded type
  46. from l:=0 until n.item(l).is_null
  47. loop
  48. l:=l+1
  49. end
  50. create Result.make(l)
  51. -- Copying contents
  52. from l:=0 until l>Result.upper
  53. loop
  54. Result.put(create {FIXED_STRING}.from_external_copy(n.item(l)),l)
  55. l:=l+1
  56. end
  57. end
  58. sized_strings_array_from (a_pointer: POINTER; a_size: INTEGER_32): FAST_ARRAY[FIXED_STRING]
  59. -- Build a FAST_ARRAY of STRINGs from `a_pointer' which must be of a
  60. -- array of C strings of `a_size' elements. The actual C type must be
  61. -- "char**" String contents and array itself are copied.
  62. local i: INTEGER_32; n: NATIVE_ARRAY[POINTER]
  63. do
  64. create Result.make(a_size)
  65. -- Copying contents
  66. n := n.from_pointer (a_pointer) -- remember, NATIVE_ARRAY is expanded
  67. from i:=0 until i>Result.upper
  68. loop
  69. Result.put(create {FIXED_STRING}.from_external_copy(n.item(i)),i)
  70. i:=i+1
  71. end
  72. end
  73. end -- class POINTER_HANDLING