Ardour  8.12
functor_command.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Paul Davis <paul@linuxaudiosystems.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __lib_pbd_functor_command_h__
20 #define __lib_pbd_functor_command_h__
21 
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <map>
26 
27 #include "pbd/libpbd_visibility.h"
28 #include "pbd/xml++.h"
29 #include "pbd/shiva.h"
30 #include "pbd/command.h"
31 #include "pbd/failed_constructor.h"
32 
36 namespace PBD {
37 
38 template <class obj_type, class arg_type>
39 class /*LIBPBD_API*/ FunctorCommand : public PBD::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 
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;
108 };
109 
110 // static initialization of functor_map...
111 template <class obj_type, class arg_type>
114 
115 };
116 
117 #endif // __lib_pbd_functor_command_h__
118 
const std::string & name() const
Definition: command.h:43
static void register_functor(std::string name, functor_type f)
virtual XMLNode & get_state()
static FunctorMap functor_map
static functor_type find_functor(std::string name)
FunctorMap::iterator FunctorMapIterator
std::string functor_name
std::map< std::string, functor_type > FunctorMap
void(obj_type::* functor_type)(arg_type)
FunctorCommand(std::string functor, obj_type &object, arg_type b, arg_type a)
Definition: xml++.h:114
Definition: axis_view.h:42