Skip to content

Commit

Permalink
Using enum-generator for CLibEzspInternal::State and CLibEzspPublic::…
Browse files Browse the repository at this point in the history
…State. Partially implements #20
  • Loading branch information
Lionel AINS committed Mar 17, 2020
1 parent cb51cb8 commit e1a8f4e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 115 deletions.
12 changes: 7 additions & 5 deletions include/ezsp/enum-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <sstream>
#include <stdexcept>
#include <cstring> // For strcmp()

/*!
* \brief Expansion macro for enum value definition
Expand Down Expand Up @@ -43,16 +44,16 @@
enum EnumType { \
ENUM_DEF(ENUM_VALUE) \
}; \
const char* getString(EnumType value); \
EnumType get##EnumType##Value(const char* str); \
static const char* getString(EnumType value); \
static EnumType get##EnumType##Value(const char* str); \

/*!
* \brief Define the access function names
*
* Marco to implement (define) the functions for the prototypes previously declared using macro #DECLARE_ENUM(EnumType,ENUM_DEF)
*/
#define DEFINE_ENUM(EnumType,ENUM_DEF) \
const char* getString(EnumType value) \
#define DEFINE_ENUM(EnumType,ENUM_DEF,Scope) \
const char* Scope::getString(EnumType value) \
{ \
switch(value) \
{ \
Expand All @@ -62,9 +63,10 @@
throw std::range_error(ss.str());; /* handle input error */ \
} \
} \
EnumType get##EnumType##Value(const char* str) \
Scope::EnumType Scope::get##EnumType##Value(const char* str) \
{ \
ENUM_DEF(ENUM_STRCMP) \
throw std::range_error("String " + std::string(str) + " does not match any value"); /* handle input error */ \
}


28 changes: 21 additions & 7 deletions include/ezsp/ezsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,41 @@

#include <ezsp/export.h>
#include <ezsp/config.h>
#include "ezsp/enum-generator.h"
#include <ezsp/gpd.h>
#include <ezsp/zbmessage/green-power-device.h>
#include <ezsp/zbmessage/green-power-frame.h>
#include <spi/TimerBuilder.h>
#include <spi/IUartDriver.h>

namespace NSEZSP {

#define CLIBEZSP_STATE_LIST(XX) \
XX(UNINITIALIZED,=1) /*<! Initial state, before starting. */ \
XX(READY,) /*<! Library is ready to work and process new command */ \
XX(INIT_FAILED,) /*<! Initialisation failed, Library is out of work */ \
XX(SINK_BUSY,) /*<! Enclosed sink is busy executing commands */ \
XX(IN_XMODEM_XFR,) /*<! Adapter is ready to perform a firmware upgrade via X-modem */ \

/**
* @brief Possible states of class CLibEzspMain as visible from the outside (these are much simpler than the real internal states defined in CLibEzspInternalState)
*
* @note The lines above describes all states known in order to build both an enum and enum-to-string/string-to-enum methods
* In this macro, XX is a placeholder for the macro to use for building.
* We start numbering from 1, so that 0 can be understood as value not found for enum-generator.h
* @see enum-generator.h
*/
enum class CLibEzspState {
UNINITIALIZED, /*<! Initial state, before starting. */
READY, /*<! Library is ready to work and process new command */
INIT_FAILED, /*<! Initialisation failed, Library is out of work */
SINK_BUSY, /*<! Enclosed sink is busy executing commands */
IN_XMODEM_XFR, /*<! Adapter is ready to perform a firmware upgrade via X-modem */

class CLibEzspPublic {
public:
DECLARE_ENUM(State, CLIBEZSP_STATE_LIST)
};

typedef CLibEzspPublic::State CLibEzspState; /* Shortcut for access to public state enum */

class CLibEzspMain;

typedef std::function<void (CLibEzspState i_state)> FLibStateCallback; /*!< Callback type for method registerLibraryStateCallback() */
typedef std::function<void (CLibEzspPublic::State i_state)> FLibStateCallback; /*!< Callback type for method registerLibraryStateCallback() */
typedef std::function<void (uint32_t &i_gpd_id, bool i_gpd_known, CGpdKeyStatus i_gpd_key_status)> FGpSourceIdCallback; /*!< Callback type for method registerGPSourceIdCallback() */
typedef std::function<void (CGpFrame &i_gpf)> FGpFrameRecvCallback; /*!< Callback type for method registerGPFrameRecvCallback() */
typedef std::function<void (std::map<uint8_t, int8_t>)> FEnergyScanCallback; /*!< Callback type for method startEnergyScan() */
Expand Down
2 changes: 2 additions & 0 deletions src/ezsp/ezsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "ezsp/ezsp.h"
#include "ezsp/lib-ezsp-main.h"

DEFINE_ENUM(State, CLIBEZSP_STATE_LIST, NSEZSP::CLibEzspPublic)

using NSEZSP::CEzsp;

CEzsp::CEzsp(NSSPI::IUartDriverHandle uartHandle, const NSSPI::TimerBuilder& timerbuilder, unsigned int requestZbNetworkResetToChannel)
Expand Down
Loading

0 comments on commit e1a8f4e

Please sign in to comment.