• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

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.
0
KurtVon
Asked:
KurtVon
  • 4
  • 2
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now