swimtracker-firmware/firmware/lib/userdb/UserDB.h

73 lines
1.7 KiB
C++

#pragma once
#ifndef PLATFORM_NATIVE
#include <Arduino.h>
#endif
using SessionIdType = uint32_t;
constexpr size_t MAX_USERS = 64;
constexpr size_t INITIAL_SESSIONS_PER_USER = 128;
struct User
{
public:
User() : numSessions_(0), numSessionsAllocated_(0), sessionIds_(nullptr) {}
void init(const String &name, size_t sessionAllocateSize = INITIAL_SESSIONS_PER_USER);
void freeResources();
bool load(const String &name);
void save();
void remove();
bool valid() const { return sessionIds_ != nullptr; }
void insertSession(SessionIdType sessionId);
bool removeSession(SessionIdType sessionId);
bool hasSession(SessionIdType sessionId) const;
const String &name() const { return name_; }
// session access
SessionIdType *sessionBegin() { return sessionIds_; }
SessionIdType *sessionEnd() { return sessionIds_ + numSessions_; }
const SessionIdType *sessionBegin() const { return sessionIds_; }
const SessionIdType *sessionEnd() const { return sessionIds_ + numSessions_; }
private:
void growSessionArrayIfNecessary();
String name_;
size_t numSessions_;
size_t numSessionsAllocated_;
SessionIdType *sessionIds_;
};
class UserStorage
{
public:
UserStorage()
: numUsers_(0)
{
fillFromFileSystem();
}
User *getUserInfo(const String &userName);
User *getUnassignedUser();
User *addNewUser(const String &userName);
bool deleteUser(const String &userName);
User *begin() { return &users_[0]; }
User *end() { return &users_[numUsers_]; }
const User *begin() const { return &users_[0]; }
const User *end() const { return &users_[numUsers_]; }
private:
void fillFromFileSystem();
User users_[MAX_USERS];
size_t numUsers_;
};