libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/new_graph/triple_buffer.hpp
Go to the documentation of this file.
00001 
00011 /*
00012  *  Copyright (C) 2011 Juan Pedro BolĂ­var Puente
00013  *
00014  *  This file is part of Psychosynth.
00015  *   
00016  *  Psychosynth is free software: you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License as published by
00018  *  the Free Software Foundation, either version 3 of the License, or
00019  *  (at your option) any later version.
00020  *
00021  *  Psychosynth is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  You should have received a copy of the GNU General Public License
00027  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028  *
00029  */
00030 
00031 #ifndef PSYNTH_GRAPH_TRIPLE_BUFFER_HPP_
00032 #define PSYNTH_GRAPH_TRIPLE_BUFFER_HPP_
00033 
00034 #include <cassert>
00035 #include <tuple>
00036 #include <psynth/base/util.hpp>
00037 
00038 namespace psynth
00039 {
00040 namespace graph
00041 {
00042 
00043 template <class Buffer, class GuardPolicy>
00044 class double_buffer : private boost::noncopyable
00045 {
00046 public:
00047     typedef Buffer buffer_type;
00048         
00049     double_buffer ();
00050     double_buffer (const Buffer&);
00051     double_buffer (Buffer&&, Buffer&&);
00052     
00053     GuardPolicy& back_policy () const
00054     { return _back_lock; };
00055     
00056     buffer_type& front ()
00057     { return *_front; }
00058     
00059     buffer_type& back ()
00060     { return *_back; }
00061 
00062     const buffer_type& front () const
00063     { return *_front; }
00064     
00065     const buffer_type& back () const
00066     { return *_back; }
00067 
00068     bool flip_back ();
00069     
00070 protected:
00071     std::tuple<buffer_type, buffer_type> _storage;
00072 
00073     buffer_type* _front;
00074     buffer_type* _back;
00075     mutable GuardPolicy  _back_lock;
00076 };
00077 
00078 
00079 template <class Buffer, class BackGuardPolicy, class LocalGuardPolicy>
00080 class triple_buffer : public double_buffer<Buffer, BackGuardPolicy>
00081 {
00082 public:
00083     typedef std::unique_lock<std::mutex> guard;
00084     typedef Buffer buffer_type;
00085 
00086     triple_buffer ();
00087     triple_buffer (const Buffer&);
00088     triple_buffer (Buffer&&, Buffer&&, Buffer&&);
00089 
00090     LocalGuardPolicy& local_policy () const
00091     { return _local_lock; };
00092     
00093     bool flip_local ();
00094     
00095     buffer_type& local ()
00096     { return *_local; }
00097 
00098     const buffer_type& local () const
00099     { return *_local; }
00100 
00101 protected:
00102     buffer_type  _local_storage;
00103     buffer_type* _local;
00104     mutable LocalGuardPolicy  _local_lock;
00105 };
00106 
00107 } /* namespace graph */
00108 } /* namespace psynth */
00109 
00110 #include <psynth/new_graph/triple_buffer.tpp>
00111 
00112 #endif /* PSYNTH_GRAPH_TRIPLE_BUFFER_HPP_ */