Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Policy based design

0 views
Skip to first unread message

Martin Vorbrodt

unread,
Oct 11, 2005, 12:23:49 PM10/11/05
to
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

Martin Vorbrodt

unread,
Oct 11, 2005, 12:29:45 PM10/11/05
to
One more thing. What about inheritance? I saw an approach where the host
class inherits from the policy class.


"Martin Vorbrodt" <mvor...@poczta.onet.pl> wrote in message
news:digopd$dsc$1...@news.onet.pl...

Victor Bazarov

unread,
Oct 11, 2005, 12:33:35 PM10/11/05
to

A static function is definitely preferable. And WRT your approach, you
don't have to keep the copy of the policy object, you could always use
a temporary:

inline int GetOffset(int row, int col) const
{

return OffsetPolicy().GetOffset(row, col);
}

, if for some reason a static member doesn't work out for you. It does
carry a tiny overhead of creating a temporary object and passing its
pointer to the member function, of course (unlike the static member).

> 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?

No.

> Does it require an additional function call (even though
> the body of the static is visible)?

No.

> [..]

V

Victor Bazarov

unread,
Oct 11, 2005, 12:45:13 PM10/11/05
to
Martin Vorbrodt wrote:
> One more thing. What about inheritance? I saw an approach where the host
> class inherits from the policy class.

Please don't top-post, even in response to your own messages.

What about inheritance? Did you mean to ask a particular question? Yes,
Alexandrescu in his book shows plenty of examples of classes inheriting
from policies...

> [..]

V

Martin Vorbrodt

unread,
Oct 11, 2005, 1:02:31 PM10/11/05
to
"Victor Bazarov" <v.Aba...@comAcast.net> wrote in message
news:u_R2f.40332$Tf5....@newsread1.mlpsca01.us.to.verio.net...

Do you know what the advantages would be of using inheritance for policy
classes? Why not statics, or policy class as a data member?


Victor Bazarov

unread,
Oct 11, 2005, 1:15:51 PM10/11/05
to

The advantage is you provide your 'GetOffset' only once -- in the policy,
and there is no need for another member, in the actual class. Why not?
A policy as a data member wastes memory. Statics, sure, why not?

V

Axter

unread,
Oct 11, 2005, 1:36:57 PM10/11/05
to

I recommend you inherit the policy. It's more efficient then the
member method, and IMHO, easier to maintain and read then the static
method.

Example:


template<typename T, typename OffsetPolicy>

class Matrix : public OffsetPolicy {
public:
// No need for the following code, since you inheritance the method
from base class
// inline int GetOffset(int row, int col) const
// { return GetOffset(row, col); }
};

FYI:
This does not follow the IS-A rule for inheritance, but policy classes
are the exception to the (IS-A) rule.

Martin Vorbrodt

unread,
Oct 11, 2005, 2:49:01 PM10/11/05
to
"Axter" <goo...@axter.com> wrote in message
news:1129052217.2...@g14g2000cwa.googlegroups.com...

so i hear!
make the destructor private and non virtual should take care of is-a.

ufff, cool shit! c++ rules!


mlimber

unread,
Oct 11, 2005, 5:00:33 PM10/11/05
to
Axter wrote:
> I recommend you inherit the policy. It's more efficient then the
> member method, and IMHO, easier to maintain and read then the static
> method.
[snip]

I normally do this also. However, empty base classes (in this case,
policy classes without data members) are not always free and thus not
always more efficient in terms of memory usage. There is further
discussion of and a trick to get around the problem in the article
"Smart Pointers Reloaded" by Andrei Alexandrescu and David B. Held in
section entitled "Size Does Matter"
(http://www.cuj.com/documents/s=8890/cujexp0310alexandr/alexandr.htm).

Cheers! --M

Martin Vorbrodt

unread,
Oct 11, 2005, 8:55:16 PM10/11/05
to

private:
OffsetPolicy Offset;
};

Thanks in advance!

Martin

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

Phlip

unread,
Oct 11, 2005, 9:45:24 PM10/11/05
to
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!!!


hydr...@yahoo.com

unread,
Oct 12, 2005, 12:30:09 PM10/12/05
to
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.

Puppet_Sock

unread,
Oct 12, 2005, 2:14:47 PM10/12/05
to
Martin Vorbrodt wrote:
[snip]
> But is it slower?

Usually the answer to that question is "get out a stopwatch
and write a test code to try it." If you do, try to make it
as representative of typical situations as possible.

But before you do that, ask yourself if the "slower" that
might actually exist is going to make a big difference.
If you are talking about a part of your code where it
spends, say, 5 percent of its time, it's probably not
worth worrying about even if it *is* slower. If it's in
part of the code where it spends 90 percent of its time,
it might be worth it if there is a big difference. If you
worry about "is it slower" before you find out if it matters,
you are probably wasting your time.
Socks

Yuri Khan

unread,
Oct 13, 2005, 9:40:59 AM10/13/05
to
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).

mdli...@yahoo.co.in

unread,
Oct 16, 2005, 5:43:20 AM10/16/05
to
>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

mdli...@yahoo.co.in

unread,
Oct 16, 2005, 12:14:45 PM10/16/05
to
Sorry I forgot to send link :

http://www.gamedev.net/reference/articles/article2054.asp

Regards
Dinesh

0 new messages