Small documentation in the valuenode.h
[synfig.git] / synfig-core / src / synfig / valuenode.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.h
3 **      \brief Header file for implementation of the "Placeholder" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_VALUENODE_H
27 #define __SYNFIG_VALUENODE_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include "vector.h"
32 #include "value.h"
33 #include "string.h"
34 #include "releases.h"
35 #include <ETL/handle>
36 #include <ETL/stringf>
37 #include "exception.h"
38 #include <map>
39 #include <sigc++/signal.h>
40 #include "guid.h"
41 #include <ETL/angle>
42
43 #include "node.h"
44
45 #include <set>
46
47 /* === M A C R O S ========================================================= */
48
49 #define CHECK_TYPE_AND_SET_VALUE(variable, type)                                                \
50         /* I don't think this ever happens - maybe remove this code? */         \
51         if (get_type() == ValueBase::TYPE_NIL) {                                                        \
52                 warning("%s:%d get_type() IS nil sometimes!",                                   \
53                                 __FILE__, __LINE__);                                                                    \
54                 return false;                                                                                                   \
55         }                                                                                                                                       \
56         if (get_type() != ValueBase::TYPE_NIL &&                                                        \
57                 !(ValueBase::same_type_as(value->get_type(), type)) &&                  \
58                 !PlaceholderValueNode::Handle::cast_dynamic(value)) {                   \
59                 error(_("%s:%d wrong type for %s: need %s but got %s"),                 \
60                           __FILE__, __LINE__,                                                                           \
61                           link_local_name(i).c_str(),                                                           \
62                           ValueBase::type_local_name(type).c_str(),                                     \
63                           ValueBase::type_local_name(value->get_type()).c_str());       \
64                 return false;                                                                                                   \
65         }                                                                                                                                       \
66         variable = value;                                                                                                       \
67         signal_child_changed()(i);                                                                                      \
68         signal_value_changed()();                                                                                       \
69         return true
70
71 /* === T Y P E D E F S ===================================================== */
72
73 /* === C L A S S E S & S T R U C T S ======================================= */
74
75 namespace synfig {
76
77 class Canvas;
78 class LinkableValueNode;
79 class Layer;
80
81 /*!     \class ValueNode
82 **      \todo writeme
83 */
84 class ValueNode : public synfig::Node
85 {
86         friend class Layer;
87         friend class LinkableValueNode;
88
89         /*
90  --     ** -- T Y P E S -----------------------------------------------------------
91         */
92
93 public:
94
95         typedef etl::handle<ValueNode> Handle;
96
97         typedef etl::loose_handle<ValueNode> LooseHandle;
98
99         typedef etl::handle<const ValueNode> ConstHandle;
100
101         typedef etl::rhandle<ValueNode> RHandle;
102
103
104         static bool subsys_init();
105
106         static bool subsys_stop();
107
108         /*
109  --     ** -- D A T A -------------------------------------------------------------
110         */
111
112 private:
113         ValueBase::Type type;
114         String name;
115         etl::loose_handle<Canvas> canvas_;
116         etl::loose_handle<Canvas> root_canvas_;
117
118         /*
119  -- ** -- S I G N A L S -------------------------------------------------------
120         */
121
122 private:
123
124         //!     ValueBase Changed
125         sigc::signal<void> signal_value_changed_;
126
127         //!     Children Reordered
128         sigc::signal<void,int*> signal_children_reordered_;
129
130         //!     Child Changed
131         sigc::signal<void,int> signal_child_changed_;
132
133         //!     Child Removed
134         sigc::signal<void,int> signal_child_removed_;
135
136         //!     Child Inserted
137         sigc::signal<void,int> signal_child_inserted_;
138
139         //!     ID Changed
140         sigc::signal<void> signal_id_changed_;
141
142         /*
143  -- ** -- S I G N A L   I N T E R F A C E -------------------------------------
144         */
145
146 public:
147
148         //!     ValueBase Changed
149         sigc::signal<void>& signal_value_changed() { return signal_value_changed_; }
150
151         //!     Children Reordered
152         sigc::signal<void,int*>& signal_children_reordered() { return signal_children_reordered_; }
153
154         //!     Child Changed
155         sigc::signal<void,int>& signal_child_changed() { return signal_child_changed_; }
156
157         //!     Child Removed
158         sigc::signal<void,int>& signal_child_removed() { return signal_child_removed_; }
159
160         //!     Child Inserted
161         sigc::signal<void,int>& signal_child_inserted() { return signal_child_inserted_; }
162
163         //!     ID Changed
164         sigc::signal<void>& signal_id_changed() { return signal_id_changed_; }
165
166         /*
167  --     ** -- C O N S T R U C T O R S ---------------------------------------------
168         */
169
170 protected:
171
172         ValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
173
174 public:
175
176         virtual ~ValueNode();
177
178         /*
179  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
180         */
181
182 public:
183
184         //! Returns the value of the ValueNode at time \a t
185         virtual ValueBase operator()(Time /*t*/)const
186                 { return ValueBase(); }
187
188         //! \internal Sets the id of the ValueNode
189         void set_id(const String &x);
190
191         //! Returns the id of the ValueNode
192         /*!     The ID is used for keeping track of a
193         **      specific instance of a ValueNode. */
194         const String &get_id()const { return name; }
195
196         //! Returns the name of the ValueNode type
197         virtual String get_name()const=0;
198
199         //! Returns the localized name of the ValueNode type
200         virtual String get_local_name()const=0;
201
202         //! Return a full description of the ValueNode and its parentage
203         virtual String get_description(bool show_exported_name = true)const;
204
205
206         //! \writeme
207         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const=0;
208
209         //! \writeme
210         bool is_exported()const { return !get_id().empty(); }
211
212         //! Returns the type of the ValueNode
213         ValueBase::Type get_type()const { return type; }
214
215         //! Returns a handle to the parent canvas, if it has one.
216         etl::loose_handle<Canvas> get_parent_canvas()const { return canvas_; }
217
218         //! Returns a handle to the parent canvas, if it has one.
219         etl::loose_handle<Canvas> get_root_canvas()const { return root_canvas_; }
220
221         //! \writeme
222         void set_parent_canvas(etl::loose_handle<Canvas> x);
223
224         //! \writeme
225         void set_root_canvas(etl::loose_handle<Canvas> x);
226
227         //! \writeme
228         String get_relative_id(etl::loose_handle<const Canvas> x)const;
229
230         int replace(etl::handle<ValueNode> x);
231
232 protected:
233         //! Sets the type of the ValueNode
234         void set_type(ValueBase::Type t) { type=t; }
235
236         virtual void on_changed();
237 }; // END of class ValueNode
238
239 /*!     \class PlaceholderValueNode
240 **      \todo writeme
241 */
242 class PlaceholderValueNode : public ValueNode
243 {
244 public:
245         typedef etl::handle<PlaceholderValueNode> Handle;
246         typedef etl::loose_handle<PlaceholderValueNode> LooseHandle;
247         typedef etl::handle<const PlaceholderValueNode> ConstHandle;
248         typedef etl::rhandle<PlaceholderValueNode> RHandle;
249
250 private:
251
252         PlaceholderValueNode(ValueBase::Type type=ValueBase::TYPE_NIL);
253
254 public:
255
256         virtual ValueBase operator()(Time t)const;
257
258         virtual String get_name()const;
259
260         virtual String get_local_name()const;
261
262         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
263
264         static Handle create(ValueBase::Type type=ValueBase::TYPE_NIL);
265
266 protected:
267         virtual void get_times_vfunc(Node::time_set &/*set*/) const {}
268 }; // END of class PlaceholderValueNode
269
270
271 /*!     \class LinkableValueNode
272 **      \todo writeme
273 */
274 class LinkableValueNode : public ValueNode
275 {
276         friend class ValueNode;
277 public:
278
279         typedef etl::handle<LinkableValueNode> Handle;
280
281         typedef etl::loose_handle<LinkableValueNode> LooseHandle;
282
283         typedef etl::handle<const LinkableValueNode> ConstHandle;
284
285         typedef etl::rhandle<LinkableValueNode> RHandle;
286
287
288         //! Type that represents a pointer to a ValueNode's constructor
289         /*! As a pointer to the constructor, it represents a "factory" of
290         **  objects of this class.
291         */
292         typedef LinkableValueNode* (*Factory)(const ValueBase&);
293
294         //! This represents a pointer to a Type check member fucntion
295         /*! As a pointer to the member, it represents a fucntion that checks
296         **  the type of the provided ValueBase
297         */
298         typedef bool (*CheckType)(ValueBase::Type);
299
300         struct BookEntry
301         {
302                 String local_name;
303                 Factory factory;
304                 CheckType check_type;
305                 ReleaseVersion release_version; // which version of synfig introduced this valuenode type
306         };
307
308         //! Book of types of linkable value nodes indexed by type name.
309         /*! While the sifz file is read, each time a new LinkableValueNode entry
310         **  is found, the factory constructor that the "factory" pointer member
311         **  of the "BookEntry" struct points to, is called, and a new object of
312         **  that type is created.
313         **  \sa LinkableValueNode::Factory
314         */
315         typedef std::map<String,BookEntry> Book;
316
317         static Book& book();
318
319         static Handle create(const String &name, const ValueBase& x);
320
321         static bool check_type(const String &name, ValueBase::Type x);
322
323 public:
324         LinkableValueNode(ValueBase::Type type=ValueBase::TYPE_NIL):
325                 ValueNode(type) { }
326
327 protected:
328         virtual bool set_link_vfunc(int i,ValueNode::Handle x)=0;
329
330         void unlink_all();
331
332 public:
333
334         virtual int link_count()const=0;
335
336         virtual String link_local_name(int i)const=0;
337
338         virtual String link_name(int i)const=0;
339
340         virtual int get_link_index_from_name(const String &name)const=0;
341
342         virtual ValueNode* clone(const GUID& deriv_guid=GUID())const;
343
344         bool set_link(int i,ValueNode::Handle x);
345         bool set_link(const String &name,ValueNode::Handle x) { return set_link(get_link_index_from_name(name),x);      }
346
347         ValueNode::LooseHandle get_link(int i)const;
348         ValueNode::LooseHandle get_link(const String &name)const { return get_link(get_link_index_from_name(name)); }
349
350         String
351         get_description(int index = -1, bool show_exported_name = true)const;
352
353 protected:
354         //! Sets the type of the ValueNode
355         void set_type(ValueBase::Type t) { ValueNode::set_type(t); }
356
357         virtual ValueNode::LooseHandle get_link_vfunc(int i)const=0;
358
359         // Wrapper for new operator, used by clone()
360         virtual LinkableValueNode* create_new()const=0;
361
362         virtual void get_times_vfunc(Node::time_set &set) const;
363 }; // END of class LinkableValueNode
364
365 /*!     \class ValueNodeList
366 **      \brief A searchable value_node list container
367 **      \warning Do not confuse with ValueNode_DynamicList!
368 **      \todo writeme
369 */
370 class ValueNodeList : public std::list<ValueNode::RHandle>
371 {
372         int placeholder_count_;
373 public:
374         ValueNodeList();
375
376         //! Finds the ValueNode in the list with the given \a name
377         /*!     \return If found, returns a handle to the ValueNode.
378         **              Otherwise, returns an empty handle.
379         */
380         ValueNode::Handle find(const String &name);
381
382         //! Finds the ValueNode in the list with the given \a name
383         /*!     \return If found, returns a handle to the ValueNode.
384         **              Otherwise, returns an empty handle.
385         */
386         ValueNode::ConstHandle find(const String &name)const;
387
388         //! Removes the \a value_node from the list
389         bool erase(ValueNode::Handle value_node);
390
391         //! \writeme
392         bool add(ValueNode::Handle value_node);
393
394         //! \writeme
395         bool count(const String &id)const;
396
397         //! Similar to find, but will create a placeholder value_node if it cannot be found.
398         ValueNode::Handle surefind(const String &name);
399
400         //! Removes any value_nodes with reference counts of 1.
401         void audit();
402
403         //! Placeholder Count
404         int placeholder_count()const { return placeholder_count_; }
405 };
406
407 ValueNode::LooseHandle find_value_node(const GUID& guid);
408
409 }; // END of namespace synfig
410
411 /* === E N D =============================================================== */
412
413 #endif