473,382 Members | 1,409 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,382 software developers and data experts.

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<typename T, typename OffsetPolicy>
class Matrix {
public:
inline int GetOffset(int row, int col) const
{ return Offset.GetOffset(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++.moderated. First time posters: Do this! ]

Oct 12 '05 #1
5 3083
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<typename 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++.moderated. First time posters: Do this! ]

Oct 12 '05 #3
Martin Vorbrodt wrote:
template<typename T, typename OffsetPolicy>
class Matrix {
public:
inline int GetOffset(int row, int col) const
{ return Offset.GetOffset(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::GetOffset(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++.moderated. 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++.moderated. 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++.moderated. 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
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: ...
1
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...
0
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
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...
4
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
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...
1
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. ...
0
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...
11
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.