more updates
[synfig.git] / synfig-core / trunk / src / synfig / node.h
1 /* === S I N F G =========================================================== */
2 /*!     \file node.h
3 **      \brief Template Header
4 **
5 **      $Id: node.h,v 1.3 2005/01/10 07:40:26 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SINFG_PARENTNODE_H
25 #define __SINFG_PARENTNODE_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <sigc++/signal.h>
30 #include <set>
31 #include "time.h"
32 #include "guid.h"
33 #include <ETL/handle>
34 #include "interpolation.h"
35 #include "mutex.h"
36
37 /* === M A C R O S ========================================================= */
38
39 /* === T Y P E D E F S ===================================================== */
40
41 /* === C L A S S E S & S T R U C T S ======================================= */
42
43 namespace sinfg {
44
45 class TimePoint
46 {
47         GUID guid;
48         Time time;
49         Interpolation before,after;
50 public:
51
52         TimePoint(const Time& x=Time::begin()):
53                 guid(0),
54                 time(x),
55                 before(INTERPOLATION_NIL),
56                 after(INTERPOLATION_NIL)
57         {
58         }
59
60         const GUID& get_guid()const { return guid; }
61         const Time& get_time()const { return time; }
62         Interpolation get_before()const { return before; }
63         Interpolation get_after()const { return after; }
64
65         void set_guid(const GUID& x) { guid=x; }
66         void set_time(const Time& x) { time=x; }
67         void set_before(Interpolation x) { before=x; }
68         void set_after(Interpolation x) { after=x; }
69         
70         void absorb(const TimePoint& x);
71 }; // END of class TimePoint
72
73 inline TimePoint operator+(TimePoint lhs,const Time& rhs)
74         { lhs.set_time(lhs.get_time()+rhs); return lhs; }
75
76 inline bool operator<(const TimePoint& lhs,const TimePoint& rhs)
77         { return lhs.get_time()<rhs.get_time(); }
78
79 inline bool operator<(const TimePoint& lhs,const Time& rhs)
80         { return lhs.get_time()<rhs; }
81
82 inline bool operator<(const Time& lhs,const TimePoint& rhs)
83         { return lhs<rhs.get_time(); }
84
85 inline bool operator==(const TimePoint& lhs,const TimePoint& rhs)
86         { return lhs.get_time()==rhs.get_time(); }
87
88 inline bool operator!=(const TimePoint& lhs,const TimePoint& rhs)
89         { return lhs.get_time()!=rhs.get_time(); }
90
91 class TimePointSet : public std::set<TimePoint>
92 {
93 public:
94         iterator insert(const TimePoint& x);
95
96         template <typename ITER> void insert(ITER begin, ITER end)
97                 { for(;begin!=end;++begin) insert(*begin); }
98
99 }; // END of class TimePointSet
100         
101 class Node : public etl::rshared_object
102 {
103         /*
104  --     ** -- T Y P E S -----------------------------------------------------------
105         */
106
107 public: 
108         
109         //! \writeme
110         typedef TimePointSet    time_set;
111
112         /*
113  --     ** -- D A T A -------------------------------------------------------------
114         */
115
116 private:
117
118         //! \writeme
119         GUID guid_;
120
121         //! cached time values for all the childrens
122         mutable time_set        times;
123
124         //! \writeme
125         mutable bool            bchanged;
126
127         //! \writeme
128         mutable int time_last_changed_;
129
130         //! \writeme
131         mutable RWLock rw_lock_;
132         
133         //! \writeme
134         bool deleting_;
135
136 public:
137
138         //! \todo This should really be private
139         std::set<Node*>         parent_set;
140
141         /*
142  -- ** -- S I G N A L S -------------------------------------------------------
143         */
144
145 private:
146         
147         sigc::signal<void> signal_changed_;
148
149         //!     GUID Changed
150         /*! \note The second parameter is the *OLD* guid! */
151         sigc::signal<void,GUID> signal_guid_changed_;   
152
153         //!     Deleted
154         sigc::signal<void> signal_deleted_;     
155
156         /*
157  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
158         */
159
160 public:
161
162         sigc::signal<void>& signal_deleted() { return signal_deleted_; }
163
164         sigc::signal<void>& signal_changed() { return signal_changed_; }
165
166         //!     GUID Changed
167         /*! \note The second parameter is the *OLD* guid! */
168         sigc::signal<void,GUID>& signal_guid_changed() { return signal_guid_changed_; }
169
170         /*
171  --     ** -- C O N S T R U C T O R S ---------------------------------------------
172         */
173
174 protected:
175
176         Node();
177
178         // This class cannot be copied -- use clone() if necessary
179 private:
180         Node(const Node &x);
181
182 public:
183         virtual ~Node();
184
185         /*
186  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
187         */
188         
189 public:
190
191         void changed();
192
193         //! Gets the GUID for this value node
194         const GUID& get_guid()const;
195
196         //! Sets the GUID for this value node
197         void set_guid(const GUID& x);
198
199         int get_time_last_changed()const;
200         
201         void add_child(Node*x);
202
203         void remove_child(Node*x);
204
205         int parent_count()const;
206         
207         const time_set &get_times() const;
208
209         RWLock& get_rw_lock()const { return rw_lock_; }
210         
211 protected:
212         
213         void begin_delete();
214         
215         /*
216  --     ** -- V I R T U A L   F U N C T I O N S -----------------------------------
217         */
218
219 protected:
220         virtual void on_changed();
221
222         virtual void on_guid_changed(GUID guid);
223
224         /*!     Function to be overloaded that fills 
225         */
226         virtual void get_times_vfunc(time_set &set) const = 0;
227 };      
228
229 sinfg::Node* sinfg::find_node(const sinfg::GUID& guid);
230
231 template<typename T> etl::handle<T>
232 guid_cast(const sinfg::GUID& guid)
233 {
234         return etl::handle<T>::cast_dynamic(sinfg::find_node(guid));
235 }
236
237 typedef etl::handle<Node> NodeHandle;
238
239 }; // END of namespace sinfg
240
241 /* === E N D =============================================================== */
242
243 #endif