1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#ifndef MODEL_H
#define MODEL_H
#define maxNumberOfColumns 100
#define maxNumberOfRows 100
// from the standard library
#include <string> // std::string string.find string.find_last_of string.substr
#include <mutex> // std::mutex, std::unique_lock, std::condition_variable
// from the project files
#include "MyErrorCodes.h" // definition of the available errorcodes
#include "Dll_interface.h" // typedef of the callback functions
/** \brief The Model class contains all data and is responsible for access to the data
*/
class Model
{
public:
/** \brief Function to get a pointer to the Model object
*
* To ensure that the model is the same for all components using it, it is important to access the model using:
* Model::instance()->someFunction instead of creating an instance of the Model class (because if you creae an
* instance of the Model there may also be multiple instances containing different values.
*
* \return a pointer to the unique Model object.
*/
static Model* instance();
/** \brief Initialize sets all variables to their default value
* Before calling this functions, values may be un-initialized/un-defined.
* After calling this function, all other functions can be called safely.
*
* \return 0 upon success or an error value upon failure
*/
int initialize();
/** \brief Terminate clears all variables and objects kept in memory
* Calling a function in the DLL after calling this function may result in un-defined behavior or a crash
*
* \return 0 upon success or an error value upon failure
*/
int terminate();
/** \brief Function to get the version of the DLL as a character array
*
* \param char* pointer to the array where the version string should be stored
* \param int with the capacity of the buffer pointed to by the first argument. After execution, this value will indicate the length of the character string.
*
* \return 0 upon success or an error value upon failure
*/
int getDllVersion(char* version, int versionCapacity);
/** \brief Function to request changing a value
*
* This function requests the change of a value . Upon success, the state will be changed and the
* OnValueChanged event handler will be called with the new value information.
*
* \param int that indicates which value to change
* \param int that indicates which value to change
* \param int with the number of the desired state
* \return 0 upon success or an error value upon failure
*/
int setValue(int argument_1, int argument_2, int newValue);
/** \brief Function to get the current value of a specific variable
*
* \param int that indicates which value to change
* \param int that indicates which value to change
* \param int* pointer to the variable where the current value should be stored.
* \return 0 upon success or an error value upon failure
*/
int getValue(int argument_1, int argument_2, int* currentValue);
/// Functions called when a callback function should be executed
void sendErrorValueChangedEvent(int errorCode, std::string source, std::string message);
void sendValueChangedEvent(int collumn, int row, int value);
/// Register callback functions
/** \brief Callback to obtain information about the configuration after the initialize function has been completed.
*
* \param OnConfigurationReceived the function to execute when the configuration is loaded.
*
* \return 0 upon success or an error value upon failure
*/
void setCallbackOnConfigurationReceived(OnConfigurationReceived handler);
/** \brief Callback to get notified if an error occurs.
*
* \param eventHandler with three arguments, an error code, a buffer for information about where the error was detected and a buffer for the error message.
* \return 0 upon success or an error value upon failure
*/
void setCallbackOnErrorValueChanged(OnErrorValueChanged handler);
/** \brief Function to add an event handler to get a callback if a value changes (any value)
*
* \param eventHandler with three arguments, the first two indicate which value was changed, the third argument contains the new value
* \return 0 upon success or an error value upon failure
*/
void setCallbackOnValueChanged(OnValueChanged handler);
protected:
// Prevent calling the constructor directly
Model();
// Prevent calling the destructor on a singleton class
virtual ~Model();
private:
// Member variables
std::string configurationString;
std::mutex valuesMutex;
int values[maxNumberOfColumns][maxNumberOfRows];
static Model* mModel;
// Helper function to safely copy a string into a provided buffer
int getSafeString(const char* result, char* buffer, int buffLength);
// Places to store the callback functions.
void (*theConfigurationReceivedHandler)(const char*);
void (*theOnErrorValueChangedHandler)(int errorValue, const char* errorSource, const char* errorMessage);
void (*theOnValueChangedHandler)(int, int, int);
};
#endif // MODEL_H
|