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;
47 FunctorCommand(std::string functor, obj_type& object, arg_type b, arg_type a)
48 : functor_name(functor)
53 method = find_functor(functor);
55 /* catch destruction of the object */
56 new PBD::Shiva< obj_type, FunctorCommand<obj_type, arg_type> > (object, *this);
64 (object.*method) (after);
68 (object.*method) (before);
71 virtual XMLNode &get_state() {
74 XMLNode *node = new XMLNode("FunctorCommand");
75 node->add_property("type_name", typeid(obj_type).name());
76 node->add_property("functor", functor_name);
78 node->add_property("before", ss.str());
81 node->add_property("after", ss.str());
86 static void register_functor(std::string name, functor_type f) {
87 functor_map[name] = f;
91 static functor_type find_functor(std::string name) {
92 FunctorMapIterator iter;
94 if((iter = functor_map.find(name)) == functor_map.end()) {
95 throw failed_constructor();
102 std::string functor_name;
107 static FunctorMap functor_map;
110 // static initialization of functor_map...
111 template <class obj_type, class arg_type>
112 typename FunctorCommand<obj_type, arg_type>::FunctorMap
113 FunctorCommand<obj_type, arg_type>::functor_map;
117 #endif // __lib_pbd_functor_command_h__