xref: /openbmc/phosphor-logging/extensions/openpower-pels/section.hpp (revision cbf3c4d4e9b8fc33cea36dd14448fa2406c11aca)
1 #pragma once
2 
3 #include "registry.hpp"
4 #include "section_header.hpp"
5 
6 #include <optional>
7 
8 namespace openpower
9 {
10 namespace pels
11 {
12 /**
13  * @class Section
14  *
15  * The base class for a PEL section.  It contains the SectionHeader
16  * as all sections start with it.
17  *
18  */
19 class Section
20 {
21   public:
22     Section() = default;
23     virtual ~Section() = default;
24     Section(const Section&) = default;
25     Section& operator=(const Section&) = default;
26     Section(Section&&) = default;
27     Section& operator=(Section&&) = default;
28 
29     /**
30      * @brief Returns a reference to the SectionHeader
31      */
32     const SectionHeader& header() const
33     {
34         return _header;
35     }
36 
37     /**
38      * @brief Says if the section is valid.
39      */
40     bool valid() const
41     {
42         return _valid;
43     }
44 
45     /**
46      * @brief Flatten the section into the stream
47      *
48      * @param[in] stream - The stream to write to
49      */
50     virtual void flatten(Stream& stream) const = 0;
51 
52     /**
53      * @brief Get section in JSON. Derived classes to override when required to.
54      * @return std::optional<std::string> - If a section comes with a JSON
55      * representation, this would return the string for it.
56      */
57     virtual std::optional<std::string> getJSON() const
58     {
59         return std::nullopt;
60     }
61 
62     /**
63      * @brief Get section in JSON. Derived classes to override when required to.
64      * @param[in] registry - Registry object reference
65      * @return std::optional<std::string> - If a section comes with a JSON
66      * representation, this would return the string for it.
67      */
68     virtual std::optional<std::string>
69         getJSON(message::Registry& registry) const
70     {
71         return std::nullopt;
72     }
73 
74     /**
75      * @brief Shrinks a PEL section
76      *
77      * If this is even possible for a section depends on which section
78      * it is.  If a section cannot be shrunk, it doesn't need to implement
79      * shrink so it will just return false, meaning no shrinking was done.
80      *
81      * If a section can be shrunk, this function will be overridden in that
82      * class.
83      *
84      * @param[in] newSize - The new size, in bytes, to shrink to
85      *
86      * @return bool - true if successful, false else
87      */
88     virtual bool shrink(size_t newSize)
89     {
90         return false;
91     }
92 
93   protected:
94     /**
95      * @brief Returns the flattened size of the section header
96      */
97     static constexpr size_t flattenedSize()
98     {
99         return SectionHeader::flattenedSize();
100     }
101 
102     /**
103      * @brief Used to validate the section.
104      *
105      * Implemented by derived classes.
106      */
107     virtual void validate() = 0;
108 
109     /**
110      * @brief The section header structure.
111      *
112      * Filled in by derived classes.
113      */
114     SectionHeader _header;
115 
116     /**
117      * @brief The section valid flag.
118      *
119      * This is determined by the derived class.
120      */
121     bool _valid = false;
122 };
123 } // namespace pels
124 } // namespace openpower
125