• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //* Copyright 2021 The Dawn Authors
2 //*
3 //* Licensed under the Apache License, Version 2.0 (the "License");
4 //* you may not use this file except in compliance with the License.
5 //* You may obtain a copy of the License at
6 //*
7 //*     http://www.apache.org/licenses/LICENSE-2.0
8 //*
9 //* Unless required by applicable law or agreed to in writing, software
10 //* distributed under the License is distributed on an "AS IS" BASIS,
11 //* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //* See the License for the specific language governing permissions and
13 //* limitations under the License.
14 
15 {% set API = metadata.api.upper() %}
16 {% set api = API.lower() %}
17 #ifndef {{API}}_CPP_PRINT_H_
18 #define {{API}}_CPP_PRINT_H_
19 
20 #include "dawn/{{api}}_cpp.h"
21 
22 #include <iomanip>
23 #include <ios>
24 #include <ostream>
25 #include <type_traits>
26 
27 namespace {{metadata.namespace}} {
28 
29   {% for type in by_category["enum"] %}
30       template <typename CharT, typename Traits>
31       std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
32           switch (value) {
33             {% for value in type.values %}
34               case {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
35                 o << "{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}";
36                 break;
37             {% endfor %}
38               default:
39                 o << "{{as_cppType(type.name)}}::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
40           }
41           return o;
42       }
43   {% endfor %}
44 
45   {% for type in by_category["bitmask"] %}
46       template <typename CharT, typename Traits>
47       std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
48         o << "{{as_cppType(type.name)}}::";
49         if (!static_cast<bool>(value)) {
50           {% for value in type.values if value.value == 0 %}
51             // 0 is often explicitly declared as None.
52             o << "{{as_cppEnum(value.name)}}";
53           {% else %}
54             o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << 0;
55           {% endfor %}
56           return o;
57         }
58 
59         bool moreThanOneBit = !HasZeroOrOneBits(value);
60         if (moreThanOneBit) {
61           o << "(";
62         }
63 
64         bool first = true;
65         {% for value in type.values if value.value != 0 %}
66           if (value & {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}) {
67             if (!first) {
68               o << "|";
69             }
70             first = false;
71             o << "{{as_cppEnum(value.name)}}";
72             value &= ~{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}};
73           }
74         {% endfor %}
75 
76         if (static_cast<bool>(value)) {
77           if (!first) {
78             o << "|";
79           }
80           o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
81         }
82 
83         if (moreThanOneBit) {
84           o << ")";
85         }
86         return o;
87       }
88   {% endfor %}
89 
90 }  // namespace {{metadata.namespace}}
91 
92 #endif // {{API}}_CPP_PRINT_H_
93