Create a lookup table for array of classes in C++/Arduino

  arduino-esp32, arduinojson, arrays, c++, class

I have multiple types of classes.
Each type has an array and an index in the array.

If an outside function knows only a string ID of a class and wants to use it’s public function,
it must search for that particular class by ID in it’s array.

This is really inefficient.
All my classes created at runtime and a function which creates it, puts it into an array.

I want to create a lookup table of some sort for this when the classes are created, so any outside
function if wants to use one class it does not have to for loop on the class’s array and check each ID if it matches but to be able to reach the class by some struct or array.

Here how it is done now:

#define MAX_ONE_TYPES 20
int createdOneTypesCounter = 0;

// Create one type of classes in one for loop and put it into an array.
// We must keep track of the index because a class can be created later
// at runtime so we must keep increasing the index. I don't check for max index now...

// oneTypes is a JSON OBJECT
for (JsonPair oneTypeRef: oneTypes) {
   const char* oneTypeID     = oneTypeRef.key().c_str();
   JsonObject oneTypeOptions = oneTypes[oneTypeID];
   oneTypeClasses[createdOneTypesCounter ] = new oneTypeClass(oneTypeOptions);
   createdOneTypesCounter++;
}

class oneTypeClass{
    private:
       // using an external ram for this kinda stuffs.
       const size_t IDS_AND_NAMES_SIZE = 500;
       const char * id  = (char *) ps_malloc (IDS_AND_NAMES_SIZE * sizeof (char));
    public:
     thermHandler(JsonObject options){
       // She got an ID on creation.
       id  = strdup(options["id"]);
     }
     
     void setModes(boolean mode){
        // set some mode...
     }
     boolean isMyID(const char* packetID){
        if( strcmp(id, packetID) == 0 ){return true;}
        return false;
     }
};
oneTypeClass* oneTypeClasses[MAX_ONE_TYPES] EXT_RAM_ATTR;

// Here comes an outside function. Wants to set a boolean in a class with specific ID.

static const inline void setOneTypeMode(JsonObject packet){
    for(int i = 0; i < MAX_ONE_TYPES; i++){
        if(oneTypeClasses[i] != NULL && oneTypeClasses[i]->isMyID(packet["id"])){
            oneTypeClasses[i]->setModes(packet["mode"]);
            break;
        }
    }
}

And here is my problem. I must search for a class by ID every time some outside function wants to do something with one of the classes.

I don’t know how would i do it.
In JS i would create an object for a lookup table and every time a class is created i would put it’s ID for the key and it’s index to the value like this:

var oneTypeClass_Lookup{
 "CLASS ID STRING" : "CLASS INDEX IN ARRAY"
};

//And a function would do it like this:

static const inline void setOneTypeMode(JsonObject packet){
   int myClassIndex = oneTypeClass_Lookup[ packet["id"] ];
   oneTypeClasses[myClassIndex]->setModes(packet["mode"]);
}

I’m doing this for "mass operation":

static const inline int getOneTypeClassIndex(const char* packetID){
    for(int i = 0; i < MAX_THERMS; i++){
        if(oneTypeClasses[i] != NULL && oneTypeClasses[i]->isMyID(packetID)){
            return i;
        }
    }
    return -1;
}

static const inline void setThing(int newThing, const char* packetID){
    int index = getOneTypeClassIndex(packetID);
    if( index > -1 ){
        oneTypeClasses[index]->setNewThing(newThing);
    }
}

static const inline void setThing_Two(int newThing, const char* packetID){
    int index = getOneTypeClassIndex(packetID);
    if( index > -1 ){
        oneTypeClasses[index]->setNewThing(newThing);
    }
}

But i can’t do this in C or Arduino C++. I hope i was clear.

UI: Class id consist of numbers and characrers. The id can never start with a number. Example: "v_kw62ffss_xg0syjlvrokbxciv65a8y"

Source: Windows Questions C++

LEAVE A COMMENT