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 ~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 * clone() const = 0;
00083 virtual const std::type_info & type() const = 0;
00084 };
00085
00086 template <typename Type>
00087 struct holder : public place_holder {
00088 holder (const Type& val) : value(val) {}
00089
00090 virtual place_holder * clone() const {
00091 return new holder(value);
00092 }
00093
00094 virtual const std::type_info & type() const {
00095 return typeid(Type);
00096 }
00097
00098 Type value;
00099
00100 private:
00101 holder & operator=(const holder&);
00102 };
00103
00104 place_holder* stored;
00105 };
00106
00115 class XDISPATCH_EXPORT sourcetype {
00116
00117 protected:
00118 sourcetype();
00119
00126 void ready(const any& = any());
00135 virtual dispatch_source_t native();
00136
00137 private:
00138 void set_cb(source*);
00139 friend class source;
00140 source* cb;
00141 };
00142
00150 class XDISPATCH_EXPORT source : public object {
00151
00152 public:
00156 source(sourcetype*);
00157 ~source();
00158
00159 void resume();
00160 void suspend();
00161
00168 void handler(xdispatch::operation*);
00169 #ifdef XDISPATCH_HAS_BLOCKS
00170
00176 void handler(dispatch_block_t);
00177 #endif
00178
00181 void target_queue(const xdispatch::queue&);
00185 xdispatch::queue target_queue() const;
00196 template <typename T> static T data(){
00197 return _data()->cast<T>();
00198 }
00199
00204 virtual dispatch_object_t native() const;
00205
00206 private:
00207 source(const source&);
00208 source& operator=(const source&);
00209 class pdata;
00210 pdata* d;
00211
00212 void notify(const any&);
00213 static const any* _data();
00214 friend class sourcetype;
00215 };
00216
00217 __XDISPATCH_END_NAMESPACE
00218
00219 #endif