[serialization] transparently treating a class like a built-in type
I have some xml archives that store a lot of floating point values. I would like to read these into a program that is designed to easily switch between using floats and a custom fixed-point type via a typedef. The way that would be easiest would be to add a serialize() method to the fixed-point class definition. However, the xml archive requires that I use an nvp wrapper. Since the data was originally serialized out as plain floats, the data looks something like this: <x>0.95260203</x> <y>0.87517798</y> <z>0.54323343</z> whereas, to serialize this into my fixed-point type, it would need another level of indirection due to the additional required nvp wrapper like so: <x><value>0.95260203</value></x> <y><value>0.87517798</value></y> <z><value>0.54323343</value></z> Is there a way to accomplish what I'm trying to do? The fixed-point type is convertible from (but not to) a float if that helps.
Zitat von Kenneth Riddile <kfriddile@gmail.com>:
I have some xml archives that store a lot of floating point values. I would like to read these into a program that is designed to easily switch between using floats and a custom fixed-point type via a typedef. The way that would be easiest would be to add a serialize() method to the fixed-point class definition. However, the xml archive requires that I use an nvp wrapper. Since the data was originally serialized out as plain floats, the data looks something like this:
<x>0.95260203</x> <y>0.87517798</y> <z>0.54323343</z>
I'm not sure if it was intended to be used that way, but you could use Boost.Serialization's versioning. namespace serialization{ struct version<your_type> : mpl::integral_c<unsigned int,mpl::if_int_then_0_else_1>{}; } void load(Archive &ar,unsigned int version){ if(version == 0) load fixed point... else load floating point... ...into whatever type is currently typedef'ed } wrt to your "value" "indirection", just load the float directly, or if that's not an option because of encapsulation use a temporary and initialize "x" later on. ar >> make_nvp("x",x.value); instead of ar & NVP(x);
Kenneth Riddile wrote:
I have some xml archives that store a lot of floating point values. I would like to read these into a program that is designed to easily switch between using floats and a custom fixed-point type via a typedef. The way that would be easiest would be to add a serialize() method to the fixed-point class definition. However, the xml archive requires that I use an nvp wrapper. Since the data was originally serialized out as plain floats, the data looks something like this:
<x>0.95260203</x> <y>0.87517798</y> <z>0.54323343</z>
whereas, to serialize this into my fixed-point type, it would need another level of indirection due to the additional required nvp wrapper like so:
<x><value>0.95260203</value></x> <y><value>0.87517798</value></y> <z><value>0.54323343</value></z>
Is there a way to accomplish what I'm trying to do? The fixed-point type is convertible from (but not to) a float if that helps.
This doesn't have anything to do with serialization per se. After loading, call a function which does whatever conversion you want to do. Robert Ramey
On 7/27/2010 12:38 PM, Robert Ramey wrote:
Kenneth Riddile wrote:
I have some xml archives that store a lot of floating point values. I would like to read these into a program that is designed to easily switch between using floats and a custom fixed-point type via a typedef. The way that would be easiest would be to add a serialize() method to the fixed-point class definition. However, the xml archive requires that I use an nvp wrapper. Since the data was originally serialized out as plain floats, the data looks something like this:
<x>0.95260203</x> <y>0.87517798</y> <z>0.54323343</z>
whereas, to serialize this into my fixed-point type, it would need another level of indirection due to the additional required nvp wrapper like so:
<x><value>0.95260203</value></x> <y><value>0.87517798</value></y> <z><value>0.54323343</value></z>
Is there a way to accomplish what I'm trying to do? The fixed-point type is convertible from (but not to) a float if that helps.
This doesn't have anything to do with serialization per se.
After loading, call a function which does whatever conversion you want to do.
Robert Ramey
I'm not sure I described things well enough. Say I have the following: typedef float Real; typedef Vector3<Real> Vector; and I have some xml archives containing serialized Vector objects that look like this: <x>-0.70032406</x> <y>-0.65001774</y> <z>0.29499683</z> Now, I won't be able to read in this archive if I change the Real typedef like so: typedef FixedPointClass Real; Even if I make FixedPointClass serializable, the data would need to contain an additional name-value pair for it to be readable, like so: <x><value>-0.70032406</value></x> <y><value>-0.65001774</value></y> <z><value>0.29499683</value></z> I know that I can fix this problem by splitting the serialization of Vector into save and load and explicitly loading floats and doing the conversion. The problem with that is that I have several other types in the same situation as Vector. I don't want to do it for all of them if I don't have to. I would rather do something once that's specific to FixedPointClass since that makes the most sense. Any suggestions or should I get started on writing lots of save/load templates?
I'm not sure I described things well enough. Say I have the following: typedef float Real; typedef Vector3<Real> Vector;
and I have some xml archives containing serialized Vector objects that look like this:
<x>-0.70032406</x> <y>-0.65001774</y> <z>0.29499683</z>
Now, I won't be able to read in this archive if I change the Real typedef like so:
typedef FixedPointClass Real;
Even if I make FixedPointClass serializable, the data would need to contain an additional name-value pair for it to be readable, like so:
<x><value>-0.70032406</value></x> <y><value>-0.65001774</value></y> <z><value>0.29499683</value></z>
Have you looked at the documentatoin regarding versioning? Does that not help? Robert Ramey
participants (3)
-
Kenneth Riddile
-
Robert Ramey
-
Stefan Strasser