libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/graph/node_param.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) Juan Pedro Bolivar Puente 2008                          *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_OBJPARAM
00024 #define PSYNTH_OBJPARAM
00025 
00026 #include <mutex>
00027 
00028 #include <boost/function.hpp>
00029 #include <string>
00030 
00031 namespace psynth
00032 {
00033 namespace graph
00034 {
00035 
00036 class node_param
00037 {
00038 public:
00039     typedef boost::function <void (node_param&)> event;
00040 
00041     enum type
00042     {
00043         NONE = -1,
00044         INT,       /* int */
00045         FLOAT,     /* float */
00046         STRING,    /* std::string */
00047         VECTOR2F,  /* Vector2f */
00048         TYPES
00049     };
00050 
00051     node_param () :
00052         m_type(NONE),
00053         m_changed(false),
00054         m_src(NULL),
00055         m_dest(NULL)
00056         {}
00057 
00058     node_param (const node_param& obj) :
00059         m_type(NONE),
00060         m_changed(false) {
00061         configure(obj.m_id, obj.m_name, obj.m_type, obj.m_dest, obj.m_event);
00062     }
00063     
00064     ~node_param () {
00065         clear();
00066     }
00067 
00068     node_param& operator= (const node_param& obj) {
00069         if (this != &obj)
00070             configure(obj.m_id, obj.m_name, obj.m_type, obj.m_dest, obj.m_event);
00071 
00072         return *this;
00073     }
00074     
00075     int get_id () const {
00076         return m_id;
00077     }
00078 
00079     const std::string& get_name () const {
00080         return m_name;
00081     }
00082         
00083     int type() const {
00084         return m_type;
00085     };
00086         
00087     template <typename T>
00088     void set (const T& d) {
00089         {
00090             std::unique_lock<std::mutex> lock (m_mutex);
00091             m_changed = true;
00092             *static_cast<T*>(m_src) = d;
00093         }
00094         if (!m_event.empty()) m_event(*this);
00095     }
00096 
00097     template <typename T>
00098     void get (T& d) const {
00099         m_mutex.lock ();
00100         d = *static_cast<T*>(m_src);
00101         m_mutex.unlock ();
00102     }
00103 
00104 private:
00105     friend class node;
00106         
00107     mutable std::mutex m_mutex;
00108 
00109     std::string m_name;
00110     int m_id;
00111     int m_type;
00112     bool m_changed;
00113     event m_event;
00114     void* m_src;
00115     void* m_dest;
00116     
00117     void clear ();
00118     void configure (int id, std::string name, int type, void* dest);
00119     void configure (int id, std::string name, int type, void* dest, event ev);
00120 
00121     void update_in ();
00122     void update_out ();
00123 };
00124 
00125 } /* namespace graph */
00126 } /* namespace psynth */
00127 
00128 #endif /* PSYNTH_OBJPARAM */