Sayonara Player
SettingNotifier.h
1 /* SettingNotifier.h */
2 
3 /* Copyright (C) 2011-2017 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef SETTINGNOTIFIER_H
22 #define SETTINGNOTIFIER_H
23 
24 #include "Utils/Logger/Logger.h"
25 #include "Utils/Settings/SayonaraClass.h"
26 #include "Utils/Settings/SettingKey.h"
27 
28 #include <functional>
29 #include <QObject>
30 
31 #pragma once
32 
33 class AbstrSettingNotifier : public QObject
34 {
35  Q_OBJECT
36 
37 signals:
38  void sig_value_changed();
39 
40 public:
41  template<typename T>
42  void add_listener(T* c, void (T::*fn)())
43  {
44  connect(this, &AbstrSettingNotifier::sig_value_changed, c, fn);
45  }
46 };
47 
48 template<typename KeyClass>
51 {
52 
53 private:
54  SettingNotifier() :
56  {}
57 
58  SettingNotifier(const SettingNotifier& other) = delete;
59 
60 
61 public:
62  ~SettingNotifier() {}
63 
64  static SettingNotifier<KeyClass>* instance()
65  {
66  static SettingNotifier<KeyClass> inst;
67  return &inst;
68  }
69 
70  void val_changed()
71  {
72  emit sig_value_changed();
73  }
74 };
75 
76 
77 
78 namespace Set
79 {
80  template<typename T, typename KeyClassInstance>
81  typename std::enable_if<std::is_base_of<SayonaraClass, T>::value, void>::type
82  listen(const KeyClassInstance& key, T* t, void (T::*fn)(), bool run=true)
83  {
84  using KeyClass=decltype(key);
85 
86  SettingNotifier<KeyClass>::instance()->add_listener(t, fn);
87 
88  if(run)
89  {
90  auto callable = std::bind(fn, t);
91  callable();
92  }
93  }
94 }
95 
96 
97 #endif // SETTINGNOTIFIER_H
Definition: SettingNotifier.h:33
Definition: SettingNotifier.h:49
Set namespace defines the setting: Which key and which type.
Definition: SettingKey.h:216