/src/ftk_source.h
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/* 26 * History: 27 * ================================================================ 28 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created 29 * 30 */ 31#ifndef FTK_SOURCE_H 32#define FTK_SOURCE_H 33 34#include "ftk_log.h" 35#include "ftk_allocator.h" 36 37FTK_BEGIN_DECLS 38 39struct _FtkSource; 40typedef struct _FtkSource FtkSource; 41 42typedef int (*FtkSourceGetFd)(FtkSource* thiz); 43typedef int (*FtkSourceCheck)(FtkSource* thiz); 44typedef Ret (*FtkSourceDispatch)(FtkSource* thiz); 45typedef void (*FtkSourceDestroy)(FtkSource* thiz); 46 47struct _FtkSource 48{ 49 FtkSourceGetFd get_fd; 50 FtkSourceCheck check; 51 FtkSourceDispatch dispatch; 52 FtkSourceDestroy destroy; 53 54 int ref; 55 int active;/*If this source is managed by sources_manager.*/ 56 int disable; 57 char priv[ZERO_LEN_ARRAY]; 58}; 59 60static inline Ret ftk_source_disable(FtkSource* thiz) 61{ 62 return_val_if_fail(thiz != NULL, RET_FAIL); 63 64 thiz->disable++; 65 66 return RET_OK; 67} 68 69static inline Ret ftk_source_enable(FtkSource* thiz) 70{ 71 return_val_if_fail(thiz != NULL, RET_FAIL); 72 73 if(thiz->disable > 0) 74 { 75 thiz->disable--; 76 } 77 else 78 { 79 thiz->disable = 0; 80 } 81 82 return RET_OK; 83} 84 85static inline int ftk_source_get_fd(FtkSource* thiz) 86{ 87 return_val_if_fail(thiz != NULL && thiz->get_fd != NULL, -1); 88 89 return thiz->get_fd(thiz); 90} 91 92static inline int ftk_source_check(FtkSource* thiz) 93{ 94 return_val_if_fail(thiz != NULL && thiz->check != NULL, -1); 95 96 return thiz->check(thiz); 97} 98 99static inline Ret ftk_source_dispatch(FtkSource* thiz) 100{ 101 return_val_if_fail(thiz != NULL && thiz->dispatch != NULL, RET_FAIL); 102 103 return thiz->dispatch(thiz); 104} 105 106static inline void ftk_source_destroy(FtkSource* thiz) 107{ 108 if(thiz != NULL && thiz->destroy != NULL) 109 { 110 thiz->destroy(thiz); 111 } 112 113 return; 114} 115 116static inline void ftk_source_ref(FtkSource* thiz) 117{ 118 return_if_fail(thiz != NULL); 119 thiz->ref++; 120 121 return; 122} 123 124static inline void ftk_source_unref(FtkSource* thiz) 125{ 126 return_if_fail(thiz != NULL); 127 128 thiz->ref--; 129 if(thiz->ref == 0) 130 { 131 ftk_source_destroy(thiz); 132 } 133 134 return; 135} 136 137FTK_END_DECLS 138 139#endif/*FTK_SOURCE_H*/ 140