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 {
29  public:
30  static constexpr int Page = 0;
31  static constexpr int MinLevel = 1;
32  static constexpr int Section = 1;
33  static constexpr int Subsection = 2;
34  static constexpr int Subsubsection = 3;
35  static constexpr int Paragraph = 4;
36  static constexpr int Subparagraph = 5;
37  static constexpr int Subsubparagraph = 6;
38  static constexpr int MaxLevel = 6;
39  static constexpr int Anchor = 7;
40  static constexpr int Table = 8;
41 
42  constexpr SectionType() : m_level(0) {}
43  constexpr SectionType(int lvl) : m_level(lvl) {}
44  constexpr int level() const { return m_level; }
45  constexpr bool isSection() const
46  {
47  return m_level>=SectionType::MinLevel && m_level<=SectionType::MaxLevel;
48  }
49 
50  private:
51  int m_level;
52 };
53 
56 {
57  public:
58  SectionInfo(const QCString &label, const QCString &fileName, int lineNr,
59  const QCString &title, SectionType type, int level,const QCString &ref) :
60  m_label(label), m_title(title), m_type(type), m_ref(ref),
61  m_lineNr(lineNr), m_fileName(fileName), m_level(level)
62  {
63  //printf("SectionInfo(%p) fileName=%s\n",(void*)this,qPrint(fileName));
64  }
65  ~SectionInfo()
66  {
67  //printf("~SectionInfo(%p)\n",(void*)this);
68  }
69 
70  // getters
71  QCString label() const { return m_label; }
72  QCString title() const { return m_title; }
73  SectionType type() const { return m_type; }
74  QCString ref() const { return m_ref; }
75  int lineNr() const { return m_lineNr; }
76  QCString fileName() const { return m_fileName; }
77  bool generated() const { return m_generated; }
78  int level() const { return m_level; }
79  Definition *definition() const { return m_definition; }
80 
81  // setters
82  void setFileName(const QCString &fn) { m_fileName = fn; }
83  void setType(SectionType t) { m_type = t; }
84  void setGenerated(bool b) { m_generated = b; }
85  void setDefinition(Definition *d) { m_definition = d; }
86  void setTitle(const QCString &t) { m_title = t; }
87  void setLevel(int l) { m_level = l; }
88  void setReference(const QCString &r) { m_ref = r; }
89  void setLineNr(int l) { m_lineNr = l; }
90 
91  private:
92  QCString m_label;
93  QCString m_title;
94  SectionType m_type;
95  QCString m_ref;
96  int m_lineNr;
97  QCString m_fileName;
98  bool m_generated = false;
99  int m_level;
100  Definition *m_definition = nullptr;
101 };
102 
105 {
106  using SectionInfoVec = std::vector<const SectionInfo*>;
107  public:
108  using const_iterator = SectionInfoVec::const_iterator;
109 
112  const SectionInfo *find(const QCString &label) const
113  {
114  auto it = m_lookup.find(label.str());
115  return it!=m_lookup.end() ? it->second : nullptr;
116  }
117 
119  void add(const SectionInfo *si)
120  {
121  m_lookup.insert({toStdString(si->label()),si});
122  m_entries.push_back(si);
123  }
124 
125  const_iterator begin() const { return m_entries.cbegin(); }
126  const_iterator end() const { return m_entries.cend(); }
127  bool empty() const { return m_entries.empty(); }
128  size_t size() const { return m_entries.size(); }
129 
130  private:
131  SectionInfoVec m_entries;
132  std::unordered_map< std::string, const SectionInfo* > m_lookup;
133 };
134 
136 class SectionManager : public LinkedMap<SectionInfo>
137 {
138  public:
142  {
143  return LinkedMap<SectionInfo>::add(si.label(),si.fileName(),
144  si.lineNr(),si.title(),si.type(),si.level(),si.ref());
145  }
146 
149  SectionInfo *add(const QCString &label, const QCString &fileName, int lineNr,
150  const QCString &title, SectionType type, int level,const QCString &ref=QCString())
151  {
152  return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
153  }
154 
157  SectionInfo *replace(const QCString &label, const QCString &fileName, int lineNr,
158  const QCString &title, SectionType type, int level,const QCString &ref=QCString())
159  {
161  if (si)
162  {
163  si->setFileName(fileName);
164  si->setLineNr(lineNr);
165  si->setTitle(title);
166  si->setType(type);
167  si->setLevel(level);
168  si->setReference(ref);
169  return si;
170  }
171  else
172  {
173  return LinkedMap<SectionInfo>::add(label.data(),fileName,lineNr,title,type,level,ref);
174  }
175  }
176 
179  {
180  static SectionManager sm;
181  return sm;
182  }
183 
184  private:
185  SectionManager() {}
186  SectionManager(const SectionManager &other) = delete;
187  SectionManager &operator=(const SectionManager &other) = delete;
188 };
189 
190 
191 #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:74
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:157
static SectionManager & instance()
returns a reference to the singleton
Definition: section.h:178
SectionInfo * add(const SectionInfo &si)
Add a new section given the data of an existing section.
Definition: section.h:141
class that represents a list of constant references to sections.
Definition: section.h:104
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:112
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:55
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:158
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:149
Definition: section.h:27
singleton class that owns the list of all sections
Definition: section.h:136
This is an alternative implementation of QCString.
Definition: qcstring.h:93
void add(const SectionInfo *si)
Adds a non-owning section reference.
Definition: section.h:119