/src/wrappers/common/library/c_struct.e
Specman e | 92 lines | 66 code | 11 blank | 15 comment | 6 complexity | 6576abbde61af04e61c4743c245ed760 MD5 | raw file
1note 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 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 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA 20 ]" 21 license: "LGPL v2 or later" 22 23deferred class C_STRUCT 24 -- Wrapper for a data structure implemented in C programming language. 25 26inherit 27 WRAPPER 28 29feature {} -- Initialization 30 from_external_copy (other: POINTER) 31 do 32 --dispose 33 if other.is_not_null then 34 handle := malloc(struct_size) 35 handle := memcpy(handle, other, struct_size) 36 else 37 handle := default_pointer 38 end 39 end 40 41 allocate 42 -- Allocate an uninitialized structure. 43 -- Memory is allocated but NOT set to zero. This may not be what you want. See implementation 44 do 45 handle := malloc(struct_size) 46 -- This feature used to invoke calloc to set the allocated memory to zero. 47 -- calloc actually has different signatures on 32 and 64 bits machines. 48 -- 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. 49 -- Please note that initializing struct memory to zero may not what 50 -- we want, for example I don't know if binary 0 also means zero 51 -- when representing a floating point number. Paolo 2010-06-19 52 if handle.is_null then 53 raise_exception(No_more_memory) 54 end 55 ensure 56 memory_allocated: handle.is_not_null 57 end 58 59feature {ANY} -- Copying 60 copy (other: like Current) 61 do 62 dispose 63 if other.handle.is_not_null then 64 handle := malloc(struct_size) 65 handle := memcpy(handle, other.handle, struct_size) 66 else 67 handle := default_pointer 68 end 69 end 70 71 is_equal (another: like Current): BOOLEAN 72 do 73 Result := Current.handle = another.handle 74 end 75 76feature {} 77 -- Access to C features 78 -- struct_size should be exported to WRAPPER, to be able to check size 79 -- before copying 80 struct_size: like size_t 81 deferred 82 end 83 84feature {WRAPPER_HANDLER} -- Destroying 85 free_handle 86 -- release the external memory 87 do 88 free(handle) 89 -- Note free(NULL) is a harmless non-operation. 90 end 91 92end -- class C_STRUCT