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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds