I have a template class with three policies: PolicyA, PolicyB, and
PolicyC. The design dilemma I am having is that B and C depend upon A,
but I would like my Host class to derive from all three. In all three
cases I would like to take advantage of enriched policies.
template <
class PolicyA,
template <classclass PolicyB,
template <classclass PolicyC
>
class Host: public PolicyA, public PolicyB<PolicyA>, public
PolicyC<PolicyA>
{
};
How do I structure the policy classes A, B, and C such that B and C
have access to A (Host)? 3 1993
DerrickH wrote:
I have a template class with three policies: PolicyA, PolicyB, and
PolicyC. The design dilemma I am having is that B and C depend upon A,
but I would like my Host class to derive from all three. In all three
cases I would like to take advantage of enriched policies.
template <
class PolicyA,
template <classclass PolicyB,
template <classclass PolicyC
class Host: public PolicyA, public PolicyB<PolicyA>, public
PolicyC<PolicyA>
{
};
How do I structure the policy classes A, B, and C such that B and C
have access to A (Host)?
The question is a bit vague. Do you need the instances of the base
classes B and C (base class subobjects of 'Host') to have access to the
instance of the base class A (the subobject of the same 'Host')? In
that case you need to construct PolicyB and PolicyC with, say, a pointer
to 'PolicyA' object. So, when you construct the Host, you'd do
something like this
Host()
: PolicyA()
, PolicyB<PolicyA>(this)
, PolicyC<PolicyA>(this)
{
}
Provided that 'PolicyB' and 'PolicyC' can be constructed from a pointer
to 'PolicyA', 'this' expression will be converted to 'PolicyA*', and the
other two subobjects will receive the correct address of 'PolicyA'.
If that's not what you're asking, then please show us what kind of
"access" you expect (better in a C++ form) so it would be possible to
understand what you're trying to accomplish.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On May 23, 11:29*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
DerrickH wrote:
I have a template class with three policies: PolicyA, PolicyB, and
PolicyC. The design dilemma I am having is that B and C depend upon A,
but I would like my Host class to derive from all three. In all three
cases I would like to take advantage of enriched policies.
template <
* *class PolicyA,
* *template <classclass PolicyB,
* *template <classclass PolicyC
class Host: public PolicyA, public PolicyB<PolicyA>, public
PolicyC<PolicyA>
{
};
How do I structure the policy classes A, B, and C such that B and C
have access to A (Host)?
The question is a bit vague. *Do you need the instances of the base
classes B and C (base class subobjects of 'Host') to have access to the
instance of the base class A (the subobject of the same 'Host')? *In
that case you need to construct PolicyB and PolicyC with, say, a pointer
to 'PolicyA' object. *So, when you construct the Host, you'd do
something like this
* * *Host()
* * * * *: PolicyA()
* * * * *, PolicyB<PolicyA>(this)
* * * * *, PolicyC<PolicyA>(this)
* * *{
* * *}
Provided that 'PolicyB' and 'PolicyC' can be constructed from a pointer
to 'PolicyA', 'this' expression will be converted to 'PolicyA*', and the
other two subobjects will receive the correct address of 'PolicyA'.
If that's not what you're asking, then please show us what kind of
"access" you expect (better in a C++ form) so it would be possible to
understand what you're trying to accomplish.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you, Victor. Sorry for being vague. That is exactly what I'm
looking for. And, actually after giving it some thought, C also
depends upon B. Let me make it a little more clear if I can. Basically
I'm trying to model a service provider which provides two different
services.
template <
class ConnectionDetails,
template <classclass Service1,
template <class, classclass Service2
>
class service_provider :
public ConnectionDetails,
public Service1<ConnectionDetails>,
public Service2<ConnectionDetails,
Service1<ConnectionDetails
{
public:
service_provider() :
ConnectionDetails(),
Service1<ConnectionDetails>(this),
Service2<ConnectionDetails,
Service1<ConnectionDetails(this, this)
{
}
};
The solution you provided is what I'm looking for, but is there a way
to avoid the this->copy_of_this-extra level of indirection that
would result? Or, even better, is there a better way to model this
type of relationship?
Thanks again.
-Derrick
DerrickH wrote:
On May 23, 11:29 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>DerrickH wrote:
>>I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In all three cases I would like to take advantage of enriched policies. template < class PolicyA, template <classclass PolicyB, template <classclass PolicyC class Host: public PolicyA, public PolicyB<PolicyA>, public PolicyC<PolicyA> { }; How do I structure the policy classes A, B, and C such that B and C have access to A (Host)?
The question is a bit vague. Do you need the instances of the base classes B and C (base class subobjects of 'Host') to have access to the instance of the base class A (the subobject of the same 'Host')? In that case you need to construct PolicyB and PolicyC with, say, a pointer to 'PolicyA' object. So, when you construct the Host, you'd do something like this
Host() : PolicyA() , PolicyB<PolicyA>(this) , PolicyC<PolicyA>(this) { }
Provided that 'PolicyB' and 'PolicyC' can be constructed from a pointer to 'PolicyA', 'this' expression will be converted to 'PolicyA*', and the other two subobjects will receive the correct address of 'PolicyA'.
If that's not what you're asking, then please show us what kind of "access" you expect (better in a C++ form) so it would be possible to understand what you're trying to accomplish.
V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask
Thank you, Victor. Sorry for being vague. That is exactly what I'm
looking for. And, actually after giving it some thought, C also
depends upon B. Let me make it a little more clear if I can. Basically
I'm trying to model a service provider which provides two different
services.
template <
class ConnectionDetails,
template <classclass Service1,
template <class, classclass Service2
class service_provider :
public ConnectionDetails,
public Service1<ConnectionDetails>,
public Service2<ConnectionDetails,
Service1<ConnectionDetails
{
public:
service_provider() :
ConnectionDetails(),
Service1<ConnectionDetails>(this),
Service2<ConnectionDetails,
Service1<ConnectionDetails(this, this)
{
}
};
The solution you provided is what I'm looking for, but is there a way
to avoid the this->copy_of_this-extra level of indirection that
would result? Or, even better, is there a better way to model this
type of relationship?
I am honestly not sure what kind of "extra level of indirection" you are
talking about. Here is a trial implementation, enjoy.
#include <ostream>
template <class Dstruct CD
{
D d;
CD(D d) : d(d) {}
std::ostream& dump(std::ostream& os) const { return os << d; }
};
template <class CDclass S1
{
CD* cd;
public:
S1(CD* cd_) : cd(cd_) {}
std::ostream& dump(std::ostream& os) const {
os << "S1(";
cd->dump(os);
return os << ")";
}
};
template <class CD, class S1class S2
{
CD* cd;
S1* s1;
public:
S2(CD* cd_, S1* s1_) : cd(cd_), s1(s1_) {}
std::ostream& dump(std::ostream& os) const {
os << "S2("; cd->dump(os);
os << ",";
s1->dump(os);
return os << ")";
}
};
template < class ConnectionDetails,
template <classclass Service1,
template <class, classclass Service2 >
class service_provider :
public ConnectionDetails,
public Service1<ConnectionDetails>,
public Service2<ConnectionDetails, Service1<ConnectionDetails
{
typedef ConnectionDetails A;
typedef Service1<ConnectionDetailsB;
typedef Service2<ConnectionDetails, Service1<ConnectionDetails C;
public:
service_provider(ConnectionDetails const& a) :
ConnectionDetails(a),
Service1<ConnectionDetails>(this),
Service2<ConnectionDetails, Service1<ConnectionDetails(this, this)
{
}
std::ostream& dump(std::ostream& os) const {
os << "provider (";
static_cast<A const*>(this)->dump(os); os << ",";
static_cast<B const*>(this)->dump(os); os << ",";
static_cast<C const*>(this)->dump(os); os << ")";
return os;
}
};
#include <iostream>
int main()
{
CD<intcd(42);
service_provider<CD<int>,S1,S2sp(cd);
sp.dump(std::cout);
std::cout << std::endl;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ian |
last post by:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++
Design by Andrei Alexandrescu. The test code is listed below and the
compiler error message that is generated is:
...
|
by: Rob Barnes |
last post by:
When I try to create a machine-level security policy based on an assembly's
strong name, I get the following error:
"ERROR: Invalid label or name"
The caspol command is:
"caspol -machine...
|
by: rjoshi |
last post by:
There is a nice article about practicle usage of Policy Base Design at
http://www.codeproject.com/library/Generic_Pool_Design.asp
|
by: Martin Vorbrodt |
last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both
OpenGL and Direct3D can take a pointer to array of 16 floats which represent
the values in the matrix. OGL takes it in...
|
by: Martin Vorbrodt |
last post by:
please be so kind and direct me to as many sources as you can regarding the
*subject* matter. i'm new to the topic and would like to learn as much as
possible.
thank you in advance
martin
|
by: Erik Wikström |
last post by:
In school (no I will not ask you to do my schoolwork for me) we talked
about policy-based design and got an assignment where we got the a code-
fragment from a stack-implementation. The idea with...
|
by: aaragon |
last post by:
Hi everyond. I'm trying to write a library usign policy based design
so I can implement
different behaviors. One of the behaviors was to define a
StoragePolicy. The following code gives the...
|
by: aaragon |
last post by:
Hi everyone. I'm trying to write a class with policy based design
(Alexandrescu's Modern C++ Design). I'm not a programmer but an
engineer so this is kind of hard for me. Through the use of...
|
by: rrangaprasad |
last post by:
i'm trying to create a 'kiosk' type of workstation, based on a particular login (where the desktop is pretty much locked and the user won't have access to run commands, change taskbar, etc). Now, the...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |