Oracle Coherence for C++ API
Release 3.6.0.0

E15728-01

coherence/io/pof/TypedSerializer.hpp

00001 /*
00002 * TypedSerializer.hpp
00003 *
00004 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_TYPED_SERIALIZER_HPP
00017 #define COH_TYPED_SERIALIZER_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/io/pof/PofReader.hpp"
00022 #include "coherence/io/pof/PofSerializer.hpp"
00023 #include "coherence/io/pof/PofWriter.hpp"
00024 
00025 // ----- helper function prototypes -----------------------------------------
00026 
00027 template<class T> extern void serialize(coherence::io::pof::PofWriter::Handle hOut, const T& t);
00028 template<class T> extern T deserialize(coherence::io::pof::PofReader::Handle hIn);
00029 
00030 COH_OPEN_NAMESPACE3(coherence,io,pof)
00031 
00032 
00033 /**
00034 * A PofSerializer implementation that supports the serialization and
00035 * deserialization of any class to and from a POF stream, by delegating the
00036 * serialization logic to templated free functions.
00037 *
00038 * The following set of free functions must be defined for the templated type:
00039 * <ul>
00040 *   <li>template<> void serialize<T>(PofWriter::Handle, const T&)</li>
00041 *   <li>template<> T deserialize(PofReader::Handle)</li>
00042 * </ul>
00043 *
00044 * This class is well suited for use with the coherence::lang::Managed template
00045 * wrapper class. Combining the two allows for the addition of serialization
00046 * support to existing non-coherence classes.
00047 *
00048 * @see coherence::lang::Managed
00049 * @author mf  2008.07.07
00050 */
00051 template<class T>
00052 class TypedSerializer
00053     : public class_spec<TypedSerializer<T>,
00054         extends<Object>,
00055         implements<PofSerializer> >
00056     {
00057     friend class factory<TypedSerializer<T> >;
00058 
00059     // ----- constructors ---------------------------------------------------
00060 
00061     protected:
00062         /**
00063         * Create a new TypedSerializer for the user type with template
00064         * parameter type.
00065         *
00066         * @return the new TypedSerializer
00067         */
00068         TypedSerializer()
00069             {
00070             }
00071 
00072 
00073     // ----- PofSerializer interface ----------------------------------------
00074 
00075     public:
00076         /**
00077         * {@inheritDoc}
00078         */
00079         virtual void serialize(PofWriter::Handle hOut, Object::View v) const
00080             {
00081             ::serialize<typename T::View>(hOut, cast<typename T::View>(v));
00082             hOut->writeRemainder(NULL);
00083             }
00084 
00085         /**
00086         * {@inheritDoc}
00087         */
00088         virtual Object::Holder deserialize(PofReader::Handle hIn) const
00089             {
00090             TypedHolder<T> oh = ::deserialize<TypedHolder<T > >(hIn);
00091             hIn->readRemainder();
00092             return oh;
00093             }
00094     };
00095 
00096 COH_CLOSE_NAMESPACE3
00097 
00098 /**
00099 * Default serialization implementation which delegates to the serialization
00100 * for a boxed type. This implementation is targeted towards Managed objects
00101 * essentially delegating to serialization functions for the non-managed custom
00102 * type.
00103 *
00104 * @param hOut  the stream to write the object to
00105 * @param t     the object to write out
00106 */
00107 template<class T> void serialize(coherence::io::pof::PofWriter::Handle hOut, const T& t)
00108     {
00109     serialize<typename T::ValueType::BoxedType>(hOut, *t);
00110     }
00111 
00112 /**
00113 * Default deserialization implementation which delegates to the deserialization
00114 * for a boxed type. This implementation is targeted towards Managed objects
00115 * essentially delegating to serialization functions for the non-managed custom
00116 * type.
00117 *
00118 * @param hIn  the stream to read the object from
00119 *
00120 * @return     the deserialized object
00121 */
00122 template<class T> T deserialize(coherence::io::pof::PofReader::Handle hIn)
00123     {
00124     return T::ValueType::create(deserialize<typename T::ValueType::BoxedType>(hIn));
00125     }
00126 
00127 #endif // COH_TYPED_SERIALIZER_HPP
Copyright © 2000, 2010, Oracle and/or its affiliates. All rights reserved.