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

  • 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
  • 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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds