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

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Live Webinar Tuesday, August 26, 2014 1:00 PM EDT Customers are more empowered and connected than ever, and the customer's journey has grown more complex. Their expectations are growing and trust is diminishing as they may interact with multiple brands through web, mobile and social channels. Considering 70% of the buying process in a complex sale is already complete before prospects are willing to engage with a live salesperson -- it's critical to understand your customers and anticipate their needs.* …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds