dimanche 17 juillet 2022

Making a template class for a State manager

i am trying to make a statemanager class to manage my program.

I have the current code:

CPP

#include "StateManager.hpp"

template <class T>
StateManager<T>::StateManager() {}

template <class T>
StateManager<T>::~StateManager() {}

template <class T>
void StateManager<T>::setState(T state)
{
    _current_state = state;
}

/*
 * Get States
 * Returns the current state of the device
 */
template <class T>
T StateManager<T>::getCurrentState()
{
    return _current_state;
}

HPP

#ifndef STATEMANAGER_HPP
#define STATEMANAGER_HPP
#include <Arduino.h>

/*
 * StateManager
 * All Project States are managed here
 */
class ProgramStates
{
public:
    struct DeviceStates
    {
        enum State_e
        {
            Starting,
            Started,
            Stopping,
            Stopped,
            Error

        };

        enum WiFiState_e
        {
            WiFiState_None,
            WiFiState_Connecting,
            WiFiState_Connected,
            WiFiState_Disconnected,
            WiFiState_Disconnecting,
            WiFiState_Error
        };

        enum WebServerState_e
        {
            WebServerState_None,
            WebServerState_Starting,
            WebServerState_Started,
            WebServerState_Stopping,
            WebServerState_Stopped,
            WebServerState_Error
        };

        enum MDNSState_e
        {
            MDNSState_None,
            MDNSState_Starting,
            MDNSState_Started,
            MDNSState_Stopping,
            MDNSState_Stopped,
            MDNSState_Error
        };

        enum CameraState_e
        {
            Camera_Success,
            Camera_Connected,
            Camera_Disconnected,
            Camera_Error
        };

        enum ButtonState_e
        {
            Buttons_OFF,
            Buttons_ON,
            Buttons_PLUS,
            Buttons_MINUS,
            Buttons_Error
        };

        enum StreamState_e
        {
            Stream_OFF,
            Stream_ON,
            Stream_Error
        };
    };
};

/*
 * EventManager
 * All Project Events are managed here
 */
template <class T>
class StateManager
{
public:
    StateManager();
    virtual ~StateManager();

    void setState(T state);

    T getCurrentState();

private:
    static T _current_state;
};

#endif // STATEMANAGER_HPP

Everything compiles, but does not link. I get undefined references when trying to use the statemanager methods.

Ex usage

StateManager<ProgramStates::DeviceStates::MDNSState_e> mdns_stateManager;
StateManager<ProgramStates::DeviceStates::WiFiState_e> wifi_stateManager;

WiFiHandler::setupWifi(ssid, password, &wifi_stateManager);
MDNSHandler::setupMDNS(MDSNTrackerName, &mdns_stateManager);

These two methods accept an argument of a class object, allowing me to pass a reference to it. However, i get undefiend on the class instationation and on calling the methods inside of the receiving classes. My intellisense shows no errors and the code compiles but does not link. I am still new to c++, and am learning - so it's probably an obvious mistake.

Aucun commentaire:

Enregistrer un commentaire