XDispatch 0.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
operation.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_OPERATION_H_
00024 #define XDISPATCH_OPERATION_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 
00128         ptr_iteration_operation(T* object, void(T::*function)(size_t))
00129             : obj(object), func(function) {}
00130         virtual void operator()(size_t index) {
00131             (*obj.*func)(index);
00132         }
00133 
00134     private:
00135         T* obj;
00136         void (T::*func)(size_t);
00137 };
00138 
00139 
00140 #if XDISPATCH_HAS_BLOCKS
00141 
00145 class block_operation : public operation {
00146     public:
00147         block_operation(dispatch_block_t b)
00148             : operation(), _block(Block_copy(b)) {}
00149         block_operation(const block_operation& other)
00150             : operation(other), _block(Block_copy(other._block)) {}
00151         ~block_operation() {
00152             Block_release(_block);
00153         }
00154 
00155         void operator ()(){
00156             _block();
00157         };
00158 
00159     private:
00160         dispatch_block_t _block;
00161 };
00162 
00167 class block_iteration_operation : public iteration_operation {
00168     public:
00169         block_iteration_operation(dispatch_iteration_block_t b)
00170             : iteration_operation(), _block(Block_copy(b)) {}
00171         block_iteration_operation(const block_iteration_operation& other)
00172             : iteration_operation(other), _block(Block_copy(other._block)) {}
00173         ~block_iteration_operation() { Block_release(_block); }
00174 
00175         void operator ()(size_t index){
00176             _block(index);
00177         };
00178 
00179     private:
00180         dispatch_iteration_block_t _block;
00181 };
00182 #endif // XDISPATCH_HAS_BLOCKS
00183 
00184 #if XDISPATCH_HAS_FUNCTION
00185 
00189 class function_operation : public operation {
00190     public:
00191         function_operation(const lambda_function& b)
00192             : operation(), _function(b) {}
00193         function_operation(const function_operation& other)
00194             : operation(other), _function(other._function) {}
00195         ~function_operation() {}
00196 
00197         void operator ()(){
00198            _function();
00199         };
00200 
00201     private:
00202         lambda_function _function;
00203 };
00204 
00209 class function_iteration_operation : public iteration_operation {
00210     public:
00211         function_iteration_operation(const iteration_lambda_function b)
00212             : iteration_operation(), _function(b) {}
00213         function_iteration_operation(const function_iteration_operation& other)
00214             : iteration_operation(other), _function(other._function) {}
00215         ~function_iteration_operation() {}
00216 
00217         void operator ()(size_t index){
00218             _function(index);
00219         };
00220 
00221     private:
00222         iteration_lambda_function _function;
00223 };
00224 #endif // XDISPATCH_HAS_FUNCTION
00225 
00226 __XDISPATCH_END_NAMESPACE
00227 
00230 #endif /* XDISPATCH_OPERATION_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Sun Oct 28 2012 22:12:47 for XDispatch by Doxygen 1.7.6.1
© 2010-2012 MLBA (about | privacy) All Rights reserved.