I am designing a system where I need to pass the name of a header file (a driver...) in a class constructor and then do an #include of the header AND initiate the driver... something like this pseudo code:
class SENSORS
{
private:
uint8_t m_SensorPin;
constchar * m_LibraryName; // the "HEADER" part of "HEADER.h"
public:
// constructor
SENSORS(uint8_t pin, constchar * library):
m_SensorPin{pin}, m_LibraryName{*library}
{
#include "m_LibraryName + .h"
// in other words, take the libraryname that was passed, and add ".h" to the end of it,
// then do an include so if "LED_Driver" was passed, we want
// "#include LED_Driver.h"
// to be invoked here
// Is it even possible to do an include within a class method?
// NEXT...
m_libraryName m_libraryName + _ + this;
// in other words, create an instance of libraryName named LibraryName +
// the name of the SENSOR...
// so in a construction of the SENSORS called LED1, where we pass the
// libraryName as LED_Driver
// ,
// SENSORS LED1(8, LED_Driver); we would get
// "LED_Driver LED_Driver_LED1;" as the effective command here.
}
};
-----------
//gibberish.h
string myfilename(){return (string)(__FILE__);}
//you will need to parse it to get the meat out, since it is path/path2/.../filename.h format.
//probably a find() on ".h" will do it if you are not insane and having folders with extensions like .h
-----------
but that just gets you a header file name.
are you trying to make some sort of class factory? There are patterns for doing that, though I find it a bit dated. I would think you can do an interface/inheritance the other direction and be better off (??) for most things a factory would do.
Please learn to use code tags correctly. After 17 posts you should know how to do so.
On this site, they use square brackets [], not angle brackets <>.
was "I am designing a system where I need to pass the name of a header file (a driver...) in a class constructor and then do an #include of the header AND initiate the driver." Not a clear enough "X"?
I know exactly what I want to do, just not how to do it. I am not asking, nor will I ask, for anyone to design my whole system. I just want to see if I can accomplish exactly what I stated above: "I am designing a system where I need to pass the name of a header file (a driver...) in a class constructor and then do an #include of the header AND initiate the driver."
and the answer is, you can't really do that.
If the initialize function had the same name for all the drivers, you can sort of do it with macros or similar smoke and mirrors, even macro conditionals (eg #ifdef thisdriver #include thisdriver.h # elseif otherdriver #include otherdriver.h ... all chained up.
even if the init function is different, the same idea can be applied for an even uglier
#ifdef ... someinit(someparam) #elseif ... otherinit(par1, par2) ... all chained up.
That is no way to code. Its been done, but its horrible; surely there is a better way if you look at OOP... as I said above, this looks like you are coming at it backwards and if you did it the other way around some sort of inheritance should solve this in a sensible way.
and the macro stuff is all at compile time, not run time. To do it at run time, you have a rather awful build of each thing into its own library and load the one you want dynamically will work, but again, not exactly a clean idea.
It sounds like what you want is to choose between a set of interchangeable software components while the program is running.
There are many ways to do this but none of the reasonable solutions have anything to do with including different header files based on the argument to some function. After all, #includes are handled by the C preprocessor, who's work is done long before the compiler gets your code; and the compiler's work is done long before your code ever executes.
jonnin and mbozzi,
Thanks for thinking about it. The point is clear that #includes are preprocessor commands and that seems to preclude passing them in class constructors. I can use @ifdefs in some configuration files to accomplish my ends, just not as "elegant" as my imagined solution.
Cheers.