doxygen
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 
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 
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 
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)\n",this);
58  }
59  ~SectionInfo()
60  {
61  //printf("~SectionInfo(%p)\n",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 
99 {
100  using SectionInfoVec = std::vector<const SectionInfo*>;
101  public:
102  using const_iterator = SectionInfoVec::const_iterator;
103 
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 
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 
130 class SectionManager : public LinkedMap<SectionInfo>
131 {
132  public:
136  {
137  return LinkedMap<SectionInfo>::add(si.label(),si.fileName(),
138  si.lineNr(),si.title(),si.type(),si.level(),si.ref());
139  }
140 
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 
151  SectionInfo *replace(const QCString &label, const QCString &fileName, int lineNr,
152  const QCString &title, SectionType type, int level,const QCString &ref=QCString())
153  {
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 
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
const T * find(const std::string &key) const
Find an object given the key.
Definition: linkedmap.h:47
The common base class of all entity definitions found in the sources.
Definition: definition.h:76
SectionInfo * replace(const QCString &label, const QCString &fileName, int lineNr, const QCString &title, SectionType type, int level, const QCString &ref=QCString())
Replace an existing section with a new one Return a non-owning pointer to the newly added section...
Definition: section.h:151
static SectionManager & instance()
returns a reference to the singleton
Definition: section.h:172
SectionInfo * add(const SectionInfo &si)
Add a new section given the data of an existing section.
Definition: section.h:135
class that represents a list of constant references to sections.
Definition: section.h:98
const SectionInfo * find(const QCString &label) const
Returns a constant pointer to the section info given a section label or nullptr if no section with th...
Definition: section.h:106
T * add(const char *k, Args &&... args)
Adds a new object to the ordered vector if it was not added already.
Definition: linkedmap.h:90
class that provide information about a section.
Definition: section.h:49
Container class representing a vector of objects with keys.
Definition: linkedmap.h:35
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition: qcstring.h:153
SectionInfo * add(const QCString &label, const QCString &fileName, int lineNr, const QCString &title, SectionType type, int level, const QCString &ref=QCString())
Add a new section Return a non-owning pointer to the newly added section.
Definition: section.h:143
singleton class that owns the list of all sections
Definition: section.h:130
This is an alternative implementation of QCString.
Definition: qcstring.h:108
void add(const SectionInfo *si)
Adds a non-owning section reference.
Definition: section.h:113