Skip to content

Commit f6aacfe

Browse files
committed
Refactoring: use std::variant for Indices
- Split of IndexIntf and IndexList into indexlist.h - Used a vector of std::variant to store the indices to avoid virtual dispatch (should allow for better compiler optimisation) - Used dispatch_call helper to implement efficient foreach construct on the index list (optimized version std::visit that uses switch on the variant's index() instead of a jump table). - replaced raw FTVNode pointer by std::shared_ptr. - improve encapsulation of EclipseHelp object by moving details to the implementation file - also used the dispatch_call helper for searchindex which had a hardcoded switch per method
1 parent fdd8e8e commit f6aacfe

35 files changed

+442
-314
lines changed

src/classdef.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstdio>
1919
#include <algorithm>
2020

21+
#include "types.h"
2122
#include "classdef.h"
2223
#include "classlist.h"
2324
#include "entry.h"

src/definition.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#include <iterator>
44
#include <unordered_map>
55
#include <string>
6-
76
#include <ctype.h>
8-
#include "md5.h"
97
#include <stdio.h>
108
#include <stdlib.h>
119
#include <assert.h>
10+
11+
#include "md5.h"
1212
#include "regex.h"
1313
#include "config.h"
1414
#include "definitionimpl.h"
@@ -34,6 +34,7 @@
3434
#include "bufstr.h"
3535
#include "reflist.h"
3636
#include "utf8.h"
37+
#include "indexlist.h"
3738

3839
//-----------------------------------------------------------------------------------------
3940

src/diagram.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "util.h"
2727
#include "doxygen.h"
2828
#include "portable.h"
29-
#include "index.h"
29+
#include "indexlist.h"
3030
#include "classlist.h"
3131
#include "textstream.h"
3232

src/dispatcher.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/******************************************************************************
2+
*
3+
* Copyright (C) 1997-2022 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 DISPATCHER_H
17+
#define DISPATCHER_H
18+
19+
#include <cstddef>
20+
#include <utility>
21+
#include <functional>
22+
#include <variant>
23+
#include <type_traits>
24+
25+
///////////////////////////////////////////////////////////////////////////////////////////////////////
26+
// idea based on https://mpark.github.io/programming/2019/01/22/variant-visitation-v2/
27+
28+
namespace detail
29+
{
30+
31+
//! Represents an unreachable piece of code
32+
#ifdef __GNUC__ // GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above)
33+
[[noreturn]] inline __attribute__((always_inline)) void unreachable() { __builtin_unreachable(); }
34+
#elif defined(_MSC_VER) // MSVC
35+
[[noreturn]] __forceinline void unreachable() { __assume(false); }
36+
#else // ???
37+
#warning "no implementation of unreachable for this compiler!"
38+
inline void unreachable() {}
39+
#endif
40+
41+
//! generic template declaration for the Dispatcher without implementation
42+
template<bool valid>
43+
struct Dispatcher;
44+
45+
//! specialization for the invalid case, signaling the compiler that the path is unreachable
46+
template<>
47+
struct Dispatcher<false>
48+
{
49+
//! case corresponding with the non-existing I'th type of variant V
50+
template<template<class> class W, std::size_t I, class V, class... As>
51+
static constexpr void case_(V &&, As &&...) { unreachable(); }
52+
};
53+
54+
//! specialization for the valid case, where `case_` invokes a specific method.
55+
template<>
56+
struct Dispatcher<true>
57+
{
58+
//! Invokes the method of a class `X` whose method pointer type is defined by `W<X>::method`
59+
//! where `X` matches the I'th type of variant `V` on the object held by `v`.
60+
template<template<class> class W, std::size_t I, class V, class... As>
61+
static constexpr void case_(V &&v, As &&... args) {
62+
using Alt = std::variant_alternative_t<I,std::decay_t<V>>;
63+
std::invoke( W<Alt>::method, std::get<I>( std::forward<V>(v) ), std::forward<As>(args)... );
64+
}
65+
};
66+
67+
} // namespace detail
68+
69+
//! Invokes the method of a class `X` whose member pointer type is defined by `W<X>::method`
70+
//! where `X` matches the type of the object held by `v` that is stored in a variant `V`.
71+
//! \tparam W a template class where `W<X>::method` points to a method of class `X` one of the types of `V`.
72+
//! \tparam V the type of the variant
73+
//! \tparam As the parameter types used to invoke the method
74+
//! \param v a object of the variant type for which to invoke the method
75+
//! \param args the parameters to pass to the method
76+
//! \note This implementation assumes a maximum of 10 types in the variant (easy to extend though by adding more cases).
77+
template<template<class> class W,class V, class...As>
78+
static constexpr void dispatch_call(V &&v,As &&... args)
79+
{
80+
// size holds the number of type of variant `V`
81+
constexpr std::size_t size = std::variant_size_v<std::decay_t<V>>;
82+
// which on a worst case maximum number of types in `V`.
83+
// fills in an unreachable branch for indices equal or higher than size.
84+
switch (v.index())
85+
{
86+
#define DISPATCH_AT(n) case n: detail::Dispatcher< ((n)<size) >::template case_<W,n>(v,std::forward<As>(args)...); break;
87+
DISPATCH_AT(0)
88+
DISPATCH_AT(1)
89+
DISPATCH_AT(2)
90+
DISPATCH_AT(3)
91+
DISPATCH_AT(4)
92+
DISPATCH_AT(5)
93+
DISPATCH_AT(6)
94+
DISPATCH_AT(7)
95+
DISPATCH_AT(8)
96+
DISPATCH_AT(9)
97+
#undef DISPATCH_AT
98+
}
99+
}
100+
101+
#endif // DISPATCHER_H

src/docparser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "portable.h"
3737
#include "printdocvisitor.h"
3838
#include "util.h"
39+
#include "indexlist.h"
3940

4041
// debug off
4142
#define DBG(x) do {} while(0)

src/docsets.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ struct DocSets::Private
4141
};
4242

4343

44-
DocSets::DocSets() : p(std::make_unique<Private>())
45-
{
46-
}
47-
48-
DocSets::~DocSets()
49-
{
50-
}
44+
DocSets::DocSets() : p(std::make_unique<Private>()) {}
45+
DocSets::~DocSets() = default;
46+
DocSets::DocSets(DocSets &&) = default;
5147

5248
void DocSets::initialize()
5349
{

src/docsets.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@
1818

1919
#include <memory>
2020

21-
#include "index.h"
21+
#include "qcstring.h"
2222

2323
class TextStream;
2424

2525
class Definition;
26+
class MemberDef;
2627

2728
/** A class that generates docset files.
2829
*
2930
* These files can be used to create context help
3031
* for use within Apple's Xcode 3.0 development environment
3132
*/
32-
class DocSets : public IndexIntf
33+
class DocSets
3334
{
3435

3536
public:
3637
DocSets();
37-
~DocSets();
38+
virtual ~DocSets();
39+
DocSets(DocSets &&);
40+
3841
void initialize();
3942
void finalize();
4043
void incContentsDepth();

src/dot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "message.h"
2929
#include "doxygen.h"
3030
#include "language.h"
31-
#include "index.h"
31+
#include "indexlist.h"
3232
#include "dir.h"
3333

3434
#define MAP_CMD "cmapx"

src/dotgraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "config.h"
2020
#include "doxygen.h"
21-
#include "index.h"
21+
#include "indexlist.h"
2222
#include "md5.h"
2323
#include "message.h"
2424
#include "util.h"

src/doxygen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "scanner.h"
3232
#include "entry.h"
3333
#include "index.h"
34+
#include "indexlist.h"
3435
#include "message.h"
3536
#include "config.h"
3637
#include "util.h"

0 commit comments

Comments
 (0)