473,396 Members | 2,076 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,396 software developers and data experts.

generalizing template code

Hello folks,
This question is a bit involved, so I am posting the code on the web,
instead of cutting and pasting it on the email.
http://www.cis.ohio-state.edu/~dubinski/Code/
I think the code is pretty clear, just there's a lot of it, so
I'll try to guide you thru it.

Here's the layout. There are 5 packages / namespaces:
Updater, Energizer, Converter, Proposal, and finally Diffuser

Energizer, Converter, and Updater are independent.
Proposal knows about all three of them, and Diffuser
knows about all other packages.
Start looking with Diffuser.

In DiffuserExt.h you can see the declarations for
classes APrime and MuCenter. You should notice that
the data memebers look similar, but are actually of
different template instantiations. Similarly in
DiffuserSF.h you will see the declarations for classes
Param, and Coeff, which have the same property.

Then if you look at the definitions for the
constructors of these classes (in DiffuserSF.cpp and DiffuserExt.cpp),
you'll see they're
almost identical in the feel and look.

And this is what I'm trying to take care of. I want to
be able to unify these classes "under one roof". I want
the diffuser interface to have 3 members: _prop,
_conv, and _upd. It's just that these are different
template instantiations, and they don't conform to the
same interface. I would like them to, but I don't see
how.

This becomes especially clear when you try to unify
Converters (see files TinyConverter.h and
Converters.h). Because of the templates, I don't see
how to make them conform to the same interface and
treat them uniformly. This is also begging for some creative design pattern,
but I am afraid I need handle the issue with the templates first.

Well, I hope this makes sense.
Does anybody see a nice solution here?

Alex

Jul 19 '05 #1
3 1884
"Aleksandr Dubinskiy" <du*********@osu.edu> wrote...
This question is a bit involved, so I am posting the code on the web,
instead of cutting and pasting it on the email.
http://www.cis.ohio-state.edu/~dubinski/Code/
I think the code is pretty clear, just there's a lot of it, so
I'll try to guide you thru it.

Here's the layout. There are 5 packages / namespaces:
Updater, Energizer, Converter, Proposal, and finally Diffuser

Energizer, Converter, and Updater are independent.
Proposal knows about all three of them, and Diffuser
knows about all other packages.
Start looking with Diffuser.

In DiffuserExt.h you can see the declarations for
classes APrime and MuCenter. You should notice that
the data memebers look similar, but are actually of
different template instantiations. Similarly in
DiffuserSF.h you will see the declarations for classes
Param, and Coeff, which have the same property.

Then if you look at the definitions for the
constructors of these classes (in DiffuserSF.cpp and DiffuserExt.cpp),
you'll see they're
almost identical in the feel and look.

And this is what I'm trying to take care of. I want to
be able to unify these classes "under one roof". I want
the diffuser interface to have 3 members: _prop,
_conv, and _upd. It's just that these are different
template instantiations, and they don't conform to the
same interface. I would like them to, but I don't see
how.

This becomes especially clear when you try to unify
Converters (see files TinyConverter.h and
Converters.h). Because of the templates, I don't see
how to make them conform to the same interface and
treat them uniformly. This is also begging for some creative design pattern, but I am afraid I need handle the issue with the templates first.

Well, I hope this makes sense.
Does anybody see a nice solution here?


I don't. But I looked at your code and (a) it's pretty convoluted,
you could at least try to straighten it up and (b) here is a couple
suggestions:
---------------------------------------------- in DiffuserExt.h
template<class P, class C, class U>
class PI : public PatronInterface
{
P* prop;
C* conv;
U* upd;
public:
PI(Patron::PInstance* other);
virtual void diffuse(double, EnergyGetter*);
};

typedef PI<Proposal::ProposalABCD,
Converter::TinyToABCD,
Updater::AprimeUpdater> APrime;

typedef PI<Proposal::ProposalGridWrap,
Converter::TinyTo1D<Wrap>,
Updater::MuCenterUpdater> MuCenter;
--------------------------------------------------- in DiffuserSF.h
template<class P, class C, class U>
class SFPI : public SFParticularInterface
{
P* prop;
C* conv;
U* upd;
public:
SFPI(BaseLevel::SFDynamic*);
virtual void diffuse_one(double, EnergyGetter*, int);
};

typedef SFPI<Proposal::ProposalGridMSPMusOnly,
Converter::TinyToTripletL,
Updater::ParamUpdater> MusOnly;

typedef SFPI<Proposal::ProposalGridMSP,
Converter::TinyToTripletL,
Updater::ParamUpdater> Param;

typedef SFPI<Proposal::ProposalABCD,
Converter::TinyToABCD,
Updater::CoeffUpdater> Coeff;
----------------------------------------------------------------
Is that what you meant?

I am not going got comment on your code much, but one thing I'd
like to suggest: do not use leading underscores. They do not make
the code more readable. Also, unless parameter names carry any
significance, leave them out of the function declaration.

If you have more particular questions, do ask.

Victor
Jul 19 '05 #2
"Aleksandr Dubinskiy" <du*********@osu.edu> wrote...
Thanks Victor,

Well, your template solution certainly helps to reduce the amount of code.
:-) That's what the templates are for...
By the way the constructors for classes MuCenter and APrime are different,
so don't I need to keep them as classes instead of using just using
typedefs? Or is there a way to specify a constructor for each particular
typedef?
No, there probably isn't any way. Unless you specialise the
constructors (and not the rest of the classes) or figure out
a way to extract the difference and execute it outside the
constructor...
But mostly I'm looking for a solution, which would make them APrime and
MuCenter interchangeable at runtime?
Not sure what you mean by that. Two classes, both deriving
from the same base class -- you could probably use polymorphism
here, no?
What I really want to be able to do is
to specify subclasses from the outside like this:

class PartonInterface {
PartonInterface(
Proposal::ProposalTemplate* p,
Converter::TinyConverter* c,
Updater::StateUpdater* u) {
prop=p; conv=c; upd=u;
}
}
Perhaps you need to merge your multiple Proposals, Converters,
Updaters, into their own hierarchies and also employ polymorphism
there...

Of course this won't work, since all of the interfaces (p,c, u) are
templates, but I'm looking for a workaround, perhaps some kind of boost::any package or void pointers.


Well, without knowing your model, I don't think I can recommend
you any particular solution. Take a look at the "Modern C++ Design"
by Alexandrescu. And read about "Policy-based design" or "Policy-
based programming".

Victor
Jul 19 '05 #3
> Not sure what you mean by that. Two classes, both deriving
from the same base class -- you could probably use polymorphism
here, no?
Yes, I'm already using polymorphism
Perhaps you need to merge your multiple Proposals, Converters,
Updaters, into their own hierarchies and also employ polymorphism
there...

It was already polymorphic.

I think I've come to a satisfactory solution in my code. In case you're
curious, the updated files are up on. Further input on design and style is
appreciated.

http://www.cis.ohio-state.edu/~dubinski/Code/

Thanks,
Aleks
Jul 19 '05 #4

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

Similar topics

5
by: Kylotan | last post by:
I thought mod_python would be the answer to my CGI performance issue, but I can't seem to make much sense out of it. All the examples are too trivial to be of much use. Given that I have an...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
7
by: nanotech | last post by:
I have a nested loop which generates ordered pairs, triplets, ... of integers from 0 to a maximum N. for ( a=0;a<N;a++) for( a=0;a<a;a++) for( a=0;a<a;a++) I would like to...
5
by: Szabolcs | last post by:
I am looking for a way to generalize the function template below, so that it will work with any type, not just double. Is this at all possible in C++? I'd like to replace double (*fun)(double)...
1
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.