Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef XDISPATCH_SOURCE_H_
00024 #define XDISPATCH_SOURCE_H_
00025
00026 #ifndef __XDISPATCH_INDIRECT__
00027 #error "Please #include <xdispatch/dispatch.h> instead of this file directly."
00028 #endif
00029
00030 #include <typeinfo>
00031
00032 __XDISPATCH_BEGIN_NAMESPACE
00033
00034 class source;
00035
00045 struct any {
00046
00047 any() : stored(0) {}
00048 template<typename Type> any(const Type& val) : stored(new holder<Type>(val)) {}
00049 any(const any& other) : stored(other.stored ? other.stored->clone() : 0 ) {}
00050 virtual ~any() { delete stored; }
00051
00052 any & swap(any & v)
00053 {
00054 std::swap(stored, v.stored);
00055 return *this;
00056 }
00057
00058 template<typename OtherType>
00059 any & operator=(const OtherType & v)
00060 {
00061 any(v).swap(*this);
00062 return *this;
00063 }
00064
00065 any & operator=(any v)
00066 {
00067 v.swap(*this);
00068 return *this;
00069 }
00070
00071 template<typename TargetType>
00072 TargetType cast() const {
00073 if(stored->type() == typeid(TargetType))
00074 return static_cast< holder<TargetType>* >(stored)->value;
00075 else
00076 throw std::bad_cast();
00077 }
00078
00079 private:
00080
00081 struct place_holder {
00082 virtual ~place_holder() {}
00083 virtual place_holder * clone() const = 0;
00084 virtual const std::type_info & type() const = 0;
00085 };
00086
00087 template <typename Type>
00088 struct holder : public place_holder {
00089 holder (const Type& val) : value(val) {}
00090
00091 virtual place_holder * clone() const {
00092 return new holder(value);
00093 }
00094
00095 virtual const std::type_info & type() const {
00096 return typeid(Type);
00097 }
00098
00099 Type value;
00100
00101 private:
00102 holder & operator=(const holder&);
00103 };
00104
00105 place_holder* stored;
00106 };
00107
00116 class XDISPATCH_EXPORT sourcetype {
00117
00118 protected:
00119 sourcetype();
00120 virtual ~sourcetype();
00121
00128 void ready(const any& = any());
00137 virtual dispatch_source_t native();
00138
00139 private:
00140 void set_cb(source*);
00141 friend class source;
00142 source* cb;
00143 };
00144
00152 class XDISPATCH_EXPORT source : public object {
00153
00154 public:
00158 source(sourcetype*);
00159 ~source();
00160
00161 void resume();
00162 void suspend();
00163
00170 void handler(xdispatch::operation*);
00171 #ifdef XDISPATCH_HAS_BLOCKS
00172
00178 void handler(dispatch_block_t);
00179 #endif
00180
00183 void target_queue(const xdispatch::queue&);
00187 xdispatch::queue target_queue() const;
00198 template <typename T> static T data(){
00199 return _data()->cast<T>();
00200 }
00201
00206 virtual dispatch_object_t native() const;
00207
00208 private:
00209 source(const source&);
00210 source& operator=(const source&);
00211 class pdata;
00212 pdata* d;
00213
00214 void notify(const any&);
00215 static const any* _data();
00216 friend class sourcetype;
00217 };
00218
00219 __XDISPATCH_END_NAMESPACE
00220
00221 #endif