Hi everybody,
If I write code I highly prefer easy to understand code over most compact, most elegant or fastest.
For my USB-keyboard-emulator-project I intend to write a library for the german keyboard-layout.
The interfacing functions shall work as a drop-in replacement like the original keyboard-library.
for internally used functions I want to rewrite the code in a way that makes the code easier to understand for newcomers.
Under this aspect the ideally code would work this way
Sending keystrokes over USB needs to setup a structured variable like this
typedef struct
{
uint8_t modifiers;
uint8_t reserved;
uint8_t keys[6];
} KeyReport;
The input to this transformation is a single character like "a", "b" "@", "K" or "[" etc.
the output is a correct set of key and modifier example for "@"
key = 'q';
modifier = Ctrl + Alt;
as a generalised example
typedef struct
{
int myIntegerVar;
long myLongVar;
char myShortName[6];
} myDataSet_t;
in programming-languages like Pascal/Delphi (where I'm coming from)
functions could return any datatype even the most nested version of structs of strucs of arrays
If I understand right in C++ the following definition of a function is
not
possible:
myDataSet_t myFunc(byte myInputVar) {
myDataSet_t myLocalDataSet ;
myLocalDataSet.myIntegerVar = myInputVar * 100;
myLocalDataSet.myLongVar = myInputVar * 50000;
myLocalDataSet.myShortName = "XY1234";
return myLocalDataSet;
}
myDataSet_t myDataSetVar;
myDataSetVar = myFunc(12);
in Delphi you would code it this way with no problems at all.
assigning a return-"value" (or in this case a structured variable)
to a variable of the same type.
In C++ it has to be coded in a different way.
What would be a way that - for newcomers - is easy to understand?
I want to exclude pointers as "easy to understand"
Of course you could code assign all values to a global defined variable
This would require an own function for every globally defined variable.
next best thing would be to pass the structured variable as a parameter
void myFunc(myDataSet_t &myParameter, byte myInputVar) {
myParameter.myIntegerVar = myInputVar * 100;
myParameter .myLongVar = myInputVar * 50000;
myParameter .myShortName = "XY1234";
}
not possible too (if I understand C++ right so far)
So which way would come close to this version
myDataSet_t mySecondDataSetVar;
mySecondDataSetVar = myFunc(12);
AND
is easy to understand for newcomers?
best regards Stefan