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

implementation assistance

Say I've got source executing on 2 processors. The prime difference
in the source lies in select member variables

So now consider:

# include <iostream>
# include <bitset>
using namespace std;

struct open_struct
{ int val1; int val2; };

#if 0
class foo { // foo for processor 1
open_struct open;
public:
foo(open_struct os)
:open(os)
{}
void test(int val)
{
std::bitset<24> bs(val);
for ( int idx(0); idx < bs.size(); ++idx )
{
if (bs[idx])
{
switch ( idx ) // i want bits 0 .. 3 here
{
case 0: cout << " bit 0 set " << endl; break;
case 1: cout << " bit 1 set " << endl; break;
case 2: cout << " bit 2 set " << endl; break;
case 3: cout << " bit 3 set " << endl; break;
break;
}
}
}
}
void do_work( int val ) { if ( val == os.val1 ) {} };
};
int main() // processor 1 main
{
open_struct os;
os.val1 = 0x200000; os.val2 = 5;
foo f;
f.test(0xFF);
}
#endif
class foo { foo for processor 2
open_struct open;

public:
foo(open_struct os)
: open(os)
{}
void test(int val)
{
std::bitset<24> bs(val);
for ( int idx(4); idx < 8; ++idx ) // i want bits 4 .. 7 here
{
if (bs[idx])
{
switch ( idx )
{
case 4: cout << " bit 4 set " << endl; break;
case 5: cout << " bit 5 set " << endl; break;
case 6: cout << " bit 6 set " << endl; break;
case 7: cout << " bit 7 set " << endl; break;
break;
}
}
}
}
void do_work(int val) { if ( val == os.val1 ) {} };
};

int main() // processor 2 main
{
open_struct os;
os.val1 = 0x100000; os.val2 = 5;

foo f(os);
f.test(0xFF);
}

Currently I'm maintaing two foo - classes - which _quite frankly_ is a
maintanence nightmare. Notice the bit range in foo::test is different
for the two classes in addition. I'm seeking to generate a generic
solution such that I'll have 1 foo class that'll handle the unique
range for each foo class. A template solution I'm not sure will help
me here.

Thanks in advance.

Feb 4 '06 #1
6 1274

ma740988 wrote:
Say I've got source executing on 2 processors. The prime difference
in the source lies in select member variables

So now consider:

# include <iostream>
# include <bitset>
using namespace std;

struct open_struct
{ int val1; int val2; };

#if 0
class foo { // foo for processor 1
open_struct open;
public:
foo(open_struct os)
'os' should be passed by non-modifiable reference. Otherwise, you are
making extra copies, which is not efficient.
:open(os)
{}
void test(int val)
{
std::bitset<24> bs(val);
for ( int idx(0); idx < bs.size(); ++idx )
{
if (bs[idx])
{
switch ( idx ) // i want bits 0 .. 3 here
{
case 0: cout << " bit 0 set " << endl; break;
case 1: cout << " bit 1 set " << endl; break;
case 2: cout << " bit 2 set " << endl; break;
case 3: cout << " bit 3 set " << endl; break;
break;
}
}
}
}
void do_work( int val ) { if ( val == os.val1 ) {} };
};
int main() // processor 1 main
{
open_struct os;
os.val1 = 0x200000; os.val2 = 5;
foo f;
f.test(0xFF);
}
#endif
class foo { foo for processor 2
open_struct open;

public:
foo(open_struct os)
: open(os)
{}
void test(int val)
{
std::bitset<24> bs(val);
for ( int idx(4); idx < 8; ++idx ) // i want bits 4 .. 7 here
{
if (bs[idx])
{
switch ( idx )
{
case 4: cout << " bit 4 set " << endl; break;
case 5: cout << " bit 5 set " << endl; break;
case 6: cout << " bit 6 set " << endl; break;
case 7: cout << " bit 7 set " << endl; break;
break;
}
}
}
}
void do_work(int val) { if ( val == os.val1 ) {} };
};

int main() // processor 2 main
{
open_struct os;
os.val1 = 0x100000; os.val2 = 5;

foo f(os);
f.test(0xFF);
}

Currently I'm maintaing two foo - classes - which _quite frankly_ is a
maintanence nightmare. Notice the bit range in foo::test is different
for the two classes in addition. I'm seeking to generate a generic
solution such that I'll have 1 foo class that'll handle the unique
range for each foo class. A template solution I'm not sure will help
me here.


Just move the specialized part into the interface:

int test(int value, int begin, int end);
// Test bits of the specified 'value' in the range specified by
// '(begin, end]', and write to the standard output, for each
bit,
// if the bit is set. Return the number of bits set. The
behavior
// is undefined unless 'begin <= end'.

Then, you can specialize the call for each specific platform. This
signature and contract is more robust, and is better designed for
testing. To really push the envelope, 'test' should take an
'std::ostream' as an argument, rather than hardcoding 'std::cout'.

Feb 4 '06 #2
me here.


Just move the specialized part into the interface:

int test(int value, int begin, int end);
// Test bits of the specified 'value' in the range specified by
// '(begin, end]', and write to the standard output, for each
bit,
// if the bit is set. Return the number of bits set. The
behavior
// is undefined unless 'begin <= end'.

Well, I'm doing a _lot_ more than just 'couts'. Cout's is only shown
here for illustration purposes.

With respect to a generic solution, I'm not seeing how that'll help me
get around the hard coded values;

for ( idx (begin) ; begin < end; ++begin )
{
if (bs[ begin ] )
{
// nwo i have to determine if it's 0 .. 3 (in one case) ( 4.. 8)
in the next
}
}

Feb 4 '06 #3

ma740988 wrote:
me here.


Just move the specialized part into the interface:

int test(int value, int begin, int end);
// Test bits of the specified 'value' in the range specified by
// '(begin, end]', and write to the standard output, for each
bit,
// if the bit is set. Return the number of bits set. The
behavior
// is undefined unless 'begin <= end'.

Well, I'm doing a _lot_ more than just 'couts'. Cout's is only shown
here for illustration purposes.

With respect to a generic solution, I'm not seeing how that'll help me
get around the hard coded values;

for ( idx (begin) ; begin < end; ++begin )
{
if (bs[ begin ] )
{
// nwo i have to determine if it's 0 .. 3 (in one case) ( 4.. 8)
in the next
}
}


You don't need to switch on the index. You already know the value:

for (int i = begin; i < end; ++i) {
if (bs[i]) {
std::cout << "bit " << i << " is set: << std::endl;
}
}

Feb 4 '06 #4

You don't need to switch on the index. You already know the value:

for (int i = begin; i < end; ++i) {
if (bs[i]) {
std::cout << "bit " << i << " is set: << std::endl;
}
}

Right but if the range is 4 (begin) .. 8(end) and I need to do specific
things when i is 4 versus 5, 6 and 7. I need more than just 'begin'
and end for a generic solution.

Feb 4 '06 #5
ma740988 wrote:
> me here.


Just move the specialized part into the interface:

int test(int value, int begin, int end);
// Test bits of the specified 'value' in the range specified by
// '(begin, end]', and write to the standard output, for each
bit,
// if the bit is set. Return the number of bits set. The
behavior
// is undefined unless 'begin <= end'.

Well, I'm doing a _lot_ more than just 'couts'. Cout's is only shown
here for illustration purposes.

With respect to a generic solution, I'm not seeing how that'll help me
get around the hard coded values;

for ( idx (begin) ; begin < end; ++begin )
{
if (bs[ begin ] )
{
// nwo i have to determine if it's 0 .. 3 (in one case) ( 4.. 8)
in the next
}
}

What about putting the code that varies into a function object that you
could either (a) create from the range boundaries or (b) just pass as a
parameter? The following code illustrates (a):

#include <iostream>
#include <bitset>
#include <functional>
#include <algorithm>
#include <cassert>

struct open_struct {
int val1;
int val2;
};
class TestAction : std::unary_function<bool,void> {

unsigned int counter;
unsigned int start_index;

public:

TestAction ( unsigned int start )
: counter ( 0 )
, start_index ( start )
{}

void operator() ( bool flag ) {
if ( flag ) {
std::cout << " bit " << start_index + counter << " is set\n";
}
++ counter;
}

}; // TestAction

class foo {

open_struct open;

public:

foo ( open_struct const & os )
: open ( os )
{}

void test ( int val, unsigned int beg, unsigned int end )
{
std::bitset<24> bs ( val );
assert( beg <= end );
assert( end <= 24 );
TestAction action ( beg );
for ( unsigned int index = beg; index < end; ++index ) {
action( bs[index] );
}
}

};
int main ( void ) {
open_struct os;
os.val1 = 0x200000; os.val2 = 5;
foo f ( os );
f.test(0xFF, 4, 8 );
}
Note that the TestAction object keeps track of the number of calls. This is
a little hacky. However, it allows you to say something like

switch ( counter ) {
case 0 : ...

}
Best

Kai-Uwe Bux
Feb 4 '06 #6

ma740988 wrote:

You don't need to switch on the index. You already know the value:

for (int i = begin; i < end; ++i) {
if (bs[i]) {
std::cout << "bit " << i << " is set: << std::endl;
}
}

Right but if the range is 4 (begin) .. 8(end) and I need to do specific
things when i is 4 versus 5, 6 and 7. I need more than just 'begin'
and end for a generic solution.


One possibility is to pass a vector of callback functions.

Feb 4 '06 #7

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

Similar topics

6
by: MPowell | last post by:
Lets suppose the return value on new_command is MSG2_STATUS My intent then is to memcpy the data via the call to CMDHolder and have a function that'll retrieve a copy of the 'stored' data. The...
14
by: jack | last post by:
At this link I have two c# projects, one is a client, the other is a server. Just point the ip address of the client at the server http://www.slip-angle.com/hosted/bug/ The server does...
5
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the...
12
by: BC | last post by:
I have read the FAQ and the discussions but I am not a Javascript programmer and do not know how to make toFixed (and other techniques) work, after several attempts. The following calculations...
0
by: Jawahar | last post by:
All I had posted this in the remote assistance group and could not get any replies so I thought that I could try in the developer group Thanks One of the issues we face when helping our remote...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
8
by: ma740988 | last post by:
Consider this statement in Excel's text Thinking in C++, Vol 2: /// 1 " A vector starts by grabbing a block of storage, as if it's taking a guess at how many objects you plan to put into it. ...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
3
by: gaurav92K | last post by:
sir i am working in a company . there are many pc. i want to use remote assistance. i configure all group policy which are related remote assistance.and i enable service through remote in system...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.