*** NEW CODING POLICY ***
[ardour.git] / libs / pbd / pbd / functor_command.h
1 /* 
2    Copyright (C) 2007 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __lib_pbd_functor_command_h__
21 #define __lib_pbd_functor_command_h__
22
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <map>
27
28 #include "pbd/xml++.h"
29 #include "pbd/shiva.h"
30 #include "pbd/command.h"
31 #include "pbd/failed_constructor.h"
32
33 /** This command class is initialized 
34  */
35
36 namespace PBD {
37
38 template <class obj_type, class arg_type>
39 class FunctorCommand : public Command
40 {
41         private:
42         typedef void (obj_type::*functor_type)(arg_type);
43         typedef std::map< std::string, functor_type > FunctorMap;
44         typedef typename FunctorMap::iterator FunctorMapIterator;
45
46         public:
47         FunctorCommand(std::string functor, obj_type& object, arg_type b, arg_type a) 
48                 : functor_name(functor)
49                 , object(object)
50                 , before(b)
51                 , after(a) 
52         {
53                 method = find_functor(functor);
54
55                 /* catch destruction of the object */
56                 new PBD::Shiva< obj_type, FunctorCommand<obj_type, arg_type> > (object, *this);
57         }
58
59         ~FunctorCommand() {
60                 GoingAway();
61         }
62
63         void operator() () {
64                 (object.*method) (after);
65         }
66
67         void undo() { 
68                 (object.*method) (before);
69         }
70
71         virtual XMLNode &get_state() {
72                 std::stringstream ss;
73                 
74                 XMLNode *node = new XMLNode("FunctorCommand");
75                 node->add_property("type_name", typeid(obj_type).name());
76                 node->add_property("functor", functor_name);
77                 ss << before;
78                 node->add_property("before", ss.str());
79                 ss.clear ();
80                 ss << after;
81                 node->add_property("after", ss.str());
82
83                 return *node;
84         }
85
86         static void register_functor(std::string name, functor_type f) {
87                 functor_map[name] = f;
88         }
89
90         private:
91         static functor_type find_functor(std::string name) {
92                 FunctorMapIterator iter;
93
94                 if((iter = functor_map.find(name)) == functor_map.end()) {
95                         throw failed_constructor();
96                 }
97
98                 return iter->second;
99         }
100
101         protected:
102         std::string functor_name;
103         obj_type &object;
104         arg_type before;
105         arg_type after;
106         functor_type method;
107         static FunctorMap functor_map;
108 };
109
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;
114
115 };
116
117 #endif // __lib_pbd_functor_command_h__
118