By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,873 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,873 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 4 '05 #1
Share this Question
Share on Google+
3 Replies


P: 3
A common solution to this is to handle the loading of the shared object library yourself after the application has started.

Some configuration file specifies which shared object libraries are available, or the code looks in a particular directory and tryies to load the libraries there.

You define a specific entry point that you ask the shared library for. This function registers constructor function (factory methods or whatever) with your main Factory.

HTH
Nov 4 '05 #2

P: n/a
On 3 Nov 2005 23:07:22 -0800, "Paramesh" <ce*********@gmail.com>
wrote:
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
Nov 4 '05 #3

P: n/a

Zara wrote:
On 3 Nov 2005 23:07:22 -0800, "Paramesh" <ce*********@gmail.com>
wrote:
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


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

Nov 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.