473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Policy Based Design Question

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)?
Jun 27 '08 #1
3 2016
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
Jun 27 '08 #2
On May 23, 11:29*am, Victor Bazarov <v.Abaza...@com Acast.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 ConnectionDetai ls,
template <classclass Service1,
template <class, classclass Service2
>
class service_provide r :
public ConnectionDetai ls,
public Service1<Connec tionDetails>,
public Service2<Connec tionDetails,
Service1<Connec tionDetails
{
public:
service_provide r() :
ConnectionDetai ls(),
Service1<Connec tionDetails>(th is),
Service2<Connec tionDetails,
Service1<Connec tionDetails(thi s, 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

Jun 27 '08 #3
DerrickH wrote:
On May 23, 11:29 am, Victor Bazarov <v.Abaza...@com Acast.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<Polic yA>
{
};
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 ConnectionDetai ls,
template <classclass Service1,
template <class, classclass Service2
class service_provide r :
public ConnectionDetai ls,
public Service1<Connec tionDetails>,
public Service2<Connec tionDetails,
Service1<Connec tionDetails
{
public:
service_provide r() :
ConnectionDetai ls(),
Service1<Connec tionDetails>(th is),
Service2<Connec tionDetails,
Service1<Connec tionDetails(thi s, 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::ostre am& os) const { return os << d; }
};

template <class CDclass S1
{
CD* cd;
public:
S1(CD* cd_) : cd(cd_) {}
std::ostream& dump(std::ostre am& 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::ostre am& os) const {
os << "S2("; cd->dump(os);
os << ",";
s1->dump(os);
return os << ")";
}
};

template < class ConnectionDetai ls,
template <classclass Service1,
template <class, classclass Service2 >
class service_provide r :
public ConnectionDetai ls,
public Service1<Connec tionDetails>,
public Service2<Connec tionDetails, Service1<Connec tionDetails
{
typedef ConnectionDetai ls A;
typedef Service1<Connec tionDetailsB;
typedef Service2<Connec tionDetails, Service1<Connec tionDetails C;
public:
service_provide r(ConnectionDet ails const& a) :
ConnectionDetai ls(a),
Service1<Connec tionDetails>(th is),
Service2<Connec tionDetails, Service1<Connec tionDetails(thi s, this)
{
}

std::ostream& dump(std::ostre am& 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_provide r<CD<int>,S1,S2 sp(cd);
sp.dump(std::co ut);
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
Jun 27 '08 #4

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

Similar topics

5
3198
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: testPolicy.cpp(25) : error C2984: 'CPolicy' : template parameters '<template parameter>' and '<template parameter>' do not match testPolicy.cpp(22) : see declaration of 'CPolicy' I am working with version 1 of Microsoft VC++ and running WinXP Pro....
1
2184
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 -addgroup All_Code -strong -file \\<network_share>\app.exe -noversion"
0
1357
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
9
514
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 column order, D3D in row order. Therefore row = 2, col = 3 will result in different offset into the float array depending on the API. Here is my basic design test program: #include <iostream> using std::cout; using std::endl;
4
2043
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
4
2281
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 the code was that if, when its destructor was called, it still contained any elements it would run 'delete' on them if they were pointers and do nothing if they were not. Our assignment was to reimplement the stack using policies so that the...
0
1374
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 whole idea for a population object: #ifndef _POPULATION_H #define _POPULATION_H #include "gaParameters.h"
11
2347
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 policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of the boost dynamic bitset. Another would be the use of the std::vector. I obtained some code that...
1
8565
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 OS of this syetsm is WIndows Server 2003. I'm trying to achieve this using LOCAL Group Policy Editor (i'm not worried abt domain right now). Now, i created an account called "TENANT" and made it a member of groups "user" and "Administrators"....
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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
8905
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
8880
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
6532
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
5869
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
4373
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...
3
2008
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.