473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>>(stre am& ds, T& t) {
// stuff
return ds;
}
template <class T>
inline stream& operator<<(stre am& 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* (*CreateInstanc eFunc)();
struct stream;
using namespace std;
class factory
{
// factory map is mapping class names to create functions
static std::map<std::s tring, CreateInstanceF unc> 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, CreateInstanceF unc>::iterator it;
if ((it = factoryMap.find (ds.classId)) == factoryMap.end( ))
return NULL;
return (it->second)();
}

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

factoryMap[className] = createFunc;
return true;
}

stream* store();
};

std::map<string , CreateInstanceF unc> factory::factor yMap;

#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_struc t";
}
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::regist erClass("test_s truct", test_struct::cr eate_msg);
class test {
test_struct* ptr_msg;
public:
test() : ptr_msg(0) {
factory::regist erClass("test_s truct", test_struct::cr eate_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*)f actory::createI nstance(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*)f actory::createI nstance(st);
factory* pfact = factory::create Instance(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*)f actory::createI nstance(st);

Thoughts/ideas?

Thanks in advance

Jul 23 '05 #1
0 1122

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

Similar topics

2
1841
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 assembly(s) I build must be installed in the GAC. My assembly(s) needs to include the following: - my own class/utility librarary code
4
1176
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 is the best way to manage this in visual studio? Should I create one solution and then 6 projects under it (1 for the main company content and then 1 for each division content) or should I split it up? If I do split it up how do I reference...
0
1458
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: Here's my 'dilema' in a nutshell. I've got 24 sources ( call them 0 ... 23 ) sending data to a recipient ( call this 'X' ). The data from each source has what's called an 'id' (call it segment id) assosciated with it.
2
1300
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 record (would this be EVERY table?) 2. Each table is duplicated as an archive table and triggers are used to update archive
0
1328
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 DB2 connectivity with the front end or about how the client gets connected to the DB2 server?can u guide me on those.. :confused: :confused: :confused:
20
1538
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 this app will work. I have NEVER done any OO programming at all although I have used OO techniques in programs of course - just never actually designed the classes etc... So I am just a tad nervous in re-writing this Access application as it is...
8
1705
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 populations with different access rights and "roles" will be visiting the site. I will be using ASP.NET 2.0's membership, roles, and profiles stuff to manage access. User controls need to be visible or not visible depending on user role. In some...
1
1141
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 the vertical axis and across the top on a horizontal axis the six different surgery rooms are represented. This way the operator can see at a glance the status of all six surgery rooms at the same time for the selected day. I am sure this is...
3
2487
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 one etc.....),building ER diagrams,proper usage of ER diagrams in our database(Primary key foreign key relations),designing small modules,relating tables and everything that relates about database design....Coz I think database design is the...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.