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

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds