M
ma740988
Consider the snippet:
# include <iostream>
# include <vector>
using namespace std;
class message {
std::string m_id_recipient;
int m_payload;
int m_count;
public:
explicit message (
const std::string& id_recipient, int payload, int count )
: m_id_recipient (id_recipient )
, m_payload(payload)
, m_count (count ) {}
std::string get_recipient_id() const { return m_id_recipient; }
int get_payload() const { return m_payload; }
int get_count() const { return m_count; }
};
class foo {
std::string str;
typedef std::vector<message> MSG_VEC;
MSG_VEC mv;
public:
foo(std::string st)
: str(st)
{ }
void add_msg(message& the_msg) { mv.push_back(the_msg); }
std::string get_foo_str() const { return str; }
};
class bar {
typedef std::vector<int> INT_VEC;
public:
explicit bar() {}
void do_a_special_thing( INT_VEC v )
{
}
};
class manager {
typedef std::vector<foo*> FOO_VEC;
FOO_VEC fv;
bar b;
public:
explicit manager(){}
~manager() {
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
delete *it;
}
void add_foo(foo* ptr_foo)
{
fv.push_back(ptr_foo);
}
void add_foo_msg ( message& the_msg )
{
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
{
const std::string& recipient = the_msg.get_recipient_id();
if ( strcmp(recipient.c_str(),
(*it)->get_foo_str().c_str()) )
{
(*it)->add_msg(the_msg);
break;
}
}
}
void do_foo_search( unsigned int seg_id )
{
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
{
// find the segment_id
//call do_a_special_thing
}
}
};
int main()
{
manager mgr;
mgr.add_foo(new foo("foo1"));
mgr.add_foo(new foo("foo2"));
mgr.add_foo_msg( message("foo1", 0x4000, 1)); // i
mgr.add_foo_msg( message("foo1", 0x1000, 5));
mgr.add_foo_msg( message("foo2", 0x2000, 1)); // ii
mgr.add_foo_msg( message("foo3", 0x3000, 2));
}
Within the member function do_foo_seach, if I passed in the value 1.
I'd like store the values 0x4000 ( see i ) and 0x2000 (see ii ) in a
vector that I'll hand to the function do_a_special_thing.
What's an elegant way to achieve this? I'm messing around with an
approach here but it doesnt seem right.
Thanks in advance
# include <iostream>
# include <vector>
using namespace std;
class message {
std::string m_id_recipient;
int m_payload;
int m_count;
public:
explicit message (
const std::string& id_recipient, int payload, int count )
: m_id_recipient (id_recipient )
, m_payload(payload)
, m_count (count ) {}
std::string get_recipient_id() const { return m_id_recipient; }
int get_payload() const { return m_payload; }
int get_count() const { return m_count; }
};
class foo {
std::string str;
typedef std::vector<message> MSG_VEC;
MSG_VEC mv;
public:
foo(std::string st)
: str(st)
{ }
void add_msg(message& the_msg) { mv.push_back(the_msg); }
std::string get_foo_str() const { return str; }
};
class bar {
typedef std::vector<int> INT_VEC;
public:
explicit bar() {}
void do_a_special_thing( INT_VEC v )
{
}
};
class manager {
typedef std::vector<foo*> FOO_VEC;
FOO_VEC fv;
bar b;
public:
explicit manager(){}
~manager() {
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
delete *it;
}
void add_foo(foo* ptr_foo)
{
fv.push_back(ptr_foo);
}
void add_foo_msg ( message& the_msg )
{
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
{
const std::string& recipient = the_msg.get_recipient_id();
if ( strcmp(recipient.c_str(),
(*it)->get_foo_str().c_str()) )
{
(*it)->add_msg(the_msg);
break;
}
}
}
void do_foo_search( unsigned int seg_id )
{
FOO_VEC::iterator it;
for ( it = fv.begin(); it != fv.end(); ++it )
{
// find the segment_id
//call do_a_special_thing
}
}
};
int main()
{
manager mgr;
mgr.add_foo(new foo("foo1"));
mgr.add_foo(new foo("foo2"));
mgr.add_foo_msg( message("foo1", 0x4000, 1)); // i
mgr.add_foo_msg( message("foo1", 0x1000, 5));
mgr.add_foo_msg( message("foo2", 0x2000, 1)); // ii
mgr.add_foo_msg( message("foo3", 0x3000, 2));
}
Within the member function do_foo_seach, if I passed in the value 1.
I'd like store the values 0x4000 ( see i ) and 0x2000 (see ii ) in a
vector that I'll hand to the function do_a_special_thing.
What's an elegant way to achieve this? I'm messing around with an
approach here but it doesnt seem right.
Thanks in advance