P4C
The P4 Compiler
Loading...
Searching...
No Matches
stringify.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/* -*-c++-*- */
9
10#ifndef LIB_STRINGIFY_H_
11#define LIB_STRINGIFY_H_
12
13#include <cstdarg>
14#include <string>
15#include <string_view>
16#include <type_traits>
17
18// FIXME: Replace with big_int_fwd.h with Boost 1.84+
19#include "big_int.h"
20#include "cstring.h"
21
22namespace P4 {
23
25 public:
26 virtual void dbprint(std::ostream &out) const = 0;
27 void print() const; // useful in the debugger
28 virtual ~IHasDbPrint() = default;
29};
30
31inline std::ostream &operator<<(std::ostream &out, const IHasDbPrint &obj) {
32 obj.dbprint(out);
33 return out;
34}
35
36inline std::ostream &operator<<(std::ostream &out, const IHasDbPrint *obj) {
37 if (obj)
38 obj->dbprint(out);
39 else
40 out << "<null>";
41 return out;
42}
43
46template <class, class = void>
47struct has_dbprint : std::false_type {};
48
49template <class T>
50struct has_dbprint<T,
51 std::void_t<decltype(std::declval<T>().dbprint(std::declval<std::ostream &>()))>>
52 : std::true_type {};
53
54template <class T>
55inline constexpr bool has_dbprint_v = has_dbprint<T>::value;
56
57template <class, class = void>
58struct has_ostream_operator : std::false_type {};
59
60template <class T>
62 T, std::void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>>
63 : std::true_type {};
64
65template <class T>
66inline constexpr bool has_ostream_operator_v = has_ostream_operator<T>::value;
67
68// convert values to cstrings
69namespace Util {
70
72template <class, class = void>
73struct has_toString : std::false_type {};
74
75template <class T>
76struct has_toString<T, std::void_t<decltype(std::declval<T>().toString())>> : std::true_type {};
77
78template <class T>
79inline constexpr bool has_toString_v = has_toString<T>::value;
80
81template <typename T, typename = decltype(std::to_string(std::declval<T>()))>
82cstring toString(T value) {
83 return cstring(std::to_string(value));
84}
85
86template <typename T>
87auto toString(const T &value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
88 return value.toString();
89}
90
91template <typename T>
92auto toString(T &value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
93 return value.toString();
94}
95
96template <typename T>
97auto toString(const T *value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
98 return value->toString();
99}
100
101template <typename T>
102auto toString(T *value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
103 return value->toString();
104}
105
106cstring toString(bool value);
107cstring toString(const std::string &value);
108cstring toString(const char *value);
109cstring toString(cstring value);
110cstring toString(std::string_view value);
112cstring toString(const big_int &value, unsigned width, bool sign, unsigned int base = 10);
113cstring toString(const void *value);
114
115char DigitToChar(int digit);
116
117} // namespace Util
118} // namespace P4
119
120#endif /* LIB_STRINGIFY_H_ */
Definition stringify.h:24
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
STL namespace.
Definition stringify.h:47
Definition stringify.h:58