0

I'm using library ArduinoJson and I need to create a function which will open a file on SD card, deserialize JSON and then calls a mapping function which will map deserialized values into a struct. I need one of the function parameters be a maping function but not sure how to do that. This does not compile:

#include <ArduinoJson.h>

// This is an attempt to define a type of function that accepts StaticJsonDocument as a parameter
// and it does not compile here.
typedef void mappingFunctionType(StaticJsonDocument);

class ConfigurationLoader {
  private:
    void _loadConfigFile(String filePath, mappingFunctionType mappingFunction)
    void _loadAppConfig();
}

The message error is:

In file included from sketch\RealTime.h:13:0,
                 from sketch\RealTime.cpp:1:
Config.h:22:52: error: typedef 'mappingFunctionType' is initialized (use decltype instead)
 typedef void mappingFunctionType(StaticJsonDocument);

The mapping function should be called like this:

void _loadAppConfig() {
  _deserializeJson(WIFI_CONFIG_FILEPATH, []() -> {
    // This is a mapping function that maps deserialiyed values to a struct
    config.interval = doc["interval"];
  });
}

Please advise. Thank you!

Vojtech
  • 2,533
  • 9
  • 34
  • 65
  • 1
    Your typedef is incorrect. The linked question has an example of a syntactically correct typedef.for a function pointer. – Stephen Newell Sep 25 '20 at 12:47
  • 1
    Also useful reading: [How do I typedef a function pointer with the C++11 using syntax?](https://stackoverflow.com/questions/16498969) – user4581301 Sep 25 '20 at 12:49

1 Answers1

0

What you actually want is not the type of a function, but the type of a function pointer.

So your typedef would look like this:

typedef void (*mappingFunctionPtrType)(StaticJsonDocument);

You then can use it like so:

void foo(mappingFunctionPtrType func)
{
    StaticJsonDocument doc;
    func(doc); //call the function through its pointer
}
perivesta
  • 3,417
  • 1
  • 10
  • 25
  • Thank you for the reply! Shouldn't the asterisk be at the beginning? Asterisk at the end gives this error: In file included from sketch\RealTime.h:13:0, from sketch\RealTime.cpp:1: Config.h:22:37: error: expected ')' before '*' token typedef void (mappingFunctionPtrType*)(StaticJsonDocument); If I move asterisks at the beginning then it gives the same error as in my question. But if I replace StaticJsonDocument with for example int it compiles. There seems to be something wrong with the way I use StaticJsonDocument. – Vojtech Sep 25 '20 at 15:04
  • Ah yes, the * goes at the beginning, sorry for the typo. The other error is then related to your StaticJsonDocument. With a trivial class it works: https://godbolt.org/z/Tvd4er – perivesta Sep 25 '20 at 15:10
  • You're right, class StaticJsonDocument requires a template parameter, for example 'StaticJsonDocument<1024>'. When I added the template parameter it compiled. I appreciate the example in the Compile Explorer, thank you! – Vojtech Sep 25 '20 at 15:42