views:

106

answers:

2

See, for example, here

http://stackoverflow.com/questions/139299/difference-between-dependency-injection-di-inversion-of-control-ioc

to remind yourself what IoC and DI are.

The question and answer here

http://stackoverflow.com/questions/820594/is-inversion-of-control-specific-to-oo-languages

suggests that it does not require an OO language.

Now, my question: Anyone doing this in C?

I am asking because we write embedded C and are considering applying these methods, without changing our programming language.

A: 

On windows, you have COM which does something similar. You have an interface and provide an implementation in a DLL. You register the DLL and that process of registration makes an entry in the registry mapping the interface (UUID) and the DLL which provides the implementation. Based on this information, when you execute QueryInterface(), the COM service will load the corresponding DLL and create an instance of the implementation object, typecast it to the requested interface type and return.

This is IoC using COM. Since COM is implemented in 'C', I am sure it is just working out the details to get this working on your embedded system. Instead of registry, you will need a file to store that mapping between interface, implementation and DLL. This is done in Catia (from Dassault Systemes) in their CNext (V5/V6) architecture. It is called the Object modeler framework.

Steps to achieve this:

  1. Define a naming convention for function that returns a pointer to an interface
  2. Create a file with interface and DLL in which it is implemented
  3. Implement the interface in a DLL and update the file in #2
  4. In the main code, read the file and create a map of interface and DLL
  5. When you need an interface, load the DLL if not loaded and get the address of the function that returns the pointer to interface (based on defined naming convention) Ex: For IDoSomething interface, your function might be Get_IDoSomething(). Since we get the address of function based on name, it is done at runtime and not at compile time.
  6. Invoke the function at the address you get from #5. You now have a pointer to the interface based on the implementation in DLL as specified in #3

You therefore tie the interface to its implementation at runtime.

hackworks
+1  A: 

Doing it in C all the time. The hint is given in the answer from Azder in your second link:

"When you give a Windows API function a pointer to a callback function, you give them the control of calling your function with their own parameters."

From this point of view, the concept is already used in the Standard library for the functions qsort() and bsearch().

Secure