gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
60 // To add support for a new type of field that can be serialized, define
61 // template specializations of the two classes below, ParseParam and ShowParam,
62 // as described above each. The way ParseParam is specialized for std::string
63 // or ShowParam is specialied for bool can be used as examples.
64 
65 /*
66  * A structure which should be specialized to contain a static method with the
67  * signature:
68  *
69  * bool parse(const std::string &s, T &value)
70  *
71  * which fills in value using the contents of s, and returns if that was
72  * successful.
73  */
74 template <class T, class Enable=void>
75 struct ParseParam;
76 
77 // Specialization for anything to_number can accept.
78 template <class T>
79 struct ParseParam<T, decltype(to_number("", std::declval<T&>()), void())>
80 {
81  static bool
82  parse(const std::string &s, T &value)
83  {
84  return to_number(s, value);
85  }
86 };
87 
88 template <>
89 struct ParseParam<bool>
90 {
91  static bool
92  parse(const std::string &s, bool &value)
93  {
94  return to_bool(s, value);
95  }
96 };
97 
98 template <>
99 struct ParseParam<std::string>
100 {
101  static bool
102  parse(const std::string &s, std::string &value)
103  {
104  // String requires no processing to speak of
105  value = s;
106  return true;
107  }
108 };
109 
110 /*
111  * A structure which should be specialized to contain a static method with the
112  * signature:
113  *
114  * void show(std::ostream &os, const T &value)
115  *
116  * which outputs value to the stream os.
117  *
118  * This default implementation falls back to the << operator which should work
119  * for many types.
120  */
121 template <class T, class Enabled=void>
122 struct ShowParam
123 {
124  static void show(std::ostream &os, const T &value) { os << value; }
125 };
126 
127 // Handle characters specially so that we print their value, not the character
128 // they encode.
129 template <class T>
130 struct ShowParam<T, std::enable_if_t<std::is_same<char, T>::value ||
131  std::is_same<unsigned char, T>::value ||
132  std::is_same<signed char, T>::value>>
133 {
134  static void
135  show(std::ostream &os, const T &value)
136  {
137  if (std::is_signed<T>::value)
138  os << (int)value;
139  else
140  os << (unsigned int)value;
141  }
142 };
143 
144 template <>
145 struct ShowParam<bool>
146 {
147  static void
148  show(std::ostream &os, const bool &value)
149  {
150  // Display bools as strings
151  os << (value ? "true" : "false");
152  }
153 };
154 
157 #endif // __SERIALIZE_HANDLERS_HH__
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:201
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
ParseParam< T, decltype(to_number("", std::declval< T & >()), void())>::parse
static bool parse(const std::string &s, T &value)
Definition: serialize_handlers.hh:82
ShowParam
Definition: serialize_handlers.hh:122
str.hh
to_number
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
Definition: vec_pred_reg.hh:379
ShowParam< bool >::show
static void show(std::ostream &os, const bool &value)
Definition: serialize_handlers.hh:148
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:135
ParseParam
Definition: serialize_handlers.hh:75
ParseParam< std::string >::parse
static bool parse(const std::string &s, std::string &value)
Definition: serialize_handlers.hh:102
ParseParam< bool >::parse
static bool parse(const std::string &s, bool &value)
Definition: serialize_handlers.hh:92
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
ShowParam::show
static void show(std::ostream &os, const T &value)
Definition: serialize_handlers.hh:124

Generated on Tue Mar 23 2021 19:41:28 for gem5 by doxygen 1.8.17