00001 00002 /* 00003 * Copyright (c) 2011-2012 MLBA-Team. All rights reserved. 00004 * 00005 * @MLBA_OPEN_LICENSE_HEADER_START@ 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * @MLBA_OPEN_LICENSE_HEADER_END@ 00020 */ 00021 00022 00023 #ifndef XDISPATCH_SOURCE_H_ 00024 #define XDISPATCH_SOURCE_H_ 00025 00031 #ifndef __XDISPATCH_INDIRECT__ 00032 #error "Please #include <xdispatch/dispatch.h> instead of this file directly." 00033 #endif 00034 00035 #include "any.h" 00036 00037 __XDISPATCH_BEGIN_NAMESPACE 00038 00039 class source; 00040 00041 00050 class XDISPATCH_EXPORT sourcetype { 00051 00052 public: 00053 virtual ~sourcetype(); 00054 00055 protected: 00056 sourcetype(); 00057 00064 void ready(const any& = any()); 00073 virtual dispatch_source_t native(); 00082 virtual void on_cancel(); 00092 virtual void on_resume(); 00102 virtual void on_suspend(); 00116 virtual bool propagate_targetqueue() const { return false; } 00117 00118 private: 00119 sourcetype(const sourcetype&){} 00120 void set_cb(source*); 00121 friend class source; 00122 source* cb; 00123 }; 00124 00125 00126 class native_source_wrapper; 00127 00137 class XDISPATCH_EXPORT native_source : public sourcetype { 00138 00139 public: 00140 native_source(dispatch_source_t); 00141 ~native_source(); 00142 00143 protected: 00144 virtual dispatch_source_t native(); 00145 void on_resume (); 00146 void on_suspend (); 00147 void on_cancel (); 00148 bool propagate_targetqueue () const; 00149 00155 virtual void on_source_ready(); 00156 00157 private: 00158 pointer<native_source_wrapper>::shared _source; 00159 }; 00160 00161 00176 class XDISPATCH_EXPORT source : public object { 00177 00178 public: 00182 source(sourcetype*); 00183 ~source(); 00184 00185 void resume(); 00186 void suspend(); 00187 00194 void handler(xdispatch::operation*); 00195 #ifdef XDISPATCH_HAS_BLOCKS 00196 00202 virtual inline void handler(dispatch_block_t b) { 00203 handler( new block_operation(b) ); 00204 } 00205 #endif 00206 00209 void target_queue(const xdispatch::queue&); 00213 xdispatch::queue target_queue() const; 00226 template <typename T> static T data(){ 00227 return _data()->cast<T>(); 00228 } 00229 00235 virtual dispatch_object_t native() const; 00241 dispatch_source_t native_source() const; 00264 void cancel(); 00273 void cancel_handler(xdispatch::operation*); 00274 #ifdef XDISPATCH_HAS_BLOCKS 00275 00283 virtual inline void cancel_handler(dispatch_block_t b) { 00284 cancel_handler( new block_operation(b) ); 00285 } 00286 #endif 00287 00288 private: 00289 source(const source&); 00290 source& operator=(const source&); 00291 class pdata; 00292 pointer<pdata>::shared d; 00293 00294 void notify(const any&); 00295 sourcetype* source_type(); 00296 static const any* _data(); 00297 friend class sourcetype; 00298 friend class timer; 00299 }; 00300 00301 __XDISPATCH_END_NAMESPACE 00302 00305 #endif /* XDISPATCH_SOURCE_H_ */