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

design guidance .. I'm confused


I've provided a stripped down version - as best I could - to get us by.
That said, I'm in a quandry with respect to a design here.

Consider:
// stream.h
#ifndef STREAM_H
#define STREAM_H

# pragma warning (disable : 4786) // if using microsofts compiler ver6

# include <iostream>
# include <string>
# include <map>
# include <typeinfo>

using namespace std;

struct stream
{
string classId;
string buffer;
size_t curPos;

stream(string clsId)
: classId(clsId), curPos(0)
{}

stream(char* buf, size_t size)
: curPos(0)
{
string temp(buf, size);
size_t pos = temp.find(':');
if (pos == string::npos || pos == 0 || pos == size-1)
return;
classId = temp.substr(0, pos);
buffer = temp.substr(pos+1);
}

bool isValid() { return curPos <= buffer.length(); }
operator bool() { return isValid(); }

size_t getBufferSize() {
return isValid()? classId.size() + 1 + buffer.size() : 0;
}

size_t getBuffer(char*& buf, size_t siz)
{
size_t sizNeeded = getBufferSize();
return sizNeeded;
}
};
template <class T>
inline stream& operator>>(stream& ds, T& t) {
// stuff
return ds;
}
template <class T>
inline stream& operator<<(stream& ds, T& t) {
// stuff
return ds;
}
// more
#endif

// factory.h
#ifndef FACTORY_H
#define FACTORY_H

# include<map>
# include<string>
# include "stream.h"

class factory;
typedef factory* (*CreateInstanceFunc)();
struct stream;
using namespace std;
class factory
{
// factory map is mapping class names to create functions
static std::map<std::string, CreateInstanceFunc> factoryMap;
public:
virtual std::string getClassId() = 0;
virtual bool load(stream& ds) = 0;
virtual bool store(stream& ds) = 0;
virtual void execute() = 0;
static factory* createInstance(const stream& ds)
{
map<string, CreateInstanceFunc>::iterator it;
if ((it = factoryMap.find(ds.classId)) == factoryMap.end())
return NULL;
return (it->second)();
}

static bool registerClass(
const std::string& className,
CreateInstanceFunc createFunc
)
{
map<string, CreateInstanceFunc>::iterator it;
if ((it = factoryMap.find(className)) != factoryMap.end())
return false; // already registered

factoryMap[className] = createFunc;
return true;
}

stream* store();
};

std::map<string, CreateInstanceFunc> factory::factoryMap;

#endif

// now we're ready file: test.cpp
# include <iostream>
# include <string>
# include "factory.h"
# include "stream.h"

using namespace std;

struct test_struct
: public factory
{
int idx;
test_struct() : idx(99) {}

std::string getClassId() {
return "test_struct";
}
bool store(stream& ds) {
ds >> idx;
return ds.isValid();
}
bool load(stream& ds) {
ds << idx;
return ds.isValid();
}
static factory* create_msg () {
return new test_struct;
}
void execute () {
}
};

/////////////////////////////////////////////////////////////////
// the registration below is REALLY bad news. Need to rethink this
// static initilization order stuff.
//factory::registerClass("test_struct", test_struct::create_msg);
class test {
test_struct* ptr_msg;
public:
test() : ptr_msg(0) {
factory::registerClass("test_struct", test_struct::create_msg);
}
void get_data()
{
char buffer[4096] = { "test_struct:99" };
size_t len = sizeof buffer / sizeof *buffer;

stream st (buffer, len);
if (st)
{
ptr_msg = (test_struct*)factory::createInstance(st); // 99
if (ptr_msg != NULL)
{
cout << " success " << endl;
if (ptr_msg->idx == 99)
cout << " even more success " << endl;
}
}
}
};

int main()
{
test t;
t.get_data();
}

The line marked '99' is a cast from a polymorphic type to a test_struct
object. So I thought it would nice to eliminate that ugly cast, hence
the addition of the 'execute' function in persistent.

Trouble is, the execute function creates a problem for two reasons:
1. I could modify the source in get_data to look like:

if (st)
{
//ptr_msg = (test_struct*)factory::createInstance(st);
factory* pfact = factory::createInstance(st);
if (pfact != NULL)
{
cout << " success " << endl;
pfact->execute();
//if (ptr_msg->idx == 99)
// cout << " even more success " << endl;
}
}
Except now I need a way to move from the factory object to a
test_struct object so that I could do:
if (ptr_msg->idx == 99)
// do a special thing.

One thought was to modify execute in the test_struc class to look like:
void execute () {
t.update(this); // call some update function in test
}

Trouble is. I now need a 'global' initilization of t. Not good. It
gets worse. In my real code, the initilization of t is wrapped in a
try catch handler so that there goes the global initilization.

2. In theory my 'struct's are defined as incoming and outging msg
structs. So now:

struct outgoing_msg
: public factory
{};

struct incoming_msg
: public factory
{};
The only struct that needs to go through these 'hoops' is the
incoming_msg struct. That said, I would have an execute member
function in the outgoing_msg struct for NO reason.

In the end I suspect I'm just forced to live with the cast from a
polymorphic object to a test_struct.

ptr_msg = (test_struct*)factory::createInstance(st);

Thoughts/ideas?

Thanks in advance

Jul 23 '05 #1
0 1100

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

Similar topics

2
by: Donal McWeeney | last post by:
Hi, Are there any good guidance white papers out there on the best way to design and build assemblys in VS.Net that would cover the following questions I have and requirements I know of: The...
4
by: HankD | last post by:
I have been given the task to upgrade the company's website. It is large and unorganized right now. There should be a main common area with subareas for each division (5). My main question is what...
0
by: ma740988 | last post by:
I inquired about utilizing a vector of pairs just yesterday and after receiving some feedback, I got to thinking my thoughts with regards to my initial 'approach' was flawed to begin with: ...
2
by: vbnetguy | last post by:
Hi All, I have read MANY posts on how to track changes to data over time It appears there are two points of view 1. Each record supports a Change Indicator flag to indicate the current...
0
by: lucky | last post by:
hi, i've to work on a DB2 project.i'm totally unaware of those concepts.can anyone guide me atleast on the overview of db2.i need specific guidance on connectivity in db2.does it refer tothe...
20
by: Brad Pears | last post by:
I am completely new to vb .net. I am using visual Studio 2005 to redo an Access 2000 application into a .net OO application using SQL Server 2000 - so a complete rewrite and re-thinking of how...
8
by: | last post by:
I'm looking for some design guidance on a collection of projects I'm working on. The project involves a bunch of websites constructed out of a collection of user controls. Different user...
1
by: Rusty Hill | last post by:
In ASP.net 2.0 I need to create a scheduling page that allows my users to book/schedule/reserve six different surgery rooms. What the design calls for is one screen that has the daily schedule on...
3
by: vicky | last post by:
Hi All, Can u please suggest me some books for relational database design or database modelling(Knowledgeable yet simple) i.e. from which we could learn database relationships(one to many,many to...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.