programming theory question regarding optional inclusion of data [SOLVED]

Hi

Bit of a mouthful of a title :slight_smile: , i had trouble researching this one with internet searches so i thought i would ask.
I have written a library for a OLED.
It includes 3 options for 3 different fonts.
I would like the user to be able to select the font they want and at compile time
the program would only include that font in the program.
The object of the exercise being to keep the program as small as possible.

I will show the program structure in basic pseudo code here,

//files 
FONT_1.h
FONT_2.h
FONT_3.h
libray.h
library.cpp

// library.cpp
INCLUDE the headers and fonts

function Print_To_OLED
{
switch(font_selection)
case 1 ref to FONT_1
case 2 ref to FONT_2
case 3 ref to FONT_3
}

Now the problem is if i do not include all the FONT at some point, the "ref to the font" in the cpp will produce an error at compile error: 'ref to FONT_X not declared in this scope. where X is missing font number.

The basic question is how do you optionally include data in a library
without causing reference errors to missing data types.
I know i can do by splitting the library.h and .cpp file up into different files and have the user include the one they want.
but i was hoping to keep in all together, i did this on another library and i ended with up with a dozen files

Again the object of the exercise is to keep program as small as possible for given user requirements.

Regards

The two possibilities that come to mind are Pre-Processor Directives or Template Specialization.

You can use #define and conditional compilation.

#define FONT_1
#define FONT_2
#define FONT_3

#ifdef FONT_1
#include "FONT_1.h"
#endif
#ifdef FONT_2
#include "FONT_2.h"
#endif
#ifdef FONT_3
#include "FONT_3.h"
#endif

switch(font_selection)
{
#ifdef FONT_1
  case 1:
     // FONT_1 code here
     break;
#endif

#ifdef FONT_2
  case 2:
     // FONT_2 code here
     break;
#endif

#ifdef FONT_3
  case 3:
     // FONT_3 code here
     break;
#endif

  default:
      // Unsupported font error handling code
}

Keep in mind conditional compilation can really clutter up code if you’re not careful.

All reachable code at rune time must be included.

The only way to exclude code is via: preprocessor directive.

gfvalvo:
The two possibilities that come to mind are Pre-Processor Directives or Template Specialization.

I don't think template would be helpful here. Template more or less a compiling time thing.

ToddL1962:
You can use #define and conditional compilation.

#define FONT_1

#define FONT_2
#define FONT_3

#ifdef FONT_1
#include “FONT_1.h”
#endif
#ifdef FONT_2
#include “FONT_2.h”
#endif
#ifdef FONT_3
#include “FONT_3.h”
#endif

switch(font_selection)
{
#ifdef FONT_1
  case 1:
    // FONT_1 code here
    break;
#endif

#ifdef FONT_2
  case 2:
    // FONT_2 code here
    break;
#endif

#ifdef FONT_3
  case 3:
    // FONT_3 code here
    break;
#endif

default:
      // Unsupported font error handling code
}




Keep in mind conditional compilation can really clutter up code if you're not careful.

Thanks for help , I have it working(compiling) now as I wanted as per your code, I still have to check it on the breadboard but its looks good.
There is only one reference to the Fonts in a central function in the .cpp file so its not too cluttered.

typematrix:
Thanks for help , I have it working(compiling) now as I wanted as per your code, I still have to check it on the breadboard but its looks good.
There is only one reference to the Fonts in a central function in the .cpp file so its not too cluttered.

Glad it worked for you!