design question

V

vijlak

Hi All,

I would like to know how to accomplish the following:


First, I would give an example of MFC.


Let's take the CreateButton styles. So, if some one wants to create a
Button they can do a bitwise 'OR' of different styles like


BS_BOTTOM | BS_DEFPUSHBUTTON | BS_LEFT etc.


Let's say each of the button styles is assigned a long value like so:


enum
{
BS_BOTTOM 0x00000001L
BS_DEFPUSHBUTTON 0x00000002L
BS_LEFT 0x00000004L
.
.
.
.



}ButtonStyles;


So when a user creates the above button, how is it handled in the
microsoft source code?
Obviuosly we cannot have an if or switch statement for each
combination, right?
like this:
switch(buttonstyle)
{
case 0x00000007L: //case that handles the above combination
buttonstlesfunc();

:
:
:
so on...



}


Can someone please point out how this type of logic should be coded?
(Obviously if we have 15 button styles there will be 15!
( factorial )
combinations, right? I am guessing something like getting the value
and doing a bitshift or something like that. i am not sure how
though)

Any help is greatly appreciated.
 
N

Noah Roberts

Hi All,

I would like to know how to accomplish the following:
[snip platform specific example of bitmask use]
Can someone please point out how this type of logic should be coded?
(Obviously if we have 15 button styles there will be 15!
( factorial )
combinations, right? I am guessing something like getting the value
and doing a bitshift or something like that. i am not sure how
though)

Bitmasks are often used to pass switchable values around. In C++ we
tend toward a more object oriented method of query/responce but bitmasks
do still have their use. You check a bitmask for any given value by
using other bit manipulators:

if you set it with

mask |= SOME_CONSTANT, or mask = SOME_CONSTANT | OTHER | CONSTANTS;

you might use:

if (mask & SOME_CONSTANT) do whatever;

or sometimes:

if (!(mask & SOME_CONSTANT)) do whatever.

There are all sorts of operations that can be used. Read about your
bitwise operators. There's also sure to be some tutorials available
through google on working with bitfields and bitmasks.
 
J

Jacek Dziedzic

[snip]
Can someone please point out how this type of logic should be coded?
(Obviously if we have 15 button styles there will be 15!
( factorial )
combinations, right?

Not right. 2^15 combinations, unless some bits
are mutually exclusive.
I am guessing something like getting the value
and doing a bitshift or something like that. i am not sure how
though)

Any help is greatly appreciated.

Noah Roberts has already answered your question, I just
wanted to be picky for a second. <eg>

- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top