xref: /openbmc/phosphor-logging/extensions/openpower-pels/section.hpp (revision ad0e0476f8febed0e597e63a5cf4982cedf116b8)
1 #pragma once
2 
3 #include "section_header.hpp"
4 
5 #include <optional>
6 
7 namespace openpower
8 {
9 namespace pels
10 {
11 /**
12  * @class Section
13  *
14  * The base class for a PEL section.  It contains the SectionHeader
15  * as all sections start with it.
16  *
17  */
18 class Section
19 {
20   public:
21     Section() = default;
22     virtual ~Section() = default;
23     Section(const Section&) = default;
24     Section& operator=(const Section&) = default;
25     Section(Section&&) = default;
26     Section& operator=(Section&&) = default;
27 
28     /**
29      * @brief Returns a reference to the SectionHeader
30      */
31     const SectionHeader& header() const
32     {
33         return _header;
34     }
35 
36     /**
37      * @brief Says if the section is valid.
38      */
39     bool valid() const
40     {
41         return _valid;
42     }
43 
44     /**
45      * @brief Flatten the section into the stream
46      *
47      * @param[in] stream - The stream to write to
48      */
49     virtual void flatten(Stream& stream) const = 0;
50 
51     /**
52      * @brief Get section in JSON. Derived classes to override when required to.
53      * @return std::optional<std::string> - If a section comes with a JSON
54      * repressentation, this would return the string for it.
55      */
56     virtual std::optional<std::string> getJSON() const
57     {
58         return std::nullopt;
59     }
60 
61   protected:
62     /**
63      * @brief Returns the flattened size of the section header
64      */
65     static constexpr size_t flattenedSize()
66     {
67         return SectionHeader::flattenedSize();
68     }
69 
70     /**
71      * @brief Used to validate the section.
72      *
73      * Implemented by derived classes.
74      */
75     virtual void validate() = 0;
76 
77     /**
78      * @brief The section header structure.
79      *
80      * Filled in by derived classes.
81      */
82     SectionHeader _header;
83 
84     /**
85      * @brief The section valid flag.
86      *
87      * This is determined by the derived class.
88      */
89     bool _valid = false;
90 };
91 } // namespace pels
92 } // namespace openpower
93