Control Surface  1.1.1
MIDI Control Surface library for Arduino
Updatable.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 
7 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
8 
9 #include <AH/Containers/LinkedList.hpp>
10 #include <AH/Error/Error.hpp>
12 #include <stddef.h>
13 
15 
16 struct NormalUpdatable {};
17 
24 template <class T = NormalUpdatable>
25 class Updatable : public DoublyLinkable<Updatable<T>> {
26  protected:
28  Updatable() { updatables.append(this); }
29 
30  public:
32  virtual ~Updatable() {
33  if (isEnabled())
34  updatables.remove(this);
35  }
36 
38  virtual void update() = 0;
39 
41  virtual void begin() = 0;
42 
45  void enable() {
46  if (isEnabled()) {
47  ERROR(F("Error: This element is already enabled."), 0x1212);
48  return;
49  }
50  updatables.append(this);
51  }
52 
55  void disable() {
56  if (!isEnabled()) {
57  ERROR(F("Error: This element is already disabled."), 0x1213);
58  return;
59  }
60  updatables.remove(this);
61  }
62 
69  bool isEnabled() { return updatables.couldContain(this); }
70 
73  static void beginAll() {
74  for (Updatable &el : updatables)
75  el.begin();
76  }
77 
80  static void updateAll() {
81  for (Updatable &el : updatables)
82  el.update();
83  }
84 
85  static void enable(Updatable *element) { element->enable(); }
86 
87  static void enable(Updatable &element) { element.enable(); }
88 
89  template <class U, size_t N>
90  static void enable(U (&array)[N]) {
91  for (U &el : array)
92  enable(el);
93  }
94 
95  static void disable(Updatable<T> *element) { element->disable(); }
96 
97  static void disable(Updatable<T> &element) { element.disable(); }
98 
99  template <class U, size_t N>
100  static void disable(U (&array)[N]) {
101  for (U &el : array)
102  disable(el);
103  }
104 
105  private:
107 };
108 
109 template <class T>
111 
113 
AH::Updatable::disable
static void disable(U(&array)[N])
Definition: Updatable.hpp:100
AH::Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:25
Warnings.hpp
AH::NormalUpdatable
Definition: Updatable.hpp:16
Error.hpp
AH::Updatable::enable
static void enable(Updatable &element)
Definition: Updatable.hpp:87
AH::Updatable::disable
static void disable(Updatable< T > &element)
Definition: Updatable.hpp:97
AH::Updatable::updatables
static DoublyLinkedList< Updatable< T > > updatables
Definition: Updatable.hpp:106
SettingsWrapper.hpp
AH::Updatable::updateAll
static void updateAll()
Update all enabled instances of this class.
Definition: Updatable.hpp:80
AH::Updatable::enable
void enable()
Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.
Definition: Updatable.hpp:45
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::Updatable::disable
static void disable(Updatable< T > *element)
Definition: Updatable.hpp:95
ERROR
#define ERROR(msg, errc)
Print the error message and error code, and stop the execution if FATAL_ERRORS are enabled.
Definition: Error.hpp:42
AH::Updatable::beginAll
static void beginAll()
Begin all enabled instances of this class.
Definition: Updatable.hpp:73
AH::Updatable::enable
static void enable(Updatable *element)
Definition: Updatable.hpp:85
AH::Updatable::Updatable
Updatable()
Create an Updatabe and add it to the linked list of instances.
Definition: Updatable.hpp:28
AH::Updatable::enable
static void enable(U(&array)[N])
Definition: Updatable.hpp:90
AH::Updatable::~Updatable
virtual ~Updatable()
Destructor: remove the updatable from the linked list of instances.
Definition: Updatable.hpp:32
AH::Updatable::isEnabled
bool isEnabled()
Check if this updatable is enabled.
Definition: Updatable.hpp:69
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23
DoublyLinkedList
A class for doubly linked lists.
Definition: LinkedList.hpp:27
DoublyLinkable
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:302
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::Updatable::disable
void disable()
Disable this updatable: remove it from the linked list of instances, so it no longer gets updated aut...
Definition: Updatable.hpp:55