pass header name (withoug .h) in class constructor ... and #include

My head is wrapped around the axle once again...

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class SENSORS
{
    private:
        uint8_t m_SensorPin;
        const char * m_LibraryName;  // the "HEADER" part of "HEADER.h"

    public:
        // constructor
        SENSORS(uint8_t pin, const char * 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.
        }
};
Last edited on
I am not sure what you want to do here.

you can do this:

-----------
//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.
Last edited on
Is it even possible

No, not at all.

This is an XY problem. You had an issue, thought of a potential solution to the issue, then asked about that potential solution.

Instead you should have just asked about the issue.
http://xyproblem.info/
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 <>.

[code]
Place code here.
[/code]

Well thanks for that AbstractionAnon. Fixed.... Now about the question...
Last edited on
mbozzi...

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."

Last edited on
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.
Last edited on
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.
Registered users can post here. Sign in or register to post.