/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/cppflow/include/cppflow/datatype.h Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/cppflow/include/cppflow/datatype.h Source File
Ratpac-two
datatype.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020 Sergio Izquierdo
4 // Copyright (c) 2020 Jiannan Liu
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
31 #ifndef INCLUDE_CPPFLOW_DATATYPE_H_
32 #define INCLUDE_CPPFLOW_DATATYPE_H_
33 
34 // C++ headers
35 #include <ostream>
36 #include <stdexcept>
37 #include <string>
38 #include <type_traits>
39 #include <typeinfo>
40 
41 namespace cppflow {
42 
43 using datatype = TF_DataType;
44 
48 inline std::string to_string(datatype dt) {
49  switch (dt) {
50  case TF_FLOAT:
51  return "TF_FLOAT";
52  case TF_DOUBLE:
53  return "TF_DOUBLE";
54  case TF_INT32:
55  return "TF_INT32";
56  case TF_UINT8:
57  return "TF_UINT8";
58  case TF_INT16:
59  return "TF_INT16";
60  case TF_INT8:
61  return "TF_INT8";
62  case TF_STRING:
63  return "TF_STRING";
64  case TF_COMPLEX64:
65  return "TF_COMPLEX64";
66  case TF_INT64:
67  return "TF_INT64";
68  case TF_BOOL:
69  return "TF_BOOL";
70  case TF_QINT8:
71  return "TF_QINT8";
72  case TF_QUINT8:
73  return "TF_QUINT8";
74  case TF_QINT32:
75  return "TF_QINT32";
76  case TF_BFLOAT16:
77  return "TF_BFLOAT16";
78  case TF_QINT16:
79  return "TF_QINT16";
80  case TF_QUINT16:
81  return "TF_QUINT16";
82  case TF_UINT16:
83  return "TF_UINT16";
84  case TF_COMPLEX128:
85  return "TF_COMPLEX128";
86  case TF_HALF:
87  return "TF_HALF";
88  case TF_RESOURCE:
89  return "TF_RESOURCE";
90  case TF_VARIANT:
91  return "TF_VARIANT";
92  case TF_UINT32:
93  return "TF_UINT32";
94  case TF_UINT64:
95  return "TF_UINT64";
96  default:
97  return "DATATYPE_NOT_KNOWN";
98  }
99 }
100 
106 template <typename T>
107 TF_DataType deduce_tf_type() {
108  if (std::is_same<T, float>::value) return TF_FLOAT;
109  if (std::is_same<T, double>::value) return TF_DOUBLE;
110  if (std::is_same<T, int32_t>::value) return TF_INT32;
111  if (std::is_same<T, uint8_t>::value) return TF_UINT8;
112  if (std::is_same<T, int16_t>::value) return TF_INT16;
113  if (std::is_same<T, int8_t>::value) return TF_INT8;
114  if (std::is_same<T, int64_t>::value) return TF_INT64;
115  if (std::is_same<T, unsigned char>::value) return TF_BOOL;
116  if (std::is_same<T, uint16_t>::value) return TF_UINT16;
117  if (std::is_same<T, uint32_t>::value) return TF_UINT32;
118  if (std::is_same<T, uint64_t>::value) return TF_UINT64;
119 
120  // decode with `c++filt --type $output` for gcc
121  throw std::runtime_error{"Could not deduce type! type_name: " + std::string(typeid(T).name())};
122 }
123 
127 inline std::ostream& operator<<(std::ostream& os, datatype dt) {
128  os << to_string(dt);
129  return os;
130 }
131 
132 } // namespace cppflow
133 
134 #endif // INCLUDE_CPPFLOW_DATATYPE_H_
std::ostream & operator<<(std::ostream &os, datatype dt)
Definition: datatype.h:127
TF_DataType deduce_tf_type()
Definition: datatype.h:107
std::string to_string(datatype dt)
Definition: datatype.h:48