2 Copyright (C) 2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __lib_pbd_functor_command_h__
21 #define __lib_pbd_functor_command_h__
28 #include <pbd/xml++.h>
29 #include <pbd/shiva.h>
30 #include <pbd/command.h>
31 #include <pbd/failed_constructor.h>
33 /** This command class is initialized
38 template <class obj_type, class arg_type>
39 class FunctorCommand : public Command
42 typedef void (obj_type::*functor_type)(arg_type);
43 typedef std::map< std::string, functor_type > FunctorMap;
44 typedef typename FunctorMap::iterator FunctorMapIterator;
52 ) : functor_name(functor),
57 method = find_functor(functor);
59 /* catch destruction of the object */
60 new PBD::Shiva< obj_type, FunctorCommand<obj_type, arg_type> > (object, *this);
68 (object.*method) (after);
72 (object.*method) (before);
75 virtual XMLNode &get_state() {
78 XMLNode *node = new XMLNode("FunctorCommand");
79 node->add_property("functor", functor_name);
81 node->add_property("before", ss.str());
84 node->add_property("after", ss.str());
89 static void register_functor(std::string name, functor_type f) {
90 functor_map[name] = f;
94 static functor_type find_functor(std::string name) {
95 FunctorMapIterator iter;
97 if((iter = functor_map.find(name)) == functor_map.end()) {
98 throw failed_constructor();
105 std::string functor_name;
110 static FunctorMap functor_map;
113 // static initialization of functor_map...
114 template <class obj_type, class arg_type>
115 typename FunctorCommand<obj_type, arg_type>::FunctorMap
116 FunctorCommand<obj_type, arg_type>::functor_map;
120 #endif // __lib_pbd_functor_command_h__