reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

action.hpp (1627B)


      1 /* ReaPack: Package manager for REAPER
      2  * Copyright (C) 2015-2025  Christian Fillion
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU Lesser General Public License as published by
      6  * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Lesser General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #ifndef REAPACK_ACTION_HPP
     19 
     20 #include <functional>
     21 #include <map>
     22 #include <memory>
     23 
     24 #include <reaper_plugin.h>
     25 
     26 class Action {
     27 public:
     28   typedef unsigned short CommandID;
     29   typedef std::function<void ()> Callback;
     30 
     31   Action(const char *name, const char *desc, const Callback &);
     32   Action(const Action &) = delete;
     33   ~Action();
     34 
     35   CommandID id() const { return m_reg.accel.cmd; }
     36   void run() const { m_callback(); }
     37 
     38 private:
     39   const char *m_name;
     40   gaccel_register_t m_reg;
     41   Callback m_callback;
     42 };
     43 
     44 class ActionList {
     45 public:
     46   template<typename... Args> void add(Args&&... args)
     47   {
     48     auto action = std::make_unique<Action>(args...);
     49     m_list.emplace(action->id(), std::move(action));
     50   }
     51 
     52   bool run(Action::CommandID id) const;
     53 
     54 private:
     55   Action *find(Action::CommandID id) const;
     56 
     57   std::map<Action::CommandID, std::unique_ptr<Action>> m_list;
     58 };
     59 
     60 #endif