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