473,322 Members | 1,699 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,322 software developers and data experts.

library design

Hi,

I have a possibly stupid question about library design. Suppose you have a
module A with function fa_1(...), ..., fa_n(...) and two modules B1 and B2
with different implementations of some functions fb_1(...), ..., fb_m(...),
i.e. B1 and B2 share the same interface but have different implementations.
Module A should use either module B1 or module B2 to accomplish its task.
In C++ (or Java or whatever) I would propably create an abstract base class
B

class B {
public:
virtual void fb_1(...) = 0;
...
virtual void fb_m(...) = 0;
};

and derive two subclasses B1 and B2 from with different implementations.
Module A would be given a pointer two an instance of B

class A {
public:
void set_B(B* b);
...
};

and so on.

A direct translation to C could be a struct B holding function pointers,
i.e.

struct B {
void (*fb_1)(...);
...
void (*fb_m)(...);
};

and then create a struct with the function pointers pointing to the
functions of B1 or B2. The module functions of A would the call B1 or B2
with a construct like

struct B* b;
....
b->fb_1(b, ...);

So far so good, nothing new. With the above approach I there're many lines
in A like

a->b->fb_1(a->b, ...)

to call functions from B1 or B2. Furthermore, A and B both have contain a
large number of functions (so not only one function pointer). This seems
to be pretty ugly.

My question is: Are there better ways to organize a library where a module A
can use either functions from B1 or B2? All modules implementing the
interface B are known at compile-time, i.e. it should not be possible to
link against the library and create new implementations of B. But of
course, the library itself should be extensible in the sense that I want to
provide 3 or 4 implementations of B as part of the library in future. Is
there a way not to mimic the approach of C++ (with abstract base class and
so on) using structs with function pointers? How could the library and the
modules be organized to get the desired functionality? Or is the approach
described above *the* standard way to do this?

Thanks in advance!

Frank

Jun 27 '08 #1
5 1651
Frank Fischer wrote:
Hi,

I have a possibly stupid question about library design. Suppose you
have a module A with function fa_1(...), ..., fa_n(...) and two
modules B1 and B2 with different implementations of some functions
fb_1(...), ..., fb_m(...), i.e. B1 and B2 share the same interface
but have different implementations. Module A should use either module
B1 or module B2 to accomplish its task. In C++ (or Java or whatever)
I would propably create an abstract base class B

class B {
public:
virtual void fb_1(...) = 0;
...
virtual void fb_m(...) = 0;
};
Wrong group C doesn't have classes or virtual functions, comp.lang.c++ is
down the hall

Bye, Jojo
Jun 27 '08 #2
Frank Fischer wrote:
>
A direct translation to C could be a struct B holding function pointers,
i.e.

struct B {
void (*fb_1)(...);
...
void (*fb_m)(...);
};

and then create a struct with the function pointers pointing to the
functions of B1 or B2. The module functions of A would the call B1 or B2
with a construct like

struct B* b;
....
b->fb_1(b, ...);

So far so good, nothing new. With the above approach I there're many lines
in A like

a->b->fb_1(a->b, ...)

to call functions from B1 or B2. Furthermore, A and B both have contain a
large number of functions (so not only one function pointer). This seems
to be pretty ugly.
It can, but it's a common approach. Use of intermediate variables can
make the code a lot cleaner (use a B* variable for a->b).

--
Ian Collins.
Jun 27 '08 #3
Joachim Schmitz wrote:
Frank Fischer wrote:
>Hi,

I have a possibly stupid question about library design.
Wrong group C doesn't have classes or virtual functions, comp.lang.c++ is
down the hall
You should have read the full post before getting lathered up about C++.

--
Ian Collins.
Jun 27 '08 #4
Ian Collins wrote:
Joachim Schmitz wrote:
>Frank Fischer wrote:
>>Hi,

I have a possibly stupid question about library design.
>Wrong group C doesn't have classes or virtual functions,
comp.lang.c++ is down the hall

You should have read the full post before getting lathered up about
C++.
Oh, yes, apologies to the OP!

Bye, Jojo
Jun 27 '08 #5
On 20 Jun 2008, 07:41:21 UTC, Frank Fischer declared to all in comp.lang.c:
I have a possibly stupid question about library design. Suppose you have a
module A with function fa_1(...), ..., fa_n(...) and two modules B1 and B2
with different implementations of some functions fb_1(...), ..., fb_m(...),
i.e. B1 and B2 share the same interface but have different implementations.
Module A should use either module B1 or module B2 to accomplish its task.
In C++ (or Java or whatever) I would propably create an abstract base class
B
....
My question is: Are there better ways to organize a library where a module A
can use either functions from B1 or B2?
Is the choice of using B1 or B2 being made at runtime or compile time? If it's
at compile time, A can be written without regard for which implementation is
used (the interfaces are *exactly* the same, correct?), and the desired B
module can then be linked with A and the rest of the code when the program is
compiled. I don't know of any compilers that would have a problem with this.

On the other hand, if the choice is indeed being made at run-time, then your
solution is the only really viable one.

I hope this helps,
-- Minimiscience

--
I sense something stupid brewing.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. In this user control we have a class...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
1
by: Ezmeralda | last post by:
Hello, I need to an TCP/IP Interface for communication with an embedded device. Since I have two different design ideas in mind, I am wondering whether you could give me some hints to decide:...
6
by: ivan.leben | last post by:
Hi! I know this question is more suitable for gnu.utils group but im posting here, since that group looks almost dead (last post 1 May with no reply yet) and I need the answer much faster. I...
1
by: rich_sposato | last post by:
I released version 2.0 of C++ Unit Test Library. You can download it from SourceForget.Net at http://sourceforge.net/projects/cppunittest/ .. I wrote this unit test library because other unit...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
3
by: djbaker | last post by:
Greetings, I am trying to create a class library/dll with C++ and I am running into a lot of trouble. I know that this is a C++ forum and not one specific to windows or VS 2005 but I'm hoping...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.