gem5  v21.1.0.2
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<char, T>::value ||
134  std::is_same<unsigned char, T>::value ||
135  std::is_same<signed char, T>::value>>
136 {
137  static void
138  show(std::ostream &os, const T &value)
139  {
140  if (std::is_signed<T>::value)
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__
gem5::ShowParam< T, std::enable_if_t< std::is_same< char, T >::value||std::is_same< unsigned char, T >::value||std::is_same< signed char, T >::value > >::show
static void show(std::ostream &os, const T &value)
Definition: serialize_handlers.hh:138
gem5::ParseParam< std::string >::parse
static bool parse(const std::string &s, std::string &value)
Definition: serialize_handlers.hh:105
gem5::ShowParam::show
static void show(std::ostream &os, const T &value)
Definition: serialize_handlers.hh:127
gem5::to_number
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:217
gem5::ShowParam
Definition: serialize_handlers.hh:125
str.hh
gem5::ParseParam< bool >::parse
static bool parse(const std::string &s, bool &value)
Definition: serialize_handlers.hh:95
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
gem5::to_bool
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:204
gem5::ParseParam< T, decltype(to_number("", std::declval< T & >()), void())>::parse
static bool parse(const std::string &s, T &value)
Definition: serialize_handlers.hh:85
std
Overload hash function for BasicBlockRange type.
Definition: types.hh:111
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:809
gem5::ShowParam< bool >::show
static void show(std::ostream &os, const bool &value)
Definition: serialize_handlers.hh:151
gem5::ParseParam
Definition: serialize_handlers.hh:78
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40

Generated on Tue Sep 21 2021 12:25:48 for gem5 by doxygen 1.8.17