GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
serialize_to_from_string.hpp
1 /*
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 #ifndef SERIALIZE_TO_FROM_STRING_HPP
25 #define SERIALIZE_TO_FROM_STRING_HPP
26 #include <sstream>
27 #include <boost/iostreams/stream.hpp>
28 
29 namespace graphlab {
30  /**
31  * \ingroup group_serialization
32  * \brief Serializes a object to a string
33  *
34  * Converts a \ref serializable object t to a string
35  * using the serializer.
36  *
37  * \tparam T the type of object to serialize. Typically
38  * will be inferred by the compiler.
39  *
40  * \param t The object to serializer
41  * \returns A string containing a serialized form of t
42  *
43  * \see deserialize_from_string()
44  */
45  template <typename T>
46  inline std::string serialize_to_string(const T &t) {
47  std::stringstream strm;
48  oarchive oarc(strm);
49  oarc << t;
50  strm.flush();
51  return strm.str();
52  }
53 
54 
55  /**
56  * \ingroup group_serialization
57  * \brief Deserializes a object from a string
58  *
59  * Deserializes a \ref serializable object t from a string
60  * using the deserializer.
61  *
62  * \tparam T the type of object to deserialize. Typically
63  * will be inferred by the compiler.
64  *
65  * \param s The string to deserialize
66  * \param t A reference to the object which will contain
67  * the deserialized object when the function returns
68  *
69  * \see serialize_from_string()
70  */
71  template <typename T>
72  inline void deserialize_from_string(const std::string &s, T &t) {
73  boost::iostreams::stream<boost::iostreams::array_source>
74  istrm(s.c_str(), s.length());
75  iarchive iarc(istrm);
76  iarc >> t;
77  }
78 }
79 
80 #endif