I'm attempting to create a new Arduino project using Sloeber (Eclipse). Assume it's a newly-created project whose main entry file is ".\ArduinoMainClass.ino"
Now... assume I want to create a half dozen new classes:
ExecutionState-- a C++ class that's intended to be functionally equivalent to a Java Interface. If you're wondering, I'm attempting to implement the 'State' pattern.
GoodState, BadState, UglyState, WeirdState-- C++ classes that all extend ExecutionState. If they were Java, they'd technically be implementing an interface... but since this is C++ and I'm still taking baby steps, for now ExecutionState is just a class with one method that does nothing, and all of THESE classes override it to do the actual work.
StateContainer-- a C++ class whose only purpose in life is to hold single instances of GoodState, BadState, UglyState, and WeirdState. If this were Java, they'd be Singletons... GoodState (and the others) would have something like:
private static ExecutionState instance = new GoodState();
// ...
public static ExecutionState getInstance() { return instance; }
... but it's obviously not Java, and I'm nowhere close to knowing enough about C++ to properly implement static singletons yet, so for now, I'm just going to create an instance of StateContainer to create and hold those single instances.
So... I now have Sloeber/Eclipse loaded, and I've created my project. If this were Java, I'd create a subdirectory like .\src\java\com\packagename\executionstate and stick the .cpp and .hpp files for ExecutionState, GoodState, BadState, UglyState, WeirdState, and StateContainer in it, and add "include com.packagename.executionstate.*" to it.
- I know C++ namespaces have no direct connection to directory names
- I've gotten the impression that "Arduino"-ish C++ auto-generates the #include files for things it regards as libraries. I have a hunch that I probably want to put all of the classes I mentioned above in a directory somewhere and present them to the IDE as an Arduino "library" so they'll all get implicitly included in one go within the .ino file.
- I'm completely confused about what I need to #include in the classes I mentioned above so they can mutually find each other... and whether those #includes rightfully belong in the .hpp or .cpp files.
So... getting back to the big picture. If my project is in c:\users\moi\Documents\Slober-workspace\FirstSloeberArduinoApp...
Where do the .cpp and .hpp files for ExecutionState, GoodState, BadState, UglyState, WeirdState, and StateContainer go? (Assume I'd be inclined to put them all in a directory named "executionstates" somewhere in the project's directory hierarchy, but I'm not sure where that exact directory should go).
What #includes do I need to put in ExecutionState, Good/Bad/Ugly/WeirdState, and StateContainer... and do they go in the .hpp file, or the .cpp file for their respective class?
What do I have to do so the .ino file can find StateContainer?
In case you're totally confused at this point, my actual .ino file will look something like this:
void setup() {
// executionStates is a global variable declared higher up
executionStates = new StateContainer();
executionState = executionStates.getGoodState();
// StateContainer's constructor itself creates instances of the various subclasses of ExecutionState,
// and passes references to itself to their constructors so they can
// get the references to the others states as they need to return them.
}
void loop() {
executionState = executionState->doSomething();
/* executionState holds a pointer to an instance of an ExecutionState subclass.
Each time doSomething() executes, it returns a pointer to its new state
*/
}
... and in reality, there will be dozens of subclasses of ExecutionState, each representing a unique execution state.