00001 00002 /* 00003 * Copyright (c) 2011 MLBA. 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_BASE_H_ 00024 #define XDISPATCH_BASE_H_ 00025 00026 #ifndef __XDISPATCH_INDIRECT__ 00027 #error "Please #include <xdispatch/dispatch.h> instead of this file directly." 00028 #endif 00029 00030 #include <string> 00031 00032 __XDISPATCH_BEGIN_NAMESPACE 00033 00043 class XDISPATCH_EXPORT operation 00044 { 00045 public: 00046 operation() : auto_del(true){} 00047 virtual ~operation(){} 00048 00049 virtual void operator()() = 0; 00050 00056 virtual void auto_delete(bool a){ auto_del = a; } 00061 virtual bool auto_delete(){ return auto_del; } 00062 00063 private: 00064 bool auto_del; 00065 }; 00066 00074 class XDISPATCH_EXPORT iteration_operation 00075 { 00076 public: 00077 iteration_operation() : auto_del(true){} 00078 virtual ~iteration_operation(){} 00079 00080 virtual void operator()(size_t index) = 0; 00086 virtual void auto_delete(bool a){ auto_del = a; } 00091 virtual bool auto_delete(){ return auto_del; } 00092 00093 private: 00094 bool auto_del; 00095 }; 00096 00101 template <class T> class ptr_operation : public operation 00102 { 00103 public: 00104 ptr_operation(T* object, void(T::*function)()) 00105 : obj(object), func(function) {} 00106 virtual void operator()() { 00107 (*obj.*func)(); 00108 } 00109 00110 private: 00111 T* obj; 00112 void (T::*func)(); 00113 }; 00114 00119 template <class T> class ptr_iteration_operation : public iteration_operation 00120 { 00121 public: 00122 ptr_iteration_operation(T* object, void(T::*function)(size_t)) 00123 : obj(object), func(function) {} 00124 virtual void operator()(size_t index) { 00125 (*obj.*func)(index); 00126 } 00127 00128 private: 00129 T* obj; 00130 void (T::*func)(size_t); 00131 }; 00132 00133 00134 #ifdef XDISPATCH_HAS_BLOCKS 00135 00139 class block_operation : public operation { 00140 public: 00141 block_operation(dispatch_block_t b) : block(XDISPATCH_BLOCK_PERSIST(b)) {} 00142 block_operation(const block_operation& other) : block(XDISPATCH_BLOCK_COPY(other.block)) {} 00143 ~block_operation() { 00144 XDISPATCH_BLOCK_DELETE(block); 00145 } 00146 00147 void operator ()(){ 00148 XDISPATCH_BLOCK_EXEC(block)(); 00149 }; 00150 00151 private: 00152 dispatch_block_store block; 00153 }; 00154 #endif 00155 00159 class XDISPATCH_EXPORT object { 00160 00161 public: 00166 virtual void resume() = 0; 00176 virtual void suspend() = 0; 00181 virtual dispatch_object_t native() const = 0; 00182 00183 bool operator ==(const object&); 00184 bool operator !=(const object&); 00185 bool operator ==(const dispatch_object_t&); 00186 bool operator !=(const dispatch_object_t&); 00187 }; 00188 00189 XDISPATCH_EXPORT bool operator ==(const dispatch_object_t&, const object&); 00190 XDISPATCH_EXPORT bool operator !=(const dispatch_object_t&, const object&); 00191 00192 class queue; 00193 00198 enum queue_priority { HIGH =2, DEFAULT =1, LOW =0 }; 00206 XDISPATCH_EXPORT queue main_queue(); 00217 XDISPATCH_EXPORT queue global_queue(queue_priority p = DEFAULT); 00222 XDISPATCH_EXPORT queue current_queue(); 00226 XDISPATCH_EXPORT dispatch_time_t as_dispatch_time(struct tm*); 00230 XDISPATCH_EXPORT struct tm as_struct_tm(dispatch_time_t t); 00236 XDISPATCH_EXPORT dispatch_time_t as_delayed_time(uint64_t delay, dispatch_time_t base = DISPATCH_TIME_NOW); 00243 XDISPATCH_EXPORT void exec(); 00244 00245 __XDISPATCH_END_NAMESPACE 00246 00247 #endif /* XDISPATCH_BASE_H_ */