473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
3 1563
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Georg Brandl | last post by:
Hello, in follow-up to the recent "dictionary accumulator" thread, I wrote a little module with several subclassed dicts. Comments (e.g. makes it sense to use super), corrections, etc.? Is...
5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
1
by: Sinex | last post by:
Hi, I have a webmethod. It takes an int parameter. I want this to get serialized as an XMLAttribute...so I used the XMLAttributeAttribute decoration and it works fine. Now, I want to specify...
6
by: Queez | last post by:
I've had a good look around and no-one seems to have mentioned this, which leads me to believe that I may be missing something simple. Basically, is there a way I can do the following, and if so,...
3
by: Rincevent | last post by:
Hi, I'm trying to process a large number of files, each with a very small size (about 500 chars maximum). I need to retrieve multiple information from each file, and some of these are optional. So...
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.