gem5  v22.1.0.0
serialize_handlers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 2018 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2002-2005 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /* @file
42  * Serialization Interface Declarations
43  */
44 
45 #ifndef __SERIALIZE_HANDLERS_HH__
46 #define __SERIALIZE_HANDLERS_HH__
47 
48 
49 #include <iostream>
50 #include <type_traits>
51 #include <utility>
52 
53 #include "base/str.hh"
54 
55 namespace gem5
56 {
57 
63 // To add support for a new type of field that can be serialized, define
64 // template specializations of the two classes below, ParseParam and ShowParam,
65 // as described above each. The way ParseParam is specialized for std::string
66 // or ShowParam is specialied for bool can be used as examples.
67 
68 /*
69  * A structure which should be specialized to contain a static method with the
70  * signature:
71  *
72  * bool parse(const std::string &s, T &value)
73  *
74  * which fills in value using the contents of s, and returns if that was
75  * successful.
76  */
77 template <class T, class Enable=void>
78 struct ParseParam;
79 
80 // Specialization for anything to_number can accept.
81 template <class T>
82 struct ParseParam<T, decltype(to_number("", std::declval<T&>()), void())>
83 {
84  static bool
85  parse(const std::string &s, T &value)
86  {
87  return to_number(s, value);
88  }
89 };
90 
91 template <>
92 struct ParseParam<bool>
93 {
94  static bool
95  parse(const std::string &s, bool &value)
96  {
97  return to_bool(s, value);
98  }
99 };
100 
101 template <>
102 struct ParseParam<std::string>
103 {
104  static bool
105  parse(const std::string &s, std::string &value)
106  {
107  // String requires no processing to speak of
108  value = s;
109  return true;
110  }
111 };
112 
113 /*
114  * A structure which should be specialized to contain a static method with the
115  * signature:
116  *
117  * void show(std::ostream &os, const T &value)
118  *
119  * which outputs value to the stream os.
120  *
121  * This default implementation falls back to the << operator which should work
122  * for many types.
123  */
124 template <class T, class Enabled=void>
125 struct ShowParam
126 {
127  static void show(std::ostream &os, const T &value) { os << value; }
128 };
129 
130 // Handle characters specially so that we print their value, not the character
131 // they encode.
132 template <class T>
133 struct ShowParam<T, std::enable_if_t<std::is_same_v<char, T> ||
134  std::is_same_v<unsigned char, T> ||
135  std::is_same_v<signed char, T>>>
136 {
137  static void
138  show(std::ostream &os, const T &value)
139  {
140  if (std::is_signed_v<T>)
141  os << (int)value;
142  else
143  os << (unsigned int)value;
144  }
145 };
146 
147 template <>
148 struct ShowParam<bool>
149 {
150  static void
151  show(std::ostream &os, const bool &value)
152  {
153  // Display bools as strings
154  os << (value ? "true" : "false");
155  }
156 };
157 
160 } // namespace gem5
161 
162 #endif // __SERIALIZE_HANDLERS_HH__
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 17 > os
Definition: misc.hh:810
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:217
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:191
Overload hash function for BasicBlockRange type.
Definition: misc.hh:2826
static bool parse(const std::string &s, bool &value)
static bool parse(const std::string &s, std::string &value)
static void show(std::ostream &os, const bool &value)
static void show(std::ostream &os, const T &value)

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1