00001 /* 00002 * Copyright (C) Sergey P. Derevyago, 2008. 00003 * 00004 * Permission to copy, use, modify, sell and distribute this software is granted 00005 * provided this copyright notice appears in all copies. 00006 * This software is provided "as is" without express or implied warranty, and 00007 * with no claim as to its suitability for any purpose. 00008 * 00009 */ 00010 00015 #include <ders/data_queue.hpp> 00016 #include <ders/destroy.hpp> 00017 00018 namespace { 00019 00020 using namespace ders; 00021 00022 class dq_impl : public data_queue { 00023 bool ir; 00024 sh_data_io dio; 00025 00026 public: 00027 dq_impl(sh_data_io d) : ir(false), dio(d) {} 00028 00029 virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); } 00030 virtual bool is_intr(); 00031 virtual void set_intr(bool val); 00032 virtual result read_obj(object_io& rdr, bool wait); 00033 virtual result write_obj(const object_io& wrtr); 00034 }; 00035 00036 bool dq_impl::is_intr() 00037 { 00038 return ir; 00039 } 00040 00041 void dq_impl::set_intr(bool val) 00042 { 00043 ir=val; 00044 } 00045 00046 data_queue::result dq_impl::read_obj(object_io& rdr, bool) 00047 { 00048 if (ir) return intr; 00049 return (rdr.read_from(*dio)) ? ok : empt; 00050 } 00051 00052 data_queue::result dq_impl::write_obj(const object_io& wrtr) 00053 { 00054 if (ir) return intr; 00055 00056 wrtr.write_to(*dio); 00057 return ok; 00058 } 00059 00060 } // unnamed 00061 00062 namespace ders { // ::ders 00063 00064 sh_data_queue new_data_queue(mem_pool& mp, int rsrv) 00065 { 00066 return new_data_queue(new_data_io(new_cycl_buf(mp, rsrv))); 00067 } 00068 00069 sh_data_queue new_data_queue(sh_data_io dio) 00070 { 00071 mp_newbuf<dq_impl> buf(dio.pool()); 00072 return sh_data_queue(buf.pool(), buf.rls(::new(buf.get()) dq_impl(dio))); 00073 } 00074 00075 } // namespace ::ders 00076