Skip to content

Commit 3cf909e

Browse files
author
Hoyt Koepke
committed
Rewrite of flexible type converter logic.
Rewrote the flexible type converter logic to be much more readible and flexible. This is the next stage of the flexible type improvements. The logic used a different metaprogramming method documented in flexible_type_converter.hpp. This gives much better control over error messages and better flexibility in adding additional types. Also discovered and fixed some minor bugs with the gl_string classes.
1 parent a6823b0 commit 3cf909e

8 files changed

+914
-547
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright (C) 2015 Dato, Inc.
3+
* All rights reserved.
4+
*
5+
* This software may be modified and distributed under the terms
6+
* of the BSD license. See the LICENSE file for details.
7+
*/
8+
9+
#ifndef GRAPHLAB_FLEXIBLE_TYPE_FLEXIBLE_TYPE_CONVERSION_UTILS_HPP
10+
#define GRAPHLAB_FLEXIBLE_TYPE_FLEXIBLE_TYPE_CONVERSION_UTILS_HPP
11+
12+
#include <vector>
13+
#include <map>
14+
#include <unordered_map>
15+
#include <tuple>
16+
#include <type_traits>
17+
#include <util/code_optimization.hpp>
18+
#include <flexible_type/flexible_type.hpp>
19+
#include <flexible_type/type_traits.hpp>
20+
21+
namespace graphlab {
22+
23+
namespace flexible_type_internals {
24+
25+
static void throw_type_conversion_error(const flexible_type& val, const char *type) {
26+
std::ostringstream ss;
27+
ss << "Type conversion failure in flexible_type converter: expected "
28+
<< type << "; got " << flex_type_enum_to_name(val.get_type());
29+
30+
throw ss.str();
31+
}
32+
33+
template <typename Arg>
34+
static void __unpack_args(std::ostringstream& ss, Arg a) {
35+
ss << a;
36+
}
37+
38+
template <typename Arg, typename... OtherArgs>
39+
static void __unpack_args(std::ostringstream& ss, Arg a1, OtherArgs... a2) {
40+
ss << a1;
41+
__unpack_args(ss, a2...);
42+
}
43+
44+
template <typename... PrintArgs>
45+
static void throw_type_conversion_error(const flexible_type& val, const char* type, PrintArgs... args) {
46+
std::ostringstream ss;
47+
ss << "Type conversion failure in flexible_type converter: expected " << type;
48+
__unpack_args(ss, args...);
49+
ss << "; got " << flex_type_enum_to_name(val.get_type());
50+
51+
throw ss.str();
52+
}
53+
54+
////////////////////////////////////////////////////////////////////////////////
55+
56+
template <typename T>
57+
static void __get_t(flexible_type& dest, const T& src) {
58+
convert_to_flexible_type(dest, src);
59+
}
60+
61+
template <typename T>
62+
static void __get_t(double& dest, const T& src) {
63+
dest = src;
64+
}
65+
66+
template <size_t idx, typename C, typename... Args>
67+
void __unpack_tuple(C& dest, const std::tuple<Args...>& src,
68+
typename std::enable_if<idx == sizeof...(Args)>::type* = NULL) {}
69+
70+
template <size_t idx, typename C, typename... Args>
71+
void __unpack_tuple(C& dest, const std::tuple<Args...>& src,
72+
typename std::enable_if<idx != sizeof...(Args)>::type* = NULL) {
73+
__get_t(dest[idx], std::get<idx>(src));
74+
__unpack_tuple<idx + 1>(dest, src);
75+
}
76+
77+
template <typename C, typename... Args>
78+
static void unpack_tuple(C& dest, const std::tuple<Args...>& src) {
79+
__unpack_tuple<0>(dest, src);
80+
}
81+
82+
////////////////////////////////////////////////////////////////////////////////
83+
84+
85+
template <typename T>
86+
static void __set_t(T& dest, const flexible_type& src) {
87+
convert_from_flexible_type(dest, src);
88+
}
89+
90+
template <typename T>
91+
static void __set_t(T& dest, double src) {
92+
dest = static_cast<T>(src);
93+
}
94+
95+
template <size_t idx, typename C, typename... Args>
96+
void __pack_tuple(std::tuple<Args...>& dest, const C& src,
97+
typename std::enable_if<idx == sizeof...(Args)>::type* = NULL) {}
98+
99+
template <size_t idx, typename C, typename... Args>
100+
void __pack_tuple(std::tuple<Args...>& dest, const C& src,
101+
typename std::enable_if<idx != sizeof...(Args)>::type* = NULL) {
102+
__set_t(std::get<idx>(dest), src[idx]);
103+
__pack_tuple<idx + 1>(dest, src);
104+
}
105+
106+
template <typename C, typename... Args>
107+
static void pack_tuple(std::tuple<Args...>& dest, const C& src) {
108+
__pack_tuple<0>(dest, src);
109+
}
110+
111+
}}
112+
113+
114+
#endif

0 commit comments

Comments
 (0)