This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Updatable.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <Def/Def.hpp>
6 #include <Helpers/Error.hpp>
7 #include <Helpers/LinkedList.hpp>
9 #include <stddef.h>
10 
12 
19 template <class T = NormalUpdatable>
20 class Updatable : public DoublyLinkable<Updatable<T>> {
21  protected:
24 
25  public:
27  virtual ~Updatable() {
28  if (isEnabled())
29  updatables.remove(this);
30  }
31 
33  virtual void update() = 0;
34 
36  virtual void begin() = 0;
37 
40  void enable() {
41  if (isEnabled()) {
42  ERROR(F("Error: This element is already enabled."), 0x1212);
43  return;
44  }
45  updatables.append(this);
46  }
47 
50  void disable() {
51  if (!isEnabled()) {
52  ERROR(F("Error: This element is already disabled."), 0x1213);
53  return;
54  }
55  updatables.remove(this);
56  }
57 
64  bool isEnabled() { return updatables.couldContain(this); }
65 
68  static void beginAll() {
69  for (Updatable &el : updatables)
70  el.begin();
71  }
72 
75  static void updateAll() {
76  for (Updatable &el : updatables)
77  el.update();
78  }
79 
80  static void enable(Updatable *element) { element->enable(); }
81 
82  static void enable(Updatable &element) { element.enable(); }
83 
84  template <class U, size_t N>
85  static void enable(U (&array)[N]) {
86  for (U &el : array)
87  enable(el);
88  }
89 
90  static void disable(Updatable<T> *element) { element->disable(); }
91 
92  static void disable(Updatable<T> &element) { element.disable(); }
93 
94  template <class U, size_t N>
95  static void disable(U (&array)[N]) {
96  for (U &el : array)
97  disable(el);
98  }
99 
100  private:
102 };
103 
104 template <class T>
106 
Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:20
Updatable::isEnabled
bool isEnabled()
Check if this updatable is enabled.
Definition: Updatable.hpp:64
Updatable::~Updatable
virtual ~Updatable()
Destructor: remove the updatable from the linked list of instances.
Definition: Updatable.hpp:27
Updatable::enable
static void enable(Updatable *element)
Definition: Updatable.hpp:80
ERROR
#define ERROR(x, e)
Definition: Error.hpp:27
Error.hpp
DoublyLinkedList::append
void append(Node *node)
Append a node to a linked list.
Definition: LinkedList.hpp:113
Def.hpp
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:50
Updatable::enable
void enable()
Enable this updatable: insert it into the linked list of instances, so it gets updated automatically.
Definition: Updatable.hpp:40
Updatable::begin
virtual void begin()=0
Initialize this updatable.
Updatable::update
virtual void update()=0
Update this updatable.
LinkedList.hpp
DoublyLinkedList::remove
void remove(Node *node)
Remove a node from the linked list.
Definition: LinkedList.hpp:196
Updatable::updateAll
static void updateAll()
Update all enabled instances of this class.
Definition: Updatable.hpp:75
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
DoublyLinkedList::couldContain
bool couldContain(Node *node)
Check if the linked list could contain the given node.
Definition: LinkedList.hpp:264
Updatable::updatables
static DoublyLinkedList< Updatable< T > > updatables
Definition: Updatable.hpp:101
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
DoublyLinkedList
A class for doubly linked lists.
Definition: LinkedList.hpp:23
DoublyLinkable
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:298
Updatable::beginAll
static void beginAll()
Begin all enabled instances of this class.
Definition: Updatable.hpp:68
Updatable::enable
static void enable(Updatable &element)
Definition: Updatable.hpp:82
Updatable::Updatable
Updatable()
Create an Updatabe and add it to the linked list of instances.
Definition: Updatable.hpp:23
SettingsWrapper.hpp
Updatable::enable
static void enable(U(&array)[N])
Definition: Updatable.hpp:85
Updatable::disable
static void disable(U(&array)[N])
Definition: Updatable.hpp:95
Updatable::disable
static void disable(Updatable< T > *element)
Definition: Updatable.hpp:90
Updatable::disable
static void disable(Updatable< T > &element)
Definition: Updatable.hpp:92
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23