473,804 Members | 3,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1908
"Aleksandr Dubinskiy" <du*********@os u.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::PIns tance* other);
virtual void diffuse(double, EnergyGetter*);
};

typedef PI<Proposal::Pr oposalABCD,
Converter::Tiny ToABCD,
Updater::Aprime Updater> APrime;

typedef PI<Proposal::Pr oposalGridWrap,
Converter::Tiny To1D<Wrap>,
Updater::MuCent erUpdater> MuCenter;
--------------------------------------------------- in DiffuserSF.h
template<class P, class C, class U>
class SFPI : public SFParticularInt erface
{
P* prop;
C* conv;
U* upd;
public:
SFPI(BaseLevel: :SFDynamic*);
virtual void diffuse_one(dou ble, EnergyGetter*, int);
};

typedef SFPI<Proposal:: ProposalGridMSP MusOnly,
Converter::Tiny ToTripletL,
Updater::ParamU pdater> MusOnly;

typedef SFPI<Proposal:: ProposalGridMSP ,
Converter::Tiny ToTripletL,
Updater::ParamU pdater> Param;

typedef SFPI<Proposal:: ProposalABCD,
Converter::Tiny ToABCD,
Updater::CoeffU pdater> 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*********@os u.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::Propo salTemplate* p,
Converter::Tiny Converter* c,
Updater::StateU pdater* 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
1437
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 application consisting of several CGI scripts, what is the simplest way to go about converting it to run on mod_python, not using the CGI-handler which is allegedly slow and buggy? Everything seems to suggest a major rewrite because I not only need...
6
2162
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 binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
5
1719
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 i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
8
3157
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, 1> and A<int, 2> are different types. Now, if the A template
3
3763
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
45
2947
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
7
1890
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 generalize this for a for given n, or find some more creative way of generating these sets. Any help is much appreciated.
5
1778
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) with a generalized A (*fun)(B). template<double (*fun)(double)> array<doubleapply(const array<double&source) { array<doubleresult(source.size()); for (int i = 0; i < source.size(); i++)
1
2379
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 { public:
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.