P4C
The P4 Compiler
Loading...
Searching...
No Matches
stringify.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/* -*-c++-*- */
18
19#ifndef LIB_STRINGIFY_H_
20#define LIB_STRINGIFY_H_
21
22#include <cstdarg>
23#include <string>
24#include <string_view>
25#include <type_traits>
26
27// FIXME: Replace with big_int_fwd.h with Boost 1.84+
28#include "big_int.h"
29#include "cstring.h"
30
31namespace P4 {
32
34 public:
35 virtual void dbprint(std::ostream &out) const = 0;
36 void print() const; // useful in the debugger
37 virtual ~IHasDbPrint() = default;
38};
39
40inline std::ostream &operator<<(std::ostream &out, const IHasDbPrint &obj) {
41 obj.dbprint(out);
42 return out;
43}
44
45inline std::ostream &operator<<(std::ostream &out, const IHasDbPrint *obj) {
46 if (obj)
47 obj->dbprint(out);
48 else
49 out << "<null>";
50 return out;
51}
52
55template <class, class = void>
56struct has_dbprint : std::false_type {};
57
58template <class T>
59struct has_dbprint<T,
60 std::void_t<decltype(std::declval<T>().dbprint(std::declval<std::ostream &>()))>>
61 : std::true_type {};
62
63template <class T>
64inline constexpr bool has_dbprint_v = has_dbprint<T>::value;
65
66template <class, class = void>
67struct has_ostream_operator : std::false_type {};
68
69template <class T>
71 T, std::void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>>
72 : std::true_type {};
73
74template <class T>
75inline constexpr bool has_ostream_operator_v = has_ostream_operator<T>::value;
76
77// convert values to cstrings
78namespace Util {
79
81template <class, class = void>
82struct has_toString : std::false_type {};
83
84template <class T>
85struct has_toString<T, std::void_t<decltype(std::declval<T>().toString())>> : std::true_type {};
86
87template <class T>
88inline constexpr bool has_toString_v = has_toString<T>::value;
89
90template <typename T, typename = decltype(std::to_string(std::declval<T>()))>
91cstring toString(T value) {
92 return std::to_string(value);
93}
94
95template <typename T>
96auto toString(const T &value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
97 return value.toString();
98}
99
100template <typename T>
101auto toString(T &value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
102 return value.toString();
103}
104
105template <typename T>
106auto toString(const T *value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
107 return value->toString();
108}
109
110template <typename T>
111auto toString(T *value) -> typename std::enable_if_t<has_toString_v<T>, cstring> {
112 return value->toString();
113}
114
115cstring toString(bool value);
116cstring toString(const std::string &value);
117cstring toString(const char *value);
118cstring toString(cstring value);
119cstring toString(std::string_view value);
121cstring toString(const big_int &value, unsigned width, bool sign, unsigned int base = 10);
122cstring toString(const void *value);
123
124char DigitToChar(int digit);
125
126} // namespace Util
127} // namespace P4
128
129#endif /* LIB_STRINGIFY_H_ */
Definition stringify.h:33
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
STL namespace.
Definition stringify.h:56
Definition stringify.h:67