Serializable Base Object

Environment: VC5, NT4 SP5

Since a started programming, I have used CodeGuru4s articles to learm how to write more efficient code. Now, it's my turn to make a contribution. Please keep in mind that I am beginner so if I have done something wrong or you know a better way to do this, please feel free to teach me.

I tried to define a generic base class that has generic code for serializing data. This class can be used for comunicating over a network or writing the object4s member variables to disk without you having to implemet serialization code in each of the base class' derived classes.

The class defines an internal type (BS_SIZE) that holds each member4s size. You can change this as you need. This class was made to work in both 16 and 32-bit environments, In fact, I use BS_SIZE.because many of my remote clients are running on older XT machines.

Here are the steps that you need to do in order to use the class.

  1. Define your class ID
  2. Enumerate your members
  3. Derive your class from this base class
  4. Modify the constructor to add your members
  5. Define your Set and Get Methods
Here is a quick example of how to use the class. You will find a more verbose example in the downloadable demo project at the bottom of this article.
// in this case
// member1  = int
// member 2 = char *

#define MY_ID 0x25

// members access order
enum {	member1, member2 };

class MyClass : public _BS_OBJ_
{
 // constructor 
 MyClass()
 {
  // set the object ID
  SetID(MY_ID);

  // add members
  // you will alway access member in order
  AddMember();
  AddMember();
 }

 // set methods
 bool SetMember1(int & i) { return SetMemberData(member1, &i, sizeof(int)); }
 bool SetMember2(char * str) { return SetMemberData(member2, &i, strlen(str) + 1); }

 // get methods
 int    GetMember1() 
 { 
  int * i = (int *)GetMemberData(member1);
  return *i; 
 }

 char * GetMember2() 
 { 
  return (char *)GetMemberData(member1); 
 } 
}

6. ready to use

int main()
{
 // vars to be used
 int     i      = 250;
 char *  pch    = "string for test !"; 
 void *  buffer = NULL;

 // internal _BS_OBJ_ type (unsigned long)
 BS_SIZE size   = 0; 

 // objects
 MyClass my1, my2;

 // set data on first object
 my1.SetMember1(i);
 my1.SetMember2(pch);

 //serialize it
 size = my1.Serialize(&buffer);

 // get it on secong object
 my2.Deserialize(buffer);

 // just to teste process
 if(my1.GetMember1() == my2.GetMember1())
  printf("OK!");
 else
  printf("ERROR!"); 

 return 0;
}

I am currently working on a Dynamic Object and would apreciate any help .

Downloads

Download demo project - 8 Kb


Comments

  • First thing...

    Posted by Legacy on 01/30/2000 12:00am

    Originally posted by: Shekar Narayanan

    The first thing I would like to tell you is Whatever you do - be confident. Don't start with any inferior complex. you don't have to apologize to anybody.

    Reply
  • Shekar Narayanan

    Posted by Legacy on 01/30/2000 12:00am

    Originally posted by: Ignoramus

    Even worse than apologia is advertising the person who apologises, since it is a form of misplaced manners to apologise in advance, you should concentrate upon helping and advancing this persons knowledge,you could start by ignoring the apologia and complement his work in some other area.


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds