Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 11th, 2005, 05:25 PM
Martin Vorbrodt
Guest
 
Posts: n/a
Default 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



  #2  
Old October 11th, 2005, 05:35 PM
Martin Vorbrodt
Guest
 
Posts: n/a
Default Re: Policy based design

One more thing. What about inheritance? I saw an approach where the host
class inherits from the policy class.


"Martin Vorbrodt" <mvorbrodt@poczta.onet.pl> wrote in message
news:digopd$dsc$1@news.onet.pl...[color=blue]
> I'm designing a Matrix class of 4x4 matrices used in computer graphics.[/color]
Both[color=blue]
> OpenGL and Direct3D can take a pointer to array of 16 floats which[/color]
represent[color=blue]
> 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[/color]
overhead[color=blue]
> for having OffsetPolicy member (no class can have a size of 0, according[/color]
to[color=blue]
> 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[/color]
me[color=blue]
> 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
>
>
>[/color]


  #3  
Old October 11th, 2005, 05:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Policy based design

Martin Vorbrodt wrote:[color=blue]
> 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?[/color]

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).
[color=blue]
> 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?[/color]

No.
[color=blue]
> Does it require an additional function call (even though
> the body of the static is visible)?[/color]

No.
[color=blue]
> [..][/color]

V
  #4  
Old October 11th, 2005, 05:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Policy based design

Martin Vorbrodt wrote:[color=blue]
> One more thing. What about inheritance? I saw an approach where the host
> class inherits from the policy class.[/color]

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...
[color=blue]
> [..][/color]

V
  #5  
Old October 11th, 2005, 06:05 PM
Martin Vorbrodt
Guest
 
Posts: n/a
Default Re: Policy based design

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:u_R2f.40332$Tf5.22386@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> Martin Vorbrodt wrote:[color=green]
> > One more thing. What about inheritance? I saw an approach where the host
> > class inherits from the policy class.[/color]
>
> 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...
>[color=green]
> > [..][/color]
>
> V[/color]

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


  #6  
Old October 11th, 2005, 06:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Policy based design

Martin Vorbrodt wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:u_R2f.40332$Tf5.22386@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>Martin Vorbrodt wrote:
>>[color=darkred]
>>>One more thing. What about inheritance? I saw an approach where the host
>>>class inherits from the policy class.[/color]
>>
>>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...
>>
>>[color=darkred]
>>>[..][/color]
>>
>>V[/color]
>
>
> Do you know what the advantages would be of using inheritance for policy
> classes? Why not statics, or policy class as a data member?[/color]

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
  #7  
Old October 11th, 2005, 06:45 PM
Axter
Guest
 
Posts: n/a
Default Re: Policy based design


Martin Vorbrodt wrote:[color=blue]
> 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[/color]

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.

  #8  
Old October 11th, 2005, 07:55 PM
Martin Vorbrodt
Guest
 
Posts: n/a
Default Re: Policy based design

"Axter" <google@axter.com> wrote in message
news:1129052217.246584.222650@g14g2000cwa.googlegr oups.com...[color=blue]
>
> Martin Vorbrodt wrote:[color=green]
> > I'm designing a Matrix class of 4x4 matrices used in computer graphics.[/color][/color]
Both[color=blue][color=green]
> > OpenGL and Direct3D can take a pointer to array of 16 floats which[/color][/color]
represent[color=blue][color=green]
> > the values in the matrix. OGL takes it in column order, D3D in row[/color][/color]
order.[color=blue][color=green]
> > Therefore row = 2, col = 3 will result in different offset into the[/color][/color]
float[color=blue][color=green]
> > 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[/color][/color]
object[color=blue][color=green]
> > 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[/color][/color]
overhead[color=blue][color=green]
> > for having OffsetPolicy member (no class can have a size of 0, according[/color][/color]
to[color=blue][color=green]
> > the standard right?) On the other hand, static costs me 0 space[/color][/color]
overhead.[color=blue][color=green]
> > But is it slower? Does it require an additional function call (even[/color][/color]
though[color=blue][color=green]
> > 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[/color][/color]
me[color=blue][color=green]
> > 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[/color]
>
> 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.
>[/color]

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

ufff, cool shit! c++ rules!


  #9  
Old October 11th, 2005, 10:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: Policy based design

Axter wrote:[color=blue]
> 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.[/color]
[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/.../alexandr.htm).

Cheers! --M

  #10  
Old October 12th, 2005, 07:25 PM
Puppet_Sock
Guest
 
Posts: n/a
Default Re: Policy based design

Martin Vorbrodt wrote:
[snip][color=blue]
> But is it slower?[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles