/src/ftk_source.h

http://ftk.googlecode.com/ · C Header · 140 lines · 82 code · 29 blank · 29 comment · 20 complexity · 476a65dd439a57c8359dfbf68b89b987 MD5 · raw file

  1. /*
  2. * File: ftk_source.h
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: event source interface
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #ifndef FTK_SOURCE_H
  31. #define FTK_SOURCE_H
  32. #include "ftk_log.h"
  33. #include "ftk_allocator.h"
  34. FTK_BEGIN_DECLS
  35. struct _FtkSource;
  36. typedef struct _FtkSource FtkSource;
  37. typedef int (*FtkSourceGetFd)(FtkSource* thiz);
  38. typedef int (*FtkSourceCheck)(FtkSource* thiz);
  39. typedef Ret (*FtkSourceDispatch)(FtkSource* thiz);
  40. typedef void (*FtkSourceDestroy)(FtkSource* thiz);
  41. struct _FtkSource
  42. {
  43. FtkSourceGetFd get_fd;
  44. FtkSourceCheck check;
  45. FtkSourceDispatch dispatch;
  46. FtkSourceDestroy destroy;
  47. int ref;
  48. int active;/*If this source is managed by sources_manager.*/
  49. int disable;
  50. char priv[ZERO_LEN_ARRAY];
  51. };
  52. static inline Ret ftk_source_disable(FtkSource* thiz)
  53. {
  54. return_val_if_fail(thiz != NULL, RET_FAIL);
  55. thiz->disable++;
  56. return RET_OK;
  57. }
  58. static inline Ret ftk_source_enable(FtkSource* thiz)
  59. {
  60. return_val_if_fail(thiz != NULL, RET_FAIL);
  61. if(thiz->disable > 0)
  62. {
  63. thiz->disable--;
  64. }
  65. else
  66. {
  67. thiz->disable = 0;
  68. }
  69. return RET_OK;
  70. }
  71. static inline int ftk_source_get_fd(FtkSource* thiz)
  72. {
  73. return_val_if_fail(thiz != NULL && thiz->get_fd != NULL, -1);
  74. return thiz->get_fd(thiz);
  75. }
  76. static inline int ftk_source_check(FtkSource* thiz)
  77. {
  78. return_val_if_fail(thiz != NULL && thiz->check != NULL, -1);
  79. return thiz->check(thiz);
  80. }
  81. static inline Ret ftk_source_dispatch(FtkSource* thiz)
  82. {
  83. return_val_if_fail(thiz != NULL && thiz->dispatch != NULL, RET_FAIL);
  84. return thiz->dispatch(thiz);
  85. }
  86. static inline void ftk_source_destroy(FtkSource* thiz)
  87. {
  88. if(thiz != NULL && thiz->destroy != NULL)
  89. {
  90. thiz->destroy(thiz);
  91. }
  92. return;
  93. }
  94. static inline void ftk_source_ref(FtkSource* thiz)
  95. {
  96. return_if_fail(thiz != NULL);
  97. thiz->ref++;
  98. return;
  99. }
  100. static inline void ftk_source_unref(FtkSource* thiz)
  101. {
  102. return_if_fail(thiz != NULL);
  103. thiz->ref--;
  104. if(thiz->ref == 0)
  105. {
  106. ftk_source_destroy(thiz);
  107. }
  108. return;
  109. }
  110. FTK_END_DECLS
  111. #endif/*FTK_SOURCE_H*/