GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
has_save.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 
26 #ifndef HAS_SAVE_HPP
27 #define HAS_SAVE_HPP
28 #include <typeinfo>
29 
30 namespace graphlab {
31 namespace archive_detail {
32 
33  /** SFINAE method to detect if a class T
34  * implements a function void T::save(ArcType&) const
35  *
36  * If T implements the method, has_save_method<ArcType,T>::value will be
37  * true. Otherwise it will be false
38  */
39  template<typename ArcType, typename T>
40  struct has_save_method
41  {
42  template<typename U, void (U::*)(ArcType&) const> struct SFINAE {};
43  template<typename U> static char Test(SFINAE<U, &U::save>*);
44  template<typename U> static int Test(...);
45  static const bool value = sizeof(Test<T>(0)) == sizeof(char);
46  };
47 
48  /**
49  * save_or_fail<ArcType, T>(arc, t)
50  * will call this version of the function if
51  * T implements void T::save(ArcType&) const.
52  *
53  * save_or_fail<ArcType, T>(arc, t) will therefore save the class successfully
54  * if T implements the save function correctly. Otherwise, calling
55  * save_or_fail will print an error message.
56  */
57  template <typename ArcType, typename ValueType>
58  typename boost::enable_if_c<has_save_method<ArcType, ValueType>::value, void>::type
59  save_or_fail(ArcType& o, const ValueType &t) {
60  t.save(o);
61  }
62 
63  /**
64  * save_or_fail<ArcType, T>(arc, t)
65  * will call this version of the function if
66  *
67  * save_or_fail<ArcType, T>(arc, t) will therefore save the class successfully
68  * if T implements the save function correctly. Otherwise, calling
69  * save_or_fail will print an error message.
70  * T does not implement void T::save(ArcType&) const.
71  */
72  template <typename ArcType, typename ValueType>
73  typename boost::disable_if_c<has_save_method<ArcType, ValueType>::value, void>::type
74  save_or_fail(ArcType& o, const ValueType &t) {
75  ASSERT_MSG(false,"Trying to serializable type %s without valid save method.", typeid(ValueType).name());
76  }
77 
78 } // archive_detail
79 } // graphlab
80 
81 #endif
82