Imported Upstream version 1.9.8
[platform/upstream/doxygen.git] / src / section.h
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2020 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15
16 #ifndef SECTION_H
17 #define SECTION_H
18
19 #include <string>
20 #include <unordered_map>
21
22 #include "qcstring.h"
23 #include "linkedmap.h"
24
25 class Definition;
26
27 //! enum representing the various types of sections and entities that can be referred to.
28 enum class SectionType
29 {
30   Page          = 0,
31   Section       = 1,
32   Subsection    = 2,
33   Subsubsection = 3,
34   Paragraph     = 4,
35   Anchor        = 5,
36   Table         = 6
37 };
38
39 //! return true if type is a section, and false if it is a page, anchor or table.
40 inline constexpr bool isSection(SectionType type)
41 {
42   return (type==SectionType::Section       ||
43           type==SectionType::Subsection    ||
44           type==SectionType::Subsubsection ||
45           type==SectionType::Paragraph);
46 }
47
48 //! class that provide information about a section.
49 class SectionInfo
50 {
51   public:
52     SectionInfo(const QCString &label, const QCString &fileName, int lineNr,
53                 const QCString &title, SectionType type, int level,const QCString &ref) :
54         m_label(label), m_title(title), m_type(type), m_ref(ref),
55         m_lineNr(lineNr), m_fileName(fileName), m_level(level)
56     {
57       //printf("SectionInfo(%p) fileName=%s\n",(void*)this,qPrint(fileName));
58     }
59     ~SectionInfo()
60     {
61       //printf("~SectionInfo(%p)\n",(void*)this);
62     }
63
64     // getters
65     QCString    label()      const { return m_label;      }
66     QCString    title()      const { return m_title;      }
67     SectionType type()       const { return m_type;       }
68     QCString    ref()        const { return m_ref;        }
69     int         lineNr()     const { return m_lineNr;     }
70     QCString    fileName()   const { return m_fileName;   }
71     bool        generated()  const { return m_generated;  }
72     int         level()      const { return m_level;      }
73     Definition *definition() const { return m_definition; }
74
75     // setters
76     void setFileName(const QCString &fn) { m_fileName   = fn; }
77     void setType(SectionType t)          { m_type       = t;  }
78     void setGenerated(bool b)            { m_generated  = b;  }
79     void setDefinition(Definition *d)    { m_definition = d;  }
80     void setTitle(const QCString &t)     { m_title      = t;  }
81     void setLevel(int l)                 { m_level      = l;  }
82     void setReference(const QCString &r) { m_ref        = r;  }
83     void setLineNr(int l)                { m_lineNr     = l;  }
84
85   private:
86     QCString    m_label;
87     QCString    m_title;
88     SectionType m_type;
89     QCString    m_ref;
90     int         m_lineNr;
91     QCString    m_fileName;
92     bool        m_generated = false;
93     int         m_level;
94     Definition *m_definition = 0;
95 };
96
97 //! class that represents a list of constant references to sections.
98 class SectionRefs
99 {
100     using SectionInfoVec = std::vector<const SectionInfo*>;
101   public:
102     using const_iterator = SectionInfoVec::const_iterator;
103
104     //! Returns a constant pointer to the section info given a section label or nullptr
105     //! if no section with the given label can be found.
106     const SectionInfo *find(const QCString &label) const
107     {
108       auto it = m_lookup.find(label.str());
109       return it!=m_lookup.end() ? it->second : nullptr;
110     }
111
112     //! Adds a non-owning section reference.
113     void add(const SectionInfo *si)
114     {
115       m_lookup.insert({toStdString(si->label()),si});
116       m_entries.push_back(si);
117     }
118
119     const_iterator begin() const { return m_entries.cbegin(); }
120     const_iterator end()   const { return m_entries.cend(); }
121     bool empty() const { return m_entries.empty(); }
122     size_t size() const { return m_entries.size(); }
123
124   private:
125     SectionInfoVec m_entries;
126     std::unordered_map< std::string, const SectionInfo* > m_lookup;
127 };
128
129 //! singleton class that owns the list of all sections
130 class SectionManager : public LinkedMap<SectionInfo>
131 {
132   public:
133     //! Add a new section given the data of an existing section.
134     //! Returns a non-owning pointer to the newly added section.
135     SectionInfo *add(const SectionInfo &si)
136     {
137       return LinkedMap<SectionInfo>::add(si.label(),si.fileName(),
138                       si.lineNr(),si.title(),si.type(),si.level(),si.ref());
139     }
140
141     //! Add a new section
142     //! Return a non-owning pointer to the newly added section
143     SectionInfo *add(const QCString &label, const QCString &fileName, int lineNr,
144                      const QCString &title, SectionType type, int level,const QCString &ref=QCString())
145     {
146       return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
147     }
148
149     //! Replace an existing section with a new one
150     //! Return a non-owning pointer to the newly added section
151     SectionInfo *replace(const QCString &label, const QCString &fileName, int lineNr,
152                          const QCString &title, SectionType type, int level,const QCString &ref=QCString())
153     {
154       SectionInfo *si = LinkedMap<SectionInfo>::find(label.data());
155       if (si)
156       {
157         si->setFileName(fileName);
158         si->setLineNr(lineNr);
159         si->setTitle(title);
160         si->setType(type);
161         si->setLevel(level);
162         si->setReference(ref);
163         return si;
164       }
165       else
166       {
167         return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
168       }
169     }
170
171     //! returns a reference to the singleton
172     static SectionManager &instance()
173     {
174       static SectionManager sm;
175       return sm;
176     }
177
178   private:
179     SectionManager() {}
180     SectionManager(const SectionManager &other) = delete;
181     SectionManager &operator=(const SectionManager &other) = delete;
182 };
183
184
185 #endif