1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
5 ** $Id: node.cpp,v 1.5 2005/01/07 03:29:12 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
25 #define HASH_MAP_H <ext/hash_map>
35 #include "proto/nodebase.h"
39 using namespace __gnu_cxx;
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === M A C R O S ========================================================= */
54 // About BE_FRUGAL_WITH_GUIDS
55 // If this macro is set, then a GUID will NOT
56 // be calculated until the first call to get_guid()
57 // This also means that the node doesn't get
58 // added to the database until get_guid() is called
59 // for the first time, or set_guid() is called.
60 // If it is expensive to calculate GUIDs, then
61 // this can improve performance a tad in
62 // some cases. Otherwise, it doesn't change
64 #define BE_FRUGAL_WITH_GUIDS 1
69 # define __sys_clock ::clock
73 # define __sys_clock ::clock
76 extern clock_t _clock();
77 # define CLOCKS_PER_SEC 1000
78 # define __sys_clock _clock
83 /* === G L O B A L S ======================================================= */
86 typedef hash_map<GUID,Node*,GUIDHash> GlobalNodeMap;
88 typedef map<GUID,Node*> GlobalNodeMap;
91 static GlobalNodeMap* global_node_map_;
93 static GlobalNodeMap& global_node_map()
96 global_node_map_=new GlobalNodeMap;
97 return *global_node_map_;
100 /* === P R O C E D U R E S ================================================= */
103 synfig::find_node(const GUID& guid)
105 if(global_node_map().count(guid)==0)
107 return global_node_map()[guid];
111 refresh_node(synfig::Node* node, GUID old_guid)
113 assert(global_node_map().count(old_guid));
114 global_node_map().erase(old_guid);
115 assert(!global_node_map().count(old_guid));
116 global_node_map()[node->get_guid()]=node;
119 /* === M E T H O D S ======================================================= */
122 TimePoint::absorb(const TimePoint& x)
124 if(get_guid()==x.get_guid())
126 set_guid(get_guid()^x.get_guid());
128 if(get_after()==INTERPOLATION_NIL)
129 set_after(x.get_after());
130 if(get_before()==INTERPOLATION_NIL)
131 set_before(x.get_before());
133 if(get_after()!=x.get_after() && x.get_after()!=INTERPOLATION_NIL)
134 set_after(INTERPOLATION_UNDEFINED);
135 if(get_before()!=x.get_before() && x.get_before()!=INTERPOLATION_NIL)
136 set_before(INTERPOLATION_UNDEFINED);
139 TimePointSet::iterator
140 TimePointSet::insert(const TimePoint& x)
142 iterator iter(find(x));
145 const_cast<TimePoint&>(*iter).absorb(x);
148 return std::set<TimePoint>::insert(x).first;
170 #ifndef BE_FRUGAL_WITH_GUIDS
173 assert(!global_node_map().count(guid_));
174 global_node_map()[guid_]=this;
184 assert(global_node_map().count(guid_));
185 global_node_map().erase(guid_);
186 assert(!global_node_map().count(guid_));
193 time_last_changed_=__sys_clock();
198 //! Gets the GUID for this value node
200 Node::get_guid()const
202 #ifdef BE_FRUGAL_WITH_GUIDS
205 const_cast<GUID&>(guid_).make_unique();
207 assert(!global_node_map().count(guid_));
208 global_node_map()[guid_]=const_cast<Node*>(this);
215 //! Sets the GUID for this value node
217 Node::set_guid(const GUID& x)
221 #ifdef BE_FRUGAL_WITH_GUIDS
225 assert(!global_node_map().count(guid_));
226 global_node_map()[guid_]=this;
234 refresh_node(this, oldguid);
235 on_guid_changed(oldguid);
240 Node::get_time_last_changed()const
242 return time_last_changed_;
246 Node::add_child(Node*x)
248 x->parent_set.insert(this);
252 Node::remove_child(Node*x)
254 if(x->parent_set.count(this)) x->parent_set.erase(this);
258 Node::parent_count()const
260 return parent_set.size();
263 const Node::time_set &
264 Node::get_times() const
269 get_times_vfunc(times);
273 //set the output set...
282 deleting_=true; signal_deleted()();
292 std::set<Node*>::iterator iter;
293 for(iter=parent_set.begin();iter!=parent_set.end();++iter)
300 Node::on_guid_changed(GUID guid)
302 signal_guid_changed()(guid);