Solved

Serializer

Posted on 2008-10-03
13
274 Views
Last Modified: 2010-04-16
Referencing the source below, for simplicity, I've only include the types: float, double,
 unsigned char and short.  I suspect the aim is to serialize a byte stream.  At present, my attempt
 to serialize a double precision value has been futile (the code supposidely works).

 The request:
:
   a) I'd like examples showing each operations on PutData and GetData for each type
      For instance, my attempt a PutData:

# include "iserializable.h"
# include <iostream>

int main () {
  ISerializable is ;
  double source = 5.;
  char buffer [ 40 ] = { 0 };
  char *ptr = is.PutData ( buffer, source ) ;
  std::cin.get() ;
}

Trouble is the contents of the variable ptr is always zero.  Of course, i'm not sure how one can pack the contents of a double precision value into a char array.  In the interim I'll continue to review documents online about serialization so maybe they'll shed some light.
   
   b) I believe a generic solution exists.  That said, instead of all this overloaded mess,
      I'd like code that will utilize a template/generic approach
// iserializable.h

# ifndef ISERIALIZABLE_H

# define ISERIALIZABLE_H
 

# include <iostream>
 

class ISerializable {
 

  template <typename T>

  void Swap( T& var ) {

    char* start,*end;

    char tmp;

    start = (char * ) &var;

    end = (char *)&var;

    end += sizeof(T)-1;

    while(start < end) {

      tmp = *start;

      *start = *end;

      *end = tmp;

      start++;end--;

    }

  }
 

public :
 
 

//////////////////////////////////////////

/// @name Overloaded functions using "double"

//////////////////////////////////////////

   char* PutData(char* out, const double& source) {

     *(double *)out = source;

     return out + sizeof(double);

   }

   char* GetData(double& target, char* source) {

     target = *(double *)source;

     return source + sizeof(double);

   }

   char* PutSwappedData(char* out, const double& source) {

     *(double *)out = source;

     Swap(*(double *)out);

     return out + sizeof(double);

   }

   char* GetSwappedData(double& target, char* source) {

     target = *(double *)source;

     Swap(target);

     return source + sizeof(double);

   }

  //////////////////////////////////////////

  /// @name Overloaded functions using "float"

  //////////////////////////////////////////

   char* PutData(char* out, const float& source) {

     *(float *)out = source;

     return out + sizeof(float);

   }

   char* GetData(float& target, char* source) {

     target = *(float *)source;

     return source + sizeof(float);

   }

   char* PutSwappedData(char* out, const float& source) {

     *(float *)out = source;

     Swap(*(float *)out);

     return out + sizeof(float);

   }

   char* GetSwappedData(float& target, char* source) {

     target = *(float *)source;

     Swap(target);

     return source + sizeof(float);

   }
 

  ///////////////////////////////////////////////

  /// @name Overloaded functions using "unsigned char"

  ///////////////////////////////////////////////

   char* PutData(char* out, const unsigned char& source) {

     *(unsigned char *)out = source;

     return out + sizeof(unsigned char);

   }

   char* GetData(unsigned char& target, char* source) {

     target = *(unsigned char *)source;

     return source + sizeof(unsigned char);

   }

   char* PutSwappedData(char* out, const unsigned char& source) {

     *(unsigned char *)out = source;

     return out + sizeof(unsigned char);

   }

   char* GetSwappedData(unsigned char& target, char* source) {

     target = *(unsigned char *)source;

     return source + sizeof(unsigned char);

   }
 

  ///////////////////////////////////////////////

  /// @name Overloaded functions using short*

  ///////////////////////////////////////////////

   char* PutData(char* out, short* source,unsigned length16BitUnits) {

     memcpy(out, (char *)source, length16BitUnits*2);

     return out + length16BitUnits*2;

   }
 

   char* GetData(short* target, char* source,unsigned length16BitUnits) {

     memcpy((char *)target,source,length16BitUnits*2);

     return source+length16BitUnits*2;

   }
 

   char* PutSwappedData(char* out, short* source,unsigned length16BitUnits) {

     unsigned i;

     char* tmp = PutData(out,source,length16BitUnits);

     short* tmpShort = (short *)out;

     for(i = 0; i < length16BitUnits; i++) {

       Swap(*tmpShort);

       tmpShort++;

     }

     return tmp;

   }

   char* GetSwappedData(short* target, char* source,unsigned length16BitUnits) {

     unsigned i;

     char* tmp = GetData(target,source,length16BitUnits);

     short* tmpShort=target;

     for(i = 0; i < length16BitUnits; i++) {

       Swap(*tmpShort);

       tmpShort++;

     }

     return tmp;

   }
 

};
 

#endif

Open in new window

0
Comment
Question by:forums_mp
  • 8
  • 5
13 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 22640075
>> Trouble is the contents of the variable ptr is always zero.

Do you mean that ptr is zero ? Or that the data it points to is 0 ? Or ???

Note that ptr will point AFTER the data that was just put in it.
0
 

Author Comment

by:forums_mp
ID: 22642524
Infinity if my debugging skills using MSVC .NET 05 is correct the data it points to is zero.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22642785
As I said : ptr will point AFTER the double value that was serialized ... So, assuming that memory was initialized to 0, it's normal that you see a 0 value there :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22700857
I got no feedback to my last post. Did you understand it ? Do you have further problems/questions ?
0
 

Author Comment

by:forums_mp
ID: 22716649
|| Do you have further problems/questions ?

Of course I did.  

There's still the issue of item b ( well maybe I forgot the question sign at the end .. big deal ) but more to the point the code (I'm not a developer so you have to forgive me here ) was written by hired help and I though overhaul was needed hence item b.   Found out through a different medium that the code is wrought with errors to begin with primarily because of portability issues with binary serialization and the potential for core dumps/undefined behavior when doing:

    '*(unsigned char *)out = source;'
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 22716788
Serializing data should not be done generically imo, but should rather be handled on a case-by-case basis. Meaning that for each type that should be serializable, code has to be written to handle serializing it.

This is especially true if the serialized data will be transferred between different systems/platforms. Integers might have different sizes on different systems, floating point values might have a different representation, etc.
A standard serialized format has to be decided on, which will then be used consistently.

An interesting alternative might be an existing serialization library, like boost's :

        http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/index.html



Btw, the reason I didn't post any more replies, was because I was waiting for a response from you after my post on the 5th.
If you want to continue with this question, I'll be more than happy to help you further and answer any questions you still have about this.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:forums_mp
ID: 22729517
|| An interesting alternative might be an existing serialization library, like boost's :
Can't use boost, hence the reason the need to generate one from scratch.    The code will be executed on a soft core processor hosted on the Altera family of FPGAs.  
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22729787
Then you can use similar techniques as the ones used in boost ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22971327
May I ask why you gave a C grade ? That usually means that the answer given was very bad. I do not think that was deserved, and would like some feedback about this grade, as well as a response to my last post.

If you still need assistance with this, then you need to explain what it is you need assistance with - I can't read your mind.
If your question was answered, then a C grade is not appropriate.

Please explain - I want to learn from my mistakes.
0
 

Author Comment

by:forums_mp
ID: 22971460
Using similar techniques like boost simply doesn't cut it.  The norm these days for me is to seek alternative solutions elsewhere. In doing so individuals will highlight what's wrong with the code then offer up solutions that I could literally drop in my application.  'use similar techniques like boost' doesn't warrant 250 damn points and I refuse to get caught up in the trivial back and forth melodrama.  I obtained a solution - REAL DAMN SOURCE not handwaving - that I was able to work with
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22971615
If your question wasn't answered, then you can ask to get your points refunded.
A C grade says that there was an answer to the question, but it was very bad. I still don't see what's bad about it, aspecially since I didn't get any feedback from you about it. Help me understand where I went wrong, so I can do better next time.


My point is that giving someone a C grade without an explanation (saying you can't do what was suggested one month later doesn't count I'm afraid) and without giving that person a chance to earn an A grade is quite rude and unrespectful. We're all volunteers here, and sacrifice our own free time to try to help others like you. Maybe you don't realize that ?
0
 

Author Comment

by:forums_mp
ID: 22972131
|| If your question wasn't answered, then you can ask to get your points refunded.
I'd love to except I'll get into a litany of discussions with the moderator who'll then ask me to upload the solution received.  I REFUSE to do that especially when I paid a coder on an alternate site $30-$40/hour for a solution.  Your solution - with all due respect doesn't warrant 250 points and I'm afraid there isn't much recourse for us buyers on this site.  I made every attempt to offer up reasonable points given the level of difficulty surround the problem

|| My point is that giving someone a C grade without an explanation
|| (saying you can't do what was suggested one month later doesn't count I'm afraid)
The problem with this site is you're often forced to make a selection EVEN if the solution goes against conventional wisdom.   I often leave the question open in the hopes that someone else will offer an viable solution.  You ask me the exchange between here and alternate site could be summarized as follows (maybe this will help you next time):  

<me>Here's what I'm attempting to do.
you:  Here's an interesting alternative
<me> Can't use the alternative
you: Use similar techniques

-- VERSUS  (alternate site) --
<me> Here's what I'm attempting to do.
source: Here's whats wrong with the approach
source: Have I considered the alternatives
<me> Can't use the alternative
source: Here's what you ought to do ( REAL source that I could work with )

|| We're all volunteers here, and sacrifice our own free time to try
|| to help others like you. Maybe you don't realize that ?
Maybe you don't realize I'm paying for this.  It's not 'free' for me.

At this juncture I'd recommend you take the issue up with the moderator.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22972177
I see you don't get the point :) That's fine - disappointing, but fine.


>>    b) I believe a generic solution exists.  That said, instead of all this overloaded mess,
>>       I'd like code that will utilize a template/generic approach

That's exactly what the boost serialization library does. If for some reason you can't / won't use boost, but still want to do the same thing, then you'll have to re-implement the boost serialization library yourself using "similar techniques".
This is not a code-writing site where you can expect people to do such a thing for you. Developing a good generic serialization library is not a small thing to do, and that takes a LOT of time.

You apparently don't understand what this site is about, and how things work. Because your expectations are too high, and your feedback too little/slow, you end up wasting not only your own time, but also the expert's time. The sooner you realize that, the better it is for everybody :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 107
Fully specialized class template function 21 118
C++ Language error 28 149
How to split this in C++ 4 78
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now