hal_objects.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_objects.h
  15. * @brief Base object.
  16. * @details This header defines a base object that is the root for the
  17. * inheritance system.
  18. *
  19. * @addtogroup HAL_BASE_OBJECT
  20. * @details HAL uses concepts of Object Oriented Programming even if it
  21. * is written in C. Things like simple inheritance, multiple
  22. * inheritance and interfaces are used through the system.
  23. * This module defines a "base object" that is the ancestor of
  24. * all classes in the system.
  25. * @{
  26. */
  27. #ifndef HAL_OBJECTS_H
  28. #define HAL_OBJECTS_H
  29. /**
  30. * @brief @p BaseObject specific methods.
  31. * @note This object defines no methods.
  32. */
  33. #define _base_object_methods \
  34. /* Instance offset, used for multiple inheritance, normally zero. It
  35. represents the offset between the current object and the container
  36. object*/ \
  37. size_t instance_offset;
  38. /**
  39. * @brief @p BaseObject specific data.
  40. * @note This object defines no data.
  41. */
  42. #define _base_object_data
  43. /**
  44. * @brief @p BaseObject virtual methods table.
  45. */
  46. struct BaseObjectVMT {
  47. _base_object_methods
  48. };
  49. /**
  50. * @brief Base stream class.
  51. * @details This class represents a generic blocking unbuffered sequential
  52. * data stream.
  53. */
  54. typedef struct {
  55. /** @brief Virtual Methods Table.*/
  56. const struct BaseObjectVMT *vmt;
  57. _base_object_data
  58. } BaseObject;
  59. /**
  60. * @name Macro Functions (BaseObject)
  61. * @{
  62. */
  63. /**
  64. * @brief Returns the instance pointer starting from an interface pointer.
  65. *
  66. * @param[in] type the type of the instance pointer, it is used for casting
  67. * @param[in] ip the interface pointer
  68. * @return A pointer to the object implementing the interface
  69. */
  70. #define objGetInstance(type, ip) \
  71. (type)(((size_t)(ip)) - (ip)->vmt->instance_offset)
  72. /** @} */
  73. #endif /* HAL_OBJECTS_H */
  74. /** @} */