473,799 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Policy based design

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;

class OGLOffset {
public:
inline int GetOffset(int row, int col) const {
cout << "OGL offsets (" << row << "," << col << ") = ";
return row + col * 4;
}
};

class D3DOffset {
public:
inline int GetOffset(int row, int col) const {
cout << "D3D offsets (" << row << "," << col << ") = ";
return col + row * 4;
}
};

template<typena me T, typename OffsetPolicy>
class Matrix {
public:
inline int GetOffset(int row, int col) const
{ return Offset.GetOffse t(row, col); }

private:
OffsetPolicy Offset;
};

int main() {
Matrix<int, OGLOffset> m1;
Matrix<int, D3DOffset> m2;

cout << m1.GetOffset(2, 3) << endl;
cout << m2.GetOffset(2, 3) << endl;
}

Now my question is this:

In this example, i use inline functions, and the matrix class has-a object
of a given offseting policy type. What i could do instead, is to have a
static member function instead, and NOT keep a copy of the policy class
inside the matrix. Which approach would be better? I can see the basic
tradeoff: inline member is probably "faster", but there is storage overhead
for having OffsetPolicy member (no class can have a size of 0, according to
the standard right?) On the other hand, static costs me 0 space overhead.
But is it slower? Does it require an additional function call (even though
the body of the static is visible)?

Please advise. I'm new to the policy based deisgn. I know STL makes
extensive use of such approaches (allocators, traits, etc). Please point me
to information (i'm in the process of reading Modern C++ Design, by
Alexandrescu), i would like to get as much info as possible on this kind
ofdesign approaches.

Thanks in advance!

Martin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Oct 12 '05 #1
5 3115
Martin Vorbrodt wrote:
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;

class OGLOffset {
public:
inline int GetOffset(int row, int col) const {
cout << "OGL offsets (" << row << "," << col << ") = ";
return row + col * 4;
}
};


Why isn't that function static? It uses no member variables of OGLOffset.

Then, if you don't need an object of type OGLOffset to use it, you don't
need a member in your matrix class. You only need the policy type parameter.

Next question: Where are your unit tests?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Oct 12 '05 #2
Why not inheret from the policy?

template<typena me T, class OffsetPolicy>
class Matrix : public OffsetPolicy
{

};

Then write your code and call your functions. It won't compile if you
put a policy class in there that doesn't support the GetOffset
function.

A static member function sort of defeats the purpose of policy based
design.

Also, when using policy, I like to state in the docs of the templated
class what the EXPECTED interface of the policy is. Helps the poor dumb
sap that comes along later.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Oct 12 '05 #3
Martin Vorbrodt wrote:
template<typena me T, typename OffsetPolicy>
class Matrix {
public:
inline int GetOffset(int row, int col) const
{ return Offset.GetOffse t(row, col); }
Why are you exposing GetOffset to the world? Isn't it just an
implementation detail that would be used in your operator[] or whatever
indexing solution you come up with?
private:
OffsetPolicy Offset;
}; Now my question is this:

In this example, i use inline functions, and the matrix class has-a object
of a given offseting policy type. What i could do instead, is to have a
static member function instead, and NOT keep a copy of the policy class
inside the matrix. Which approach would be better? I can see the basic
tradeoff: inline member is probably "faster", but there is storage overhead
for having OffsetPolicy member (no class can have a size of 0, according to
the standard right?) On the other hand, static costs me 0 space overhead.
But is it slower? Does it require an additional function call (even though
the body of the static is visible)?


You can inherit from the policy, either publicly, protectedly or
privately, depending on whether your clients and derived classes (if
any) have any business accessing your GetOffset implementation detail.
I guess they don't, so I'd use private inheritance. This also makes
more sense because Matrix is-not-an OffsetPolicy but
is-implemented-in-terms-of it.

On the other hand, you can make OffsetPolicy have a static inline
member function GetOffset, and have Matrix access that function using
OffsetPolicy::G etOffset(row, col) syntax.

In both cases, you have no size overhead (if your compiler is smart
enough to support empty base class optimization), and no speed overhead
(because all function calls involved are inline).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Oct 13 '05 #4
>A static member function sort of >defeats the purpose of policy >based design.

Could please elaborate more on this ? As per my understanding, static
member functions of TTP [template template parameters] plays an
important role in policy based designs.

The following links demonstrate static member functions usage in policy
based designs.

Am I going wrong in my understanding? Please explain.

Regards
Dinesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Oct 16 '05 #5
Sorry I forgot to send link :

http://www.gamedev.net/reference/art...rticle2054.asp

Regards
Dinesh
md******@yahoo. co.in wrote:
A static member function sort of >defeats the purpose of policy >based design.


Could please elaborate more on this ? As per my understanding, static
member functions of TTP [template template parameters] plays an
important role in policy based designs.

The following links demonstrate static member functions usage in policy
based designs.

Am I going wrong in my understanding? Please explain.

Regards
Dinesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Oct 16 '05 #6

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

Similar topics

5
3205
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
2192
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
1367
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
2049
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
2285
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...
1
2895
by: Nick | last post by:
I have read a few months ago about a policy design pattern but can find nothing on it now. Does this design pattern exist and if so could someone explain what it is or links to a description. Thanks Nick
0
1377
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
2353
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...
3
2022
by: DerrickH | last post by:
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
0
9541
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
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7565
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
6805
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
5463
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.