P4C
The P4 Compiler
Loading...
Searching...
No Matches
hex.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#ifndef LIB_HEX_H_
18#define LIB_HEX_H_
19
20#include <cstdint>
21#include <iomanip>
22#include <iostream>
23#include <vector>
24
25namespace P4 {
26
27class hex {
28 std::intmax_t val;
29 int width;
30 char fill;
31
32 public:
33 explicit hex(intmax_t v, int w = 0, char f = ' ') : val(v), width(w), fill(f) {}
34 explicit hex(void *v, int w = 0, char f = ' ') : val((intmax_t)v), width(w), fill(f) {}
35 friend std::ostream &operator<<(std::ostream &os, const hex &h);
36};
37
38inline std::ostream &operator<<(std::ostream &os, const hex &h) {
39 auto save = os.flags();
40 auto save_fill = os.fill();
41 os << std::hex << std::setw(h.width) << std::setfill(h.fill) << h.val;
42 os.fill(save_fill);
43 os.flags(save);
44 return os;
45}
46
47class hexvec {
48 void *data;
49 size_t elsize, len;
50 int width;
51 char fill;
52
53 public:
54 template <typename I>
55 hexvec(I *d, size_t l, int w = 0, char f = ' ')
56 : data(d), elsize(sizeof(I)), len(l), width(w), fill(f) {}
57 template <typename T>
58 explicit hexvec(std::vector<T> &d, int w = 0, char f = ' ')
59 : data(d.data()), elsize(sizeof(T)), len(d.size()), width(w), fill(f) {}
60 friend std::ostream &operator<<(std::ostream &os, const hexvec &h);
61};
62
63std::ostream &operator<<(std::ostream &os, const hexvec &h);
64
65} // namespace P4
66
67#endif /* LIB_HEX_H_ */
Definition hex.h:27
Definition hex.h:47
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24