XDispatch 0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
base.h
Go to the documentation of this file.
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_BASE_H_
00024 #define XDISPATCH_BASE_H_
00025 
00031 #ifndef __XDISPATCH_INDIRECT__
00032 #error "Please #include <xdispatch/dispatch.h> instead of this file directly."
00033 #endif
00034 
00035 #include <string>
00036 
00037 __XDISPATCH_BEGIN_NAMESPACE
00038 
00048 class XDISPATCH_EXPORT operation
00049 {
00050     public:
00051         operation() : auto_del(true){}
00052         virtual ~operation(){}
00053 
00054         virtual void operator()() = 0;
00055 
00061         virtual void auto_delete(bool a){ auto_del = a; }
00066         virtual bool auto_delete() const { return auto_del; }
00067 
00068     private:
00069         bool auto_del;
00070 };
00071 
00079 class XDISPATCH_EXPORT iteration_operation
00080 {
00081     public:
00082         iteration_operation() : auto_del(true){}
00083         virtual ~iteration_operation(){}
00084 
00085         virtual void operator()(size_t index) = 0;
00091         virtual void auto_delete(bool a){ auto_del = a; }
00096         virtual bool auto_delete() const { return auto_del; }
00097 
00098     private:
00099         bool auto_del;
00100 };
00101 
00106 template <class T>  class ptr_operation : public operation
00107 {
00108     public:
00109         ptr_operation(T* object, void(T::*function)())
00110             : obj(object), func(function) {}
00111         virtual void operator()() {
00112             (*obj.*func)();
00113         }
00114 
00115     private:
00116         T* obj;
00117         void (T::*func)();
00118 };
00119 
00124 template <class T> class  ptr_iteration_operation : public iteration_operation
00125 {
00126     public:
00127         ptr_iteration_operation(T* object, void(T::*function)(size_t))
00128             : obj(object), func(function) {}
00129         virtual void operator()(size_t index) {
00130             (*obj.*func)(index);
00131         }
00132 
00133     private:
00134         T* obj;
00135         void (T::*func)(size_t);
00136 };
00137 
00138 
00139 #ifdef XDISPATCH_HAS_BLOCKS
00140 
00144 class block_operation : public operation {
00145     public:
00146         block_operation(dispatch_block_t b)
00147             : operation(), block(XDISPATCH_BLOCK_PERSIST(b)) {}
00148         block_operation(const block_operation& other)
00149             : operation(other), block(XDISPATCH_BLOCK_COPY(other.block)) {}
00150         ~block_operation() {
00151             XDISPATCH_BLOCK_DELETE(block);
00152         }
00153 
00154         void operator ()(){
00155             XDISPATCH_BLOCK_EXEC(block)();
00156         };
00157 
00158     private:
00159         dispatch_block_store block;
00160 };
00161 
00166 class block_iteration_operation : public iteration_operation {
00167     public:
00168         block_iteration_operation(dispatch_iteration_block_t b)
00169             : iteration_operation(), block(XDISPATCH_BLOCK_PERSIST(b)) {}
00170         block_iteration_operation(const block_iteration_operation& other)
00171             : iteration_operation(other), block(XDISPATCH_BLOCK_COPY(other.block)) {}
00172         ~block_iteration_operation() { XDISPATCH_BLOCK_DELETE(block); }
00173 
00174         void operator ()(size_t index){
00175             XDISPATCH_BLOCK_EXEC(block)(index);
00176         };
00177 
00178     private:
00179         dispatch_iteration_block_store block;
00180 };
00181 #endif
00182 
00183 
00184 class XDISPATCH_EXPORT queue;
00185 
00189 class XDISPATCH_EXPORT object {
00190 
00191     protected:
00192         object();
00193 
00194     public:
00195         virtual ~object();
00200         virtual void resume();
00210         virtual void suspend();
00217         virtual void target_queue(const queue&);
00222         virtual dispatch_object_t native() const = 0;
00223 
00224         bool operator ==(const object&);
00225         bool operator !=(const object&);
00226         bool operator ==(const dispatch_object_t&);
00227         bool operator !=(const dispatch_object_t&);
00228 };
00229 
00230 XDISPATCH_EXPORT bool operator ==(const dispatch_object_t&, const object&);
00231 XDISPATCH_EXPORT bool operator !=(const dispatch_object_t&, const object&);
00232 
00233 class queue;
00234 
00239 typedef dispatch_time_t time;
00244 static const time time_now = DISPATCH_TIME_NOW;
00249 static const time time_forever = DISPATCH_TIME_FOREVER;
00253 static const uint64_t nsec_per_sec = NSEC_PER_SEC;
00257 static const uint64_t nsec_per_msec = NSEC_PER_MSEC;
00261 static const uint64_t nsec_per_usec = NSEC_PER_USEC;
00265 static const uint64_t usec_per_sec = USEC_PER_SEC;
00270 enum queue_priority { HIGH =2, DEFAULT =1, LOW =0 };
00278 XDISPATCH_EXPORT queue main_queue();
00289 XDISPATCH_EXPORT queue global_queue(queue_priority p = DEFAULT);
00294 XDISPATCH_EXPORT queue current_queue();
00298 XDISPATCH_EXPORT time as_dispatch_time(struct tm*);
00302 inline dispatch_time_t as_native_dispatch_time(const time& t) {
00303     return t;
00304 }
00305 
00309 XDISPATCH_EXPORT struct tm as_struct_tm(const time& t);
00315 XDISPATCH_EXPORT time as_delayed_time(uint64_t delay, time base = time_now);
00326 XDISPATCH_EXPORT void exec();
00327 
00328 __XDISPATCH_END_NAMESPACE
00329 
00332 #endif /* XDISPATCH_BASE_H_ */

Generated on Tue Jun 12 2012 14:11:47 for XDispatch by Doxygen 1.8.0
© 2010-2012 MLBA (about | privacy) All Rights reserved.