XDispatch 0.6.1
source.h
Go to the documentation of this file.
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_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 /* XDISPATCH_SOURCE_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Thu Apr 12 2012 09:50:43 for XDispatch by Doxygen 1.7.4
© 2010-2012 MLBA (about | privacy) All Rights reserved.