1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//declarations...
class Message;
//abstract Message Data class
class MsgData
{
public:
virtual bool input_value(){return true;}
virtual void output_value() const{}
virtual int get_id() const{return -1;}
virtual int get_size() const{return 0;}
virtual void add_to_message(Message * msg) const{}
virtual void read_from_message(Message * msg, int start, int & stop){}
};
//Integer Message Data class
class MsgInt:public MsgData
{
protected:
int value;
public:
bool input_value();
void output_value() const;
int get_id() const;
int get_size() const;
void add_to_message(Message * msg) const;
void read_from_message(Message * msg, int start, int & stop);
};
//Double Message Data class
class MsgDouble:public MsgData
{
protected:
double value;
public:
bool input_value();
void output_value() const;
int get_id() const;
int get_size() const;
void add_to_message(Message * msg) const;
void read_from_message(Message * msg, int start, int & stop);
};
//String Message Data class
class MsgString:public MsgData
{
protected:
string value;
public:
bool input_value();
void output_value() const;
int get_id() const;
int get_size() const;
void add_to_message(Message * msg) const;
void read_from_message(Message * msg, int start, int & stop);
};
//Message class
class Message
{
friend void MsgData::add_to_message(Message * msg) const;
friend void MsgData::read_from_message(Message * msg, int start, int & stop);
friend void MsgInt::add_to_message(Message * msg) const;
friend void MsgInt::read_from_message(Message * msg, int start, int & stop);
friend void MsgDouble::add_to_message(Message * msg) const;
friend void MsgDouble::read_from_message(Message * msg, int start, int & stop);
friend void MsgString::add_to_message(Message * msg) const;
friend void MsgString::read_from_message(Message * msg, int start, int & stop);
vector<char> data;
public:
void add(const MsgData * msg_data);
void show();
};
//********************************************************************************
//(id->message data class)-mapping
MsgData * NewMsgInt() {MsgData * msg_data=new MsgInt; return msg_data;}
MsgData * NewMsgDouble() {MsgData * msg_data=new MsgDouble; return msg_data;}
MsgData * NewMsgString() {MsgData * msg_data=new MsgString; return msg_data;}
typedef MsgData * (*NewMsgDataFunc)(void);
const NewMsgDataFunc new_msg_data_ftable[]={NewMsgInt,NewMsgDouble,NewMsgString};
MsgData * NewMsgData(int id)
{
MsgData * msg_data=new_msg_data_ftable[id]();
return msg_data;
}
//********************************************************************************
//definitions...
bool MsgInt::input_value()
{
cout << "enter an int: ";
bool success=(cin>>value);
cin.clear();
while (cin.get()!='\n');
return success;
}
void MsgInt::output_value() const
{
cout << "your int is: ";
cout << value;
}
int MsgInt::get_id() const {return 0;}
int MsgInt::get_size() const {return sizeof(int);}
void MsgInt::add_to_message(Message * msg) const
{
char data_id=get_id();
char data_size=get_size();
msg->data.push_back(data_id);
msg->data.push_back(data_size);
int i;
char * cur_byte=(char*)&value;
for (i=0; i<data_size; i++)
{
msg->data.push_back(*cur_byte);
cur_byte++;
}
}
void MsgInt::read_from_message(Message * msg, int start, int & stop)
{
int end=get_size()+start+2;
int i;
char cur_byte;
value=0;
for (i=start+2; i<end; i++)
{
cur_byte=msg->data[i];
*(((char*)&value)+i-start-2)=cur_byte;
}
stop=end;
}
bool MsgDouble::input_value()
{
cout << "enter a double: ";
bool success=cin>>value;
cin.clear();
while (cin.get()!='\n');
return success;
}
void MsgDouble::output_value() const
{
cout << "your double is: ";
cout << value;
}
int MsgDouble::get_id() const {return 1;}
int MsgDouble::get_size() const {return sizeof(double);}
void MsgDouble::add_to_message(Message * msg) const
{
char data_id=get_id();
char data_size=get_size();
msg->data.push_back(data_id);
msg->data.push_back(data_size);
int i;
char * cur_byte=(char*)&value;
for (i=0; i<data_size; i++)
{
msg->data.push_back(*cur_byte);
cur_byte++;
}
}
void MsgDouble::read_from_message(Message * msg, int start, int & stop)
{
int end=get_size()+start+2;
int i;
char cur_byte;
value=0;
for (i=start+2; i<end; i++)
{
cur_byte=msg->data[i];
*(((char*)&value)+i-start-2)=cur_byte;
}
stop=end;
}
bool MsgString::input_value()
{
cout << "enter a string (at most 100 chars): ";
getline(cin,value,'\n');
bool success=cin;
cin.clear();
if (value.size()>100) success=false;
return success;
}
void MsgString::output_value() const
{
cout << "your string is: ";
cout << value;
}
int MsgString::get_id() const {return 2;}
int MsgString::get_size() const {return value.size();}
void MsgString::add_to_message(Message * msg) const
{
char data_id=get_id();
char data_size=get_size();
msg->data.push_back(data_id);
msg->data.push_back(data_size);
int i;
char cur_byte;
for (i=0; i<data_size; i++)
{
cur_byte=value[i];
msg->data.push_back(cur_byte);
}
}
void MsgString::read_from_message(Message * msg, int start, int & stop)
{
int end=msg->data[start+1]+start+2;
int i;
char cur_byte;
value.clear();
for (i=start+2; i<end; i++)
{
cur_byte=msg->data[i];
value+=cur_byte;
}
stop=end;
}
void Message::add(const MsgData * msg_data) {msg_data->add_to_message(this);}
void Message::show()
{
system("cls");
int start=0;
int stop;
int end=data.size();
MsgData * cur_msg_data;
int id;
if (end==0) {cout << "your message is empty!" << endl; system("pause"); return;}
while (true)
{
id=data[start];
cur_msg_data=NewMsgData(id);
cur_msg_data->read_from_message(this,start,stop);
cur_msg_data->output_value();
cout << endl;
delete cur_msg_data;
start=stop;
if (start>=end) break;
}
system("pause");
}
void show_menu()
{
system("cls");
cout << "(1) add an int\n";
cout << "(2) add a double\n";
cout << "(3) add a string\n";
cout << "(4) show message\n";
cout << "(5) quit..." << endl;
}
int main()
{
Message message;
MsgData * cur_msg_data;
int choice;
while (true)
{
show_menu();
cin >> choice;
cin.clear();
while (cin.get()!='\n');
if (choice<1||choice>5) continue;
if (choice==4) {message.show(); continue;}
if (choice==5) break;
cur_msg_data=NewMsgData(choice-1);
while(!cur_msg_data->input_value());
message.add(cur_msg_data);
delete cur_msg_data;
}
system("cls");
cout << "bye!" << endl;
system("pause");
return 0;
}
|