Can I refer to an "optional module" from my factory?

P

Paramesh

Hello friends,

My friend asked me this question:

This question regards proprietary software (of which I am one of the
developers), so I cannot post actual code for this question. I will try
to use illustrative examples.

I have two libraries for distribution; one is an essential library and
the other is an add-on (providing specialized extensions and
enhancements).

I have a factory class in the first module (lets call it Factory) which
is used to generate instances of another class (lets call it Thing).
Now, Thing itself is an abstract base class, and the first (essential)
library has some derived Things. The add-on library contains some
additional derived Things.

I am wondering whether there is a technique I can use so that the
Factory implementation (which is in the first library) has hooks to
instantiate derived Things from the add-on library, but only if the
sceond library is available. An enumeration is used to identify the
particular type of Thing to be instantiated. Even if I can control the
enumeration so that it contains only the appropriate items for the
available libraries, the user can still potentially use an invalid
value via casting, so I would still need to throw some run-time
exception if the add-on library is not available, with a message like
"feature unavailable: module x not installed".

I was thinking I might be able to do this with carefully chosen
preprocessor directives and macros, but I haven't quite figured out a
way.

Please help me with finding a solution.

Thank you,
Paramesh.
 
Z

Zara

Hello friends,

My friend asked me this question:

This question regards proprietary software (of which I am one of the
developers), so I cannot post actual code for this question. I will try
to use illustrative examples.

I have two libraries for distribution; one is an essential library and
the other is an add-on (providing specialized extensions and
enhancements).

I have a factory class in the first module (lets call it Factory) which
is used to generate instances of another class (lets call it Thing).
Now, Thing itself is an abstract base class, and the first (essential)
library has some derived Things. The add-on library contains some
additional derived Things.

I am wondering whether there is a technique I can use so that the
Factory implementation (which is in the first library) has hooks to
instantiate derived Things from the add-on library, but only if the
sceond library is available. An enumeration is used to identify the
particular type of Thing to be instantiated. Even if I can control the
enumeration so that it contains only the appropriate items for the
available libraries, the user can still potentially use an invalid
value via casting, so I would still need to throw some run-time
exception if the add-on library is not available, with a message like
"feature unavailable: module x not installed".

I was thinking I might be able to do this with carefully chosen
preprocessor directives and macros, but I haven't quite figured out a
way.

Please help me with finding a solution.

Thank you,
Paramesh.


You could have a "register" method in the base library, so that the
secondary library calls it at start, identifying itself. In this way,
tha main library knows from the beginning if the secondary library is
present. It could even be extended to receive "registration" from
other future add-on libraries you may develop.

As always, you must be very careful with these "startup" methods,
because they typically rely on static data initialization, which may
be really tricky.

Regards,
-- Zara
 
M

mlimber

Zara said:
You could have a "register" method in the base library, so that the
secondary library calls it at start, identifying itself. In this way,
tha main library knows from the beginning if the secondary library is
present. It could even be extended to receive "registration" from
other future add-on libraries you may develop.

As always, you must be very careful with these "startup" methods,
because they typically rely on static data initialization, which may
be really tricky.

Regards,
-- Zara

For one implementation of such a factory, see chapter 8 in _Modern C++
Design_, or just download the accompanying Loki library
(sourceforge.net/projects/loki-lib) and check out Factory.h.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top