GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
basic_types.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 /*
25  This files defines the serializer/deserializer for all basic types
26  (as well as string and pair)
27 */
28 #ifndef ARCHIVE_BASIC_TYPES_HPP
29 #define ARCHIVE_BASIC_TYPES_HPP
30 
31 #include <string>
32 #include <graphlab/serialization/serializable_pod.hpp>
33 #include <graphlab/logger/assertions.hpp>
34 
35 
36 namespace graphlab {
37  class oarchive;
38  class iarchive;
39 }
40 
41 
42 namespace graphlab {
43  namespace archive_detail {
44 
45  /** Serialization of null terminated const char* strings.
46  * This is necessary to serialize constant strings like
47  * \code
48  * oarc << "hello world";
49  * \endcode
50  */
51  template <typename OutArcType>
52  struct serialize_impl<OutArcType, const char*, false> {
53  static void exec(OutArcType& oarc, const char* const& s) {
54  // save the length
55  // ++ for the \0
56  size_t length = strlen(s); length++;
57  oarc << length;
58  oarc.write(reinterpret_cast<const char*>(s), length);
59  DASSERT_FALSE(oarc.fail());
60  }
61  };
62 
63 
64  /// Serialization of fixed length char arrays
65  template <typename OutArcType, size_t len>
66  struct serialize_impl<OutArcType, char [len], false> {
67  static void exec(OutArcType& oarc, const char s[len] ) {
68  size_t length = len;
69  oarc << length;
70  oarc.write(reinterpret_cast<const char*>(s), length);
71  DASSERT_FALSE(oarc.fail());
72  }
73  };
74 
75 
76  /// Serialization of null terminated char* strings
77  template <typename OutArcType>
78  struct serialize_impl<OutArcType, char*, false> {
79  static void exec(OutArcType& oarc, char* const& s) {
80  // save the length
81  // ++ for the \0
82  size_t length = strlen(s); length++;
83  oarc << length;
84  oarc.write(reinterpret_cast<const char*>(s), length);
85  DASSERT_FALSE(oarc.fail());
86  }
87  };
88 
89  /// Deserialization of null terminated char* strings
90  template <typename InArcType>
91  struct deserialize_impl<InArcType, char*, false> {
92  static void exec(InArcType& iarc, char*& s) {
93  // Save the length and check if lengths match
94  size_t length;
95  iarc >> length;
96  s = new char[length];
97  //operator>> the rest
98  iarc.read(reinterpret_cast<char*>(s), length);
99  DASSERT_FALSE(iarc.fail());
100  }
101  };
102 
103  /// Deserialization of fixed length char arrays
104  template <typename InArcType, size_t len>
105  struct deserialize_impl<InArcType, char [len], false> {
106  static void exec(InArcType& iarc, char s[len]) {
107  size_t length;
108  iarc >> length;
109  ASSERT_LE(length, len);
110  iarc.read(reinterpret_cast<char*>(s), length);
111  DASSERT_FALSE(iarc.fail());
112  }
113  };
114 
115 
116 
117  /// Serialization of std::string
118  template <typename OutArcType>
119  struct serialize_impl<OutArcType, std::string, false> {
120  static void exec(OutArcType& oarc, const std::string& s) {
121  size_t length = s.length();
122  oarc << length;
123  oarc.write(reinterpret_cast<const char*>(s.c_str()),
124  (std::streamsize)length);
125  DASSERT_FALSE(oarc.fail());
126  }
127  };
128 
129 
130  /// Deserialization of std::string
131  template <typename InArcType>
132  struct deserialize_impl<InArcType, std::string, false> {
133  static void exec(InArcType& iarc, std::string& s) {
134  //read the length
135  size_t length;
136  iarc >> length;
137  //resize the string and read the characters
138  s.resize(length);
139  iarc.read(const_cast<char*>(s.c_str()), (std::streamsize)length);
140  DASSERT_FALSE(iarc.fail());
141  }
142  };
143 
144  /// Serialization of std::pair
145  template <typename OutArcType, typename T, typename U>
146  struct serialize_impl<OutArcType, std::pair<T, U>, false > {
147  static void exec(OutArcType& oarc, const std::pair<T, U>& s) {
148  oarc << s.first << s.second;
149  }
150  };
151 
152 
153  /// Deserialization of std::pair
154  template <typename InArcType, typename T, typename U>
155  struct deserialize_impl<InArcType, std::pair<T, U>, false > {
156  static void exec(InArcType& iarc, std::pair<T, U>& s) {
157  iarc >> s.first >> s.second;
158  }
159  };
160 
161  } // namespace archive_detail
162 } // namespace graphlab
163 
164 #undef INT_SERIALIZE
165 #endif
166