Uniot Core
0.8.1
Loading...
Searching...
No Matches
ObjectRegister.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <Arduino.h>
22
23#include "Common.h"
25#include "Register.h"
26
27namespace uniot {
28
34
46class ObjectRegister : public Register<Pair<uint32_t, RecordPtr>> {
47 public:
52
56 virtual ~ObjectRegister() {}
57
61 ObjectRegister(ObjectRegister const &) = delete;
62
66 void operator=(ObjectRegister const &) = delete;
67
77 bool link(const String &name, RecordPtr link, uint32_t id = FOURCC(____)) {
78 return addToRegister(name, MakePair(id, link));
79 }
80
94 template <typename T>
95 T *get(const String &name, size_t index) {
97 if (getRegisterValue(name, index, record)) {
98 if (!record.second) {
99 return nullptr;
100 }
101 if (ObjectRegisterRecord::exists(record.second)) {
102 return Type::safeStaticCast<T>(record.second);
103 }
104 setRegisterValue(name, index, MakePair(FOURCC(dead), nullptr));
105 UNIOT_LOG_DEBUG("record is dead [%s][%d]", name.c_str(), index);
106 }
107 return nullptr;
108 }
109};
110
111} // namespace uniot
T * get(const String &name, size_t index)
Retrieves a registered object by name and index with type casting.
Definition ObjectRegister.h:95
bool link(const String &name, RecordPtr link, uint32_t id=FOURCC(____))
Links an object to a name in the register.
Definition ObjectRegister.h:77
virtual ~ObjectRegister()
Virtual destructor.
Definition ObjectRegister.h:56
void operator=(ObjectRegister const &)=delete
Deleted assignment operator to prevent copying.
ObjectRegister()
Default constructor.
Definition ObjectRegister.h:51
ObjectRegister(ObjectRegister const &)=delete
Deleted copy constructor to prevent copying.
Definition ObjectRegisterRecord.h:40
static bool exists(ObjectRegisterRecord *record)
Checks if a record still exists in the registry.
Definition ObjectRegisterRecord.h:84
bool addToRegister(const String &name, const Pair< uint32_t, RecordPtr > &value)
Definition Register.h:130
bool setRegisterValue(const String &name, size_t idx, const Pair< uint32_t, RecordPtr > &value)
Definition Register.h:176
bool getRegisterValue(const String &name, size_t idx, Pair< uint32_t, RecordPtr > &outValue) const
Definition Register.h:155
static T * safeStaticCast(IWithType *obj)
Safely cast a pointer to type T if the runtime type matches.
Definition TypeId.h:99
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:184
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:169
#define FOURCC(name)
Creates a FourCC constant from a string literal.
Definition Common.h:107
#define UNIOT_LOG_DEBUG(...)
Log an DEBUG level message Used for general information about system operation. Only compiled if UNIO...
Definition Logger.h:293
ObjectRegisterRecord * RecordPtr
Type alias for ObjectRegisterRecord pointers for better readability.
Definition ObjectRegister.h:33
Contains all classes and functions related to the Uniot Core.