The Sparta Modeling Framework
Loading...
Searching...
No Matches
DetectMemberUtils.hpp
Go to the documentation of this file.
1// <DetectMemberUtils.hpp> -*- C++ -*-
2
3
10#pragma once
11
12#include <iostream>
13
14namespace sparta{
15namespace utils{
16
18// an overloaded operator for a type during compile time.
19// This is needed because in order to annotate the Histograms,
20// we need to label each enum constant with their string names.
21// The process of converting enum constants into string is
22// usually done by an overload global operator <<. But we cannot
23// blindly assume that every enum class will have this overloaded.
24// If we assume so, this will lead to compilation failure.
25// This can be detected using SFINAE using Member Detector Idiom.
26// Reference : https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector
27namespace has_ostream_operator_impl{
28
30 typedef char no;
31
33 typedef char yes[2];
34
36 // from any object of any type.
37 struct fallback{
38 template<typename T>
39 fallback(const T&);
40 };
41
43 // any fallback object.
44 no operator << (std::ostream const&, fallback const&);
45
47 // then the return type of invoking it has to be a
48 // std::ostream &. In that case, this function will
49 // be invoked.
50 yes& test(std::ostream&);
51
53 // to begin with, it will use the global << operator
54 // defined in this namespace. It will convert itself into
55 // fallback object and then invoke this function. In this
56 // case, the return type in no.
58
59 template<typename T>
61 static std::ostream& s;
62 static T const& t;
63
65 // << operator on an object of type T. The return
66 // value that we get will tell us if this class T
67 // did have an << operator defined for it or not.
68 static constexpr bool value {sizeof(test(s << t)) == sizeof(yes)};
69 };
70}
71
72template<typename T>
74} // namespace utils
75} // namespace sparta
char yes[2]
Typedef a char array of size two.
no operator<<(std::ostream const &, fallback const &)
Declare a dummy << operator which operates on.
char no
Typedef a char array of size one.
yes & test(std::ostream &)
If the class does have an << operator overloaded,.
Macros for handling exponential backoff.
A fallback struct which can create itself.
static constexpr bool value
Test to see what happens when we invoke.