I am working on a small project of my hobby, where I have a large structure that takes care of all the main tasks. This core will not be by itself, it needs dozens of subsystems that actually do the hard work. Currently, I have written only one subsystem, so itโs still easy to make a difference.
I have many code places where the kernel interacts with subsystems, and I do not want to change the kernel every time I add a new subsystem. My idea was to make it modular.
For a long time I saw something similar in the game engine, where you could create a new console command using a preprocessor macro. That was all you had to do - after compilation it worked instantly in the game.
So, take the game engine as an example for my case. I posted the comments in the code below to make my question more obvious.
My question is: how can I implement a modular system in Objective-C, built at compile time, and does not require changing anything other than the modules themselves?
And now some code
-(void)interpretCommand:(NSString*)command { // Find the space in the command NSRange pos = [command rangeOfString:@" "]; if (pos.length == 0) return; // No space found NSString *theCommand = [command substringToIndex:pos.location]; // !!! Here comes the important code !!! // Get all the available commands - this is what my question is about! NSDictionary *allCommands = nil; // Find the command in the dictionary NSString *foundCommand = [allCommands objectForKey:theCommand]; // Execute the command if (foundCommand != nil) { [[NSClassFromString(foundCommand) new] execute]; } }
I want to add a new command with something like:
REGISTER_COMMAND(MyClassName, "theCommand")
Remember that the code above is not my specific case. In addition, I do not want external modules, they need to be compiled as if they were implemented initially. Objective-C is excellent, just like C ++ or C.
Update
Clarification: I know how to do this with the plist file, but if I selected this, I would just save them in my actual code. I am looking for a C / C ++ / Objective-C solution that allows me to simply add a module with a preprocessor macro.
Update 2
Adding rewards - I would really like some good ideas for this.