1 /******************************************************************************
3 * Copyright (C) 1997-2020 by Dimitri van Heesch.
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.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
20 #include <unordered_map>
23 #include "linkedmap.h"
27 //! enum representing the various types of sections and entities that can be referred to.
28 enum class SectionType
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)
42 return (type==SectionType::Section ||
43 type==SectionType::Subsection ||
44 type==SectionType::Subsubsection ||
45 type==SectionType::Paragraph);
48 //! class that provide information about a section.
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)
57 //printf("SectionInfo(%p) fileName=%s\n",(void*)this,qPrint(fileName));
61 //printf("~SectionInfo(%p)\n",(void*)this);
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; }
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; }
92 bool m_generated = false;
94 Definition *m_definition = 0;
97 //! class that represents a list of constant references to sections.
100 using SectionInfoVec = std::vector<const SectionInfo*>;
102 using const_iterator = SectionInfoVec::const_iterator;
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
108 auto it = m_lookup.find(label.str());
109 return it!=m_lookup.end() ? it->second : nullptr;
112 //! Adds a non-owning section reference.
113 void add(const SectionInfo *si)
115 m_lookup.insert({toStdString(si->label()),si});
116 m_entries.push_back(si);
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(); }
125 SectionInfoVec m_entries;
126 std::unordered_map< std::string, const SectionInfo* > m_lookup;
129 //! singleton class that owns the list of all sections
130 class SectionManager : public LinkedMap<SectionInfo>
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)
137 return LinkedMap<SectionInfo>::add(si.label(),si.fileName(),
138 si.lineNr(),si.title(),si.type(),si.level(),si.ref());
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())
146 return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
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())
154 SectionInfo *si = LinkedMap<SectionInfo>::find(label.data());
157 si->setFileName(fileName);
158 si->setLineNr(lineNr);
162 si->setReference(ref);
167 return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
171 //! returns a reference to the singleton
172 static SectionManager &instance()
174 static SectionManager sm;
180 SectionManager(const SectionManager &other) = delete;
181 SectionManager &operator=(const SectionManager &other) = delete;