PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1note
 2   description:
 3      "Utility class to reference and dereference C pointers."
 4   copyright:
 5      "[
 6               Copyright (C) 2005-2017: Paolo Redaelli
 7
 8               This library is free software; you can redistribute it and/or
 9               modify it under the terms of the GNU Lesser General Public License
10               as published by the Free Software Foundation; either version 2.1 of
11               the License, or (at your option) any later version.
12
13               This library is distributed in the hope that it will be useful, but
14               WITHOUT ANY WARRANTY; without even the implied warranty of
15               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16               Lesser General Public License for more details.
17
18               You should have received a copy of the GNU Lesser General Public
19               License along with this library; if not, write to the Free Software
20               Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21               02110-1301 USA
22         ]"
23deferred class POINTER_HANDLING
24
25insert
26   ANY
27      undefine copy, is_equal, fill_tagged_out_memory
28      end
29
30feature {WRAPPER} -- Pointer referencing and de-referencing
31   address_of (a_pointer: POINTER): POINTER
32      external "C inline"
33      alias "(& ($a_pointer))"
34      end
35
36   content_of (a_pointer: POINTER): POINTER
37         -- The pointer referenced by `a_pointer' which has to be a
38         -- pointer to a pointer (i.e.: void **). Note: the type
39         -- cannot be checked by Eiffel AFAIK. Paolo 2006-05-08q.
40      external "C inline"
41      alias "(* ($a_pointer))"
42      end
43
44feature {WRAPPER} -- Dealing with "char **" return types
45	strings_array_from (a_pointer: POINTER): FAST_ARRAY[FIXED_STRING]
46		-- Build a FAST_ARRAY of STRINGs from `a_pointer' which must be of a
47		-- NULL-terminated array of C strings, that is a "char**" String
48		-- contents and array itself are copied.
49	local l: INTEGER_32; n: NATIVE_ARRAY[POINTER]
50	do
51		-- Find actual array size to avoid several reallocation of Result
52		n := n.from_pointer(a_pointer) -- remember that NATIVE_ARRAY is an expanded type
53		from l:=0 until n.item(l).is_null
54		loop 
55			l:=l+1
56		end
57		create Result.make(l)
58		-- Copying contents
59		from l:=0 until l>Result.upper
60		loop
61			Result.put(create {FIXED_STRING}.from_external_copy(n.item(l)),l)
62			l:=l+1
63		end
64	end
65
66	sized_strings_array_from (a_pointer: POINTER; a_size: INTEGER_32): FAST_ARRAY[FIXED_STRING]
67		-- Build a FAST_ARRAY of STRINGs from `a_pointer' which must be of a
68		-- array of C strings of `a_size' elements. The actual C type must be
69		-- "char**" String contents and array itself are copied.
70	local i: INTEGER_32; n: NATIVE_ARRAY[POINTER]
71	do
72		create Result.make(a_size)
73		-- Copying contents
74		n := n.from_pointer (a_pointer) -- remember, NATIVE_ARRAY is expanded
75		from i:=0 until i>Result.upper
76		loop
77			Result.put(create {FIXED_STRING}.from_external_copy(n.item(i)),i)
78			i:=i+1
79		end
80	end
81end -- class POINTER_HANDLING