GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
robust_cast.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 GRAPHLAB_ROBUST_CAST_HPP
25 #define GRAPHLAB_ROBUST_CAST_HPP
26 
27 #include <boost/utility.hpp>
28 #include <boost/type_traits/is_convertible.hpp>
29 namespace graphlab {
30  /** robust_cast performs a static cast from type A to type B
31  if a cast can be done. Return B() otherwise */
32 
33  template <typename Target, typename Source>
34  typename boost::disable_if_c<boost::is_convertible<Source, Target>::value,
35  Target>::type
36  robust_cast(const Source &h) {
37  return Target();
38  }
39 
40  template <typename Target, typename Source>
41  typename boost::enable_if_c<boost::is_convertible<Source, Target>::value,
42  Target>::type
43  robust_cast(const Source &h) {
44  return (Target)h;
45  }
46 }
47 
48 #endif
49 
50