Serializing data in a template

I have a template class that needs to parse a string into the user-defined type.

      void parse(const std::string& value)
      {
            _Template_defined_type data;
            std::stringstream stream_value(value, std::stringstream::in);
            stream_value >> data;
      }

This works like a charm for everything except, ironically, strings.  Strings end at the first space instead of the end of the passed string.

Obviously I can't use getline since the data may not be a strings (it could be an int, float, or user-defined class) so I either need a way to serialize the whole line for strings or a different general-purpose way to parse the data.

Thanks.
LVL 11
KurtVonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
What about escaping the spaces upon saving (or replacing them with e.g. an underscore)? .... just playing with ideas...
0
KurtVonAuthor Commented:
Nope, the string is user-entered.  I don't serialize it out at all.  And since the template may be based on a primitive type I can't even spec out that the user defined class have certain functions.

I suppose I could create a string-specific version of the template, but trusting whoever uses the template to use it right may be a bit over-hopeful.  Templates don't seem to offer any way to switch on the type they use, do they?
0
jkrCommented:
>>Templates don't seem to offer any way to switch on the type they use, do
>>they?

With RTTI, you can, e.g.


const type_info& tdata = typeid(data);
 
string s;
 
const type_info& ts = typeid(s);
 
if (ts == tdata) {
 
  // is a string, go on with 'getline()'
}

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

KurtVonAuthor Commented:
Looks like that might work (hacks in the library are acceptable as long as they don't clutter the code: so says the almighty project manager :-) )

I'll give it a try.
0
rstaveleyCommented:
Alternatively you can use template specialization. Seems wrong to insist on RTTI, when you are working with templates and therefore the static type system.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KurtVonAuthor Commented:
Hmm, so better yet would be to rewrite the parse function as

      void MyTemplate<_Tdt>::parse(const std::string& value)
      {
            _Tdt data;
            parse(data, value);
            // Do what I need to do.
      }

      template<class T>
      void parse<T& data, const std::string& value)
      {
            std::stringstream stream_value(value, std::stringstream::in);
            stream_value >> data;
      }

Then allow the user to override the template function as desired!

      void parse(std::string& data, const std::string& value)
      {
            std::stringstream stream_value(value, std::stringstream::in);
            getline(stream_value, data);
      }

Okay, I officially feel stupid for not thinking of function templates.
0
rstaveleyCommented:
I feel stupid at regular intervals during the average day, but making it official seems rather dignified :-)
0
KurtVonAuthor Commented:
Okay, the function tamplates with specialized string handling works like a charm.  I didn't expect it not to, I just was worried some issue would crop up.

Better yet, I can use this in a few other places where an issue hasn't cropped up, but could.

I'm giddy as a schoolgirl, which is not in the least dignified, I might add.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.