sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
Other
230 stars 124 forks source link

Separation of declaration and definition (only header files) #72

Closed EnduIf closed 4 years ago

EnduIf commented 4 years ago

Hello,

I want to help a Friend with some home automation and at the same time encourage him to write good code. But because of the not existing separation of declaration and definition of your library multible imports of SinricPro are not possible and so its hard to outsource code into other files that need declarations of SinricPro.

Now i'm wondering why everything has been put into header files and if i am overlooking something that still allows to include SinricPro twice.

sivar2311 commented 4 years ago

We ran into a lot of compiling problems when we tried to seperate. Thats why we keept it the easy way.

But, why do you need multiple includes? This makes no sense to me. SinricPro is designed to run multiple devices (simultaneusly)with just one instance of SinricProClass.

For very special situations, you can run multiple instances of the SinricProClass. But again: this is not necessary to run multiple devices.

EnduIf commented 4 years ago

I don't want to have multible instances of the SinricProClass. I want the class declaration of the SinricProClass so that i am able to pass an instance of the SinricProClass to function for example. But you can't import SinricPro twice for the declaration because of the definitions in the header file.

sivar2311 commented 4 years ago

Yeah, that's right. The SinricProClass is rather designed as a global object. A transfer as function parameter was not intended. May I ask what exactly do you intend to do?

As a work-around: The SinricProClass inherits from the SinricProInterface. You can extend the interface by the required methods of the SinricProClass and pass the interface class as function parameter. Maybe this is an option for you?

EnduIf commented 4 years ago

I just want to create some custom classes in other files and pass an instance of the SinricProClass to those classes. But because of that I also need the declaration of the SinricProClass in those files where the classes are.

sivar2311 commented 4 years ago

Ok, got it. For this you can write a simple "include wrapper":

//sp_include_wrapper.h
#ifndef _sp_include_wrapper_h_
#define _sp_include_wrapper_h_

#include "SinricPro.h"

#endif

Now you can include this sp_include_wrapper.h to the main file and your extra files like this:

//test.h
#ifndef _test_h_
#define _test_h_

#include "sp_include_wrapper.h"

void test(SinricProClass &sp) {
  unsigned long timestamp = sp.getTimestamp();
}

#endif
//main.cpp
include <Arduino.h>
#include "sp_include_wrapper.h"
#include "SinricProSwitch.h"
#include "test.h"
...
sivar2311 commented 4 years ago

Did this work for you?

EnduIf commented 4 years ago

Yes thank you.