Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / synfig / node.h
1 /* === S Y N F I 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-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_PARENTNODE_H
26 #define __SYNFIG_PARENTNODE_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <sigc++/signal.h>
31 #include <set>
32 #include "time.h"
33 #include "guid.h"
34 #include <ETL/handle>
35 #include "interpolation.h"
36 #include "mutex.h"
37
38 /* === M A C R O S ========================================================= */
39
40 /* === T Y P E D E F S ===================================================== */
41
42 /* === C L A S S E S & S T R U C T S ======================================= */
43
44 namespace synfig {
45
46 class TimePoint
47 {
48         GUID guid;
49         Time time;
50         Interpolation before,after;
51 public:
52
53         TimePoint(const Time& x=Time::begin()):
54                 guid(0),
55                 time(x),
56                 before(INTERPOLATION_NIL),
57                 after(INTERPOLATION_NIL)
58         {
59         }
60
61         const GUID& get_guid()const { return guid; }
62         const Time& get_time()const { return time; }
63         Interpolation get_before()const { return before; }
64         Interpolation get_after()const { return after; }
65
66         void set_guid(const GUID& x) { guid=x; }
67         void set_time(const Time& x) { time=x; }
68         void set_before(Interpolation x) { before=x; }
69         void set_after(Interpolation x) { after=x; }
70         
71         void absorb(const TimePoint& x);
72 }; // END of class TimePoint
73
74 inline TimePoint operator+(TimePoint lhs,const Time& rhs)
75         { lhs.set_time(lhs.get_time()+rhs); return lhs; }
76
77 inline bool operator<(const TimePoint& lhs,const TimePoint& rhs)
78         { return lhs.get_time()<rhs.get_time(); }
79
80 inline bool operator<(const TimePoint& lhs,const Time& rhs)
81         { return lhs.get_time()<rhs; }
82
83 inline bool operator<(const Time& lhs,const TimePoint& rhs)
84         { return lhs<rhs.get_time(); }
85
86 inline bool operator==(const TimePoint& lhs,const TimePoint& rhs)
87         { return lhs.get_time()==rhs.get_time(); }
88
89 inline bool operator!=(const TimePoint& lhs,const TimePoint& rhs)
90         { return lhs.get_time()!=rhs.get_time(); }
91
92 class TimePointSet : public std::set<TimePoint>
93 {
94 public:
95         iterator insert(const TimePoint& x);
96
97         template <typename ITER> void insert(ITER begin, ITER end)
98                 { for(;begin!=end;++begin) insert(*begin); }
99
100 }; // END of class TimePointSet
101         
102 class Node : public etl::rshared_object
103 {
104         /*
105  --     ** -- T Y P E S -----------------------------------------------------------
106         */
107
108 public: 
109         
110         //! \writeme
111         typedef TimePointSet    time_set;
112
113         /*
114  --     ** -- D A T A -------------------------------------------------------------
115         */
116
117 private:
118
119         //! \writeme
120         GUID guid_;
121
122         //! cached time values for all the childrens
123         mutable time_set        times;
124
125         //! \writeme
126         mutable bool            bchanged;
127
128         //! \writeme
129         mutable int time_last_changed_;
130
131         //! \writeme
132         mutable RWLock rw_lock_;
133         
134         //! \writeme
135         bool deleting_;
136
137 public:
138
139         //! \todo This should really be private
140         std::set<Node*>         parent_set;
141
142         /*
143  -- ** -- S I G N A L S -------------------------------------------------------
144         */
145
146 private:
147         
148         sigc::signal<void> signal_changed_;
149
150         //!     GUID Changed
151         /*! \note The second parameter is the *OLD* guid! */
152         sigc::signal<void,GUID> signal_guid_changed_;   
153
154         //!     Deleted
155         sigc::signal<void> signal_deleted_;     
156
157         /*
158  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
159         */
160
161 public:
162
163         sigc::signal<void>& signal_deleted() { return signal_deleted_; }
164
165         sigc::signal<void>& signal_changed() { return signal_changed_; }
166
167         //!     GUID Changed
168         /*! \note The second parameter is the *OLD* guid! */
169         sigc::signal<void,GUID>& signal_guid_changed() { return signal_guid_changed_; }
170
171         /*
172  --     ** -- C O N S T R U C T O R S ---------------------------------------------
173         */
174
175 protected:
176
177         Node();
178
179         // This class cannot be copied -- use clone() if necessary
180 private:
181         Node(const Node &x);
182
183 public:
184         virtual ~Node();
185
186         /*
187  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
188         */
189         
190 public:
191
192         void changed();
193
194         //! Gets the GUID for this value node
195         const GUID& get_guid()const;
196
197         //! Sets the GUID for this value node
198         void set_guid(const GUID& x);
199
200         int get_time_last_changed()const;
201         
202         void add_child(Node*x);
203
204         void remove_child(Node*x);
205
206         int parent_count()const;
207         
208         const time_set &get_times() const;
209
210         RWLock& get_rw_lock()const { return rw_lock_; }
211         
212 protected:
213         
214         void begin_delete();
215         
216         /*
217  --     ** -- V I R T U A L   F U N C T I O N S -----------------------------------
218         */
219
220 protected:
221         virtual void on_changed();
222
223         virtual void on_guid_changed(GUID guid);
224
225         /*!     Function to be overloaded that fills 
226         */
227         virtual void get_times_vfunc(time_set &set) const = 0;
228 };      
229
230 synfig::Node* find_node(const synfig::GUID& guid);
231
232 template<typename T> etl::handle<T>
233 guid_cast(const synfig::GUID& guid)
234 {
235         return etl::handle<T>::cast_dynamic(synfig::find_node(guid));
236 }
237
238 typedef etl::handle<Node> NodeHandle;
239
240 }; // END of namespace synfig
241
242 /* === E N D =============================================================== */
243
244 #endif