Empirical
TypeID.h
Go to the documentation of this file.
1 // This file is part of Empirical, https://github.com/devosoft/Empirical
2 // Copyright (C) Michigan State University, 2016.
3 // Released under the MIT Software license; see doc/LICENSE
4 //
5 // TypeID provides an easy way to convert types to strings.
6 //
7 //
8 // Developer notes:
9 // * Fill out remaining standard library classes (as possible)
10 // * Default to type_traits typeid rather than Unknown
11 
12 #ifndef EMP_TYPE_ID_H
13 #define EMP_TYPE_ID_H
14 
15 #include <sstream>
16 #include <string>
17 
18 #include "../base/vector.h"
19 #include "TypePack.h"
20 
21 
22 namespace emp {
23 
24  // Generic TypeID structure for when none of the specialty cases trigger.
25  template<typename T> struct TypeID {
26  template<typename TEST> using TypeIDFilter = decltype(&TEST::TypeID);
27  struct UnknownID { static std::string TypeID() { return "Unknown"; } };
28  static std::string GetName() {
29  using print_t = typename TypePack<T,UnknownID>::template find_t<TypeIDFilter>;
30  return print_t::TypeID();
31  }
32  };
33 
34  // Built-in types.
35  template<> struct TypeID<void> { static std::string GetName() { return "void"; } };
36 
37  template<> struct TypeID<bool> { static std::string GetName() { return "bool"; } };
38  template<> struct TypeID<double> { static std::string GetName() { return "double"; } };
39  template<> struct TypeID<float> { static std::string GetName() { return "float"; } };
40 
41  template<> struct TypeID<char> { static std::string GetName() { return "char"; } };
42  template<> struct TypeID<char16_t> { static std::string GetName() { return "char16_t"; } };
43  template<> struct TypeID<char32_t> { static std::string GetName() { return "char32_t"; } };
44 
45  template<> struct TypeID<int8_t> { static std::string GetName() { return "int8_t"; } };
46  template<> struct TypeID<int16_t> { static std::string GetName() { return "int16_t"; } };
47  template<> struct TypeID<int32_t> { static std::string GetName() { return "int32_t"; } };
48  template<> struct TypeID<int64_t> { static std::string GetName() { return "int64_t"; } };
49  template<> struct TypeID<uint8_t> { static std::string GetName() { return "uint8_t"; } };
50  template<> struct TypeID<uint16_t> { static std::string GetName() { return "uint16_t"; } };
51  template<> struct TypeID<uint32_t> { static std::string GetName() { return "uint32_t"; } };
52  template<> struct TypeID<uint64_t> { static std::string GetName() { return "uint64_t"; } };
53 
54  // Check for type attributes...
55  template<typename T> struct TypeID<T*> {
56  static std::string GetName() { return TypeID<T>::GetName() + '*'; }
57  };
58 
59  // Tools for using TypePack
60  template<typename T, typename... Ts> struct TypeID<emp::TypePack<T,Ts...>> {
61  static std::string GetTypes() {
62  std::string out = TypeID<T>::GetName();
63  if (sizeof...(Ts) > 0) out += ",";
64  out += TypeID<emp::TypePack<Ts...>>::GetTypes();
65  return out;
66  }
67  static std::string GetName() {
68  std::string out = "emp::TypePack<";
69  out += GetTypes();
70  out += ">";
71  return out;
72  }
73  };
74  template<> struct TypeID< emp::TypePack<> > {
75  static std::string GetTypes() { return ""; }
76  static std::string GetName() { return "emp::TypePack<>"; }
77  };
78 
79  // Generic TemplateID structure for when none of the specialty cases trigger.
80  template <typename T> struct TemplateID {
81  static std::string GetName() { return "UnknownTemplate"; }
82  };
83 
84  template<template <typename...> class TEMPLATE, typename... Ts>
85  struct TypeID<TEMPLATE<Ts...>> {
86  static std::string GetName() {
87  return TemplateID<TEMPLATE<Ts...>>::GetName()
88  + '<' + TypeID<emp::TypePack<Ts...>>::GetTypes() + '>';
89  }
90  };
91 }
92 
93 
94 namespace emp{
95 
96  // Standard library types.
97  template<> struct TypeID<std::string> { static std::string GetName() { return "std::string"; } };
98 
99  // Standard library templates.
100  // template <typename... Ts> struct TemplateID<std::array<Ts...>> { static std::string GetName() { return "array"; } };
101 
102  template<typename T, typename... Ts> struct TypeID< emp::vector<T,Ts...> > {
103  static std::string GetName() {
104  using simple_vt = emp::vector<T>;
105  using full_vt = emp::vector<T,Ts...>;
106  if (std::is_same<simple_vt,full_vt>::value) {
107  return "emp::vector<" + TypeID<T>::GetName() + ">";
108  }
109  return "emp::vector<" + TypeID<TypePack<T,Ts...>>::GetTypes() + ">";
110  }
111  };
112 
113 }
114 
115 #endif
static std::string GetName()
Definition: TypeID.h:76
static std::string GetName()
Definition: TypeID.h:50
static std::string GetName()
Definition: TypeID.h:56
Definition: TypeID.h:25
static std::string GetName()
Definition: TypeID.h:97
static std::string GetName()
Definition: TypeID.h:67
static std::string GetName()
Definition: TypeID.h:46
static std::string GetName()
Definition: TypeID.h:47
Definition: TypeID.h:27
Definition: BitVector.h:785
static std::string GetName()
Definition: TypeID.h:35
static std::string GetName()
Definition: TypeID.h:86
static std::string GetName()
Definition: TypeID.h:38
static std::string GetName()
Definition: TypeID.h:37
static std::string GetName()
Definition: TypeID.h:81
decltype(&TEST::TypeID) TypeIDFilter
Definition: TypeID.h:26
A set of types that can be manipulated at compile time (good for metaprogramming) ...
Definition: TypeID.h:80
static std::string GetName()
Definition: TypeID.h:51
static std::string GetName()
Definition: TypeID.h:41
static std::string GetName()
Definition: TypeID.h:52
static std::string GetName()
Definition: TypeID.h:42
static std::string GetName()
Definition: TypeID.h:28
static std::string GetName()
Definition: TypeID.h:48
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
static std::string GetName()
Definition: TypeID.h:49
static std::string GetTypes()
Definition: TypeID.h:61
static std::string GetName()
Definition: TypeID.h:39
static std::string GetTypes()
Definition: TypeID.h:75
static std::string GetName()
Definition: TypeID.h:43
static std::string TypeID()
Definition: TypeID.h:27
static std::string GetName()
Definition: TypeID.h:103
static std::string GetName()
Definition: TypeID.h:45
Definition: TypePack.h:71