/src/wrappers/common/library/wrapper_factory.e

http://github.com/tybor/Liberty · Specman e · 78 lines · 39 code · 7 blank · 32 comment · 3 complexity · 7430b04258c95cc35f11c33f146ada82 MD5 · raw file

  1. note
  2. copyright:
  3. "[
  4. Copyright (C) 2008-2017: Paolo Redaelli
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. deferred class WRAPPER_FACTORY[ITEM_ -> WRAPPER]
  19. -- A wrapper factory returns the "fittest" wrapper object given a
  20. -- pointer to a wrapped "thing".
  21. -- A wrapper factory would implement the typing policy described in the
  22. -- wrapper library documentation. In fact some libraries implement their
  23. -- own type system like GObject, while other having smaller scopes will
  24. -- describe the types of each returned pointer, sometime being polymorphic.
  25. -- If the developer needs to create wrappers of several different types and
  26. -- those types are pre-defined, known during the development of the
  27. -- wrapper, for example when the actual returned type is explained in the
  28. -- documentation of the library it is sometimes better to use (as a client)
  29. -- one of its expanded variant, i.e. G_OBJECT_EXPANDED_FACTORY.
  30. inherit
  31. WRAPPER_HANDLER
  32. feature {WRAPPER, WRAPPER_HANDLER} -- Implementation
  33. wrapper (a_pointer: POINTER): ITEM_
  34. -- The wrapper for `a_pointer'. It could be newly created or
  35. -- retrieved from a cache, a dictionary, from the underlying
  36. -- object, depending on the implementation.
  37. -- See also wrapper_or_void: when `a_pointer' is the
  38. -- default pointer (known as NULL in C) Result will be Void.
  39. require
  40. pointer_not_null: a_pointer.is_not_null
  41. deferred
  42. ensure
  43. non_void: Result /= Void
  44. correct: Result.handle = a_pointer
  45. end
  46. wrapper_or_void (a_pointer: POINTER): ITEM_
  47. -- A wrapper for `a_pointer' or Void if `a_pointer'
  48. -- default_pointer (NULL in C). A commodity feature to
  49. -- replace the following code snippet:
  50. -- my_gobject: A_WRAPPER
  51. -- local p: POINTER
  52. -- do
  53. -- p := get_foo(handle)
  54. -- if p.is_not_null then
  55. -- Result := factory.wrapper(p)
  56. -- end
  57. -- end
  58. -- with
  59. -- my_gobject: A_G_OBJECT_HEIR
  60. -- do
  61. -- Result := factory.wrapper_or_void(get_foo(handle))
  62. -- end
  63. do
  64. if a_pointer.is_not_null then
  65. Result := wrapper(a_pointer)
  66. end
  67. ensure
  68. null_pointer_returns_void: a_pointer.is_null implies Result = Void
  69. correct: a_pointer.is_not_null implies Result /= Void and then Result.handle = a_pointer
  70. end
  71. end -- class WRAPPER_FACTORY