Skip to content

Commit c6b4e54

Browse files
committed
Updated README and Examples
Fixed a bunch of issues with the original examples, and removed all of the performance stuff as the point of the pattern is not to argue about the performance costs of pure virtual, but instead to simply show how to do abstraction without the need for pure virtual.
1 parent 38c3c9a commit c6b4e54

30 files changed

+395
-806
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ include(ExternalProject)
2626
# ------------------------------------------------------------------------------
2727

2828
set(CMAKE_CXX_STANDARD 17)
29-
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--strip-all")
29+
# set(CMAKE_EXE_LINKER_FLAGS "-Wl,--strip-all")
3030

3131
# ------------------------------------------------------------------------------
3232
# source
3333
# ------------------------------------------------------------------------------
3434

3535
add_subdirectory(bad)
36-
add_subdirectory(dynamic_abstraction)
37-
add_subdirectory(static_abstraction)
36+
add_subdirectory(dynamic_interface)
37+
add_subdirectory(static_interface)

README.md

Lines changed: 170 additions & 391 deletions
Large diffs are not rendered by default.

bad/CMakeLists.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,5 @@
1616
# License along with this library; if not, write to the Free Software
1717
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818

19-
add_executable(bad a.cpp b.cpp main.cpp)
20-
add_executable(bad_ut b.cpp unittest.cpp)
21-
add_executable(bad_perf a.cpp b.cpp performance.cpp)
22-
23-
target_compile_options(bad PRIVATE -O3 -DNDEBUG)
24-
target_compile_options(bad_ut PRIVATE -g)
25-
target_compile_options(bad_perf PRIVATE -O3 -DNDEBUG)
19+
add_executable(bad main.cpp)
20+
target_compile_options(bad PRIVATE -O3)

bad/a.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

bad/a.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
#ifndef A_H
2323
#define A_H
2424

25+
static volatile int g_count;
26+
#define NOINLINE __attribute__ ((noinline))
27+
2528
class A
2629
{
2730
public:
28-
void foo();
31+
void NOINLINE foo() { g_count += 1; }
2932
};
3033

3134
#endif

bad/b.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

bad/b.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424

2525
#include "a.h"
2626

27-
struct B
27+
class B
2828
{
29-
void bar();
29+
public:
30+
void bar()
31+
{ m_a.foo(); }
32+
33+
private:
3034
A m_a;
3135
};
3236

bad/performance.cpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

bad/unittest.cpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

dynamic_abstraction/a.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

dynamic_abstraction/b.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

dynamic_abstraction/performance.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

static_abstraction/CMakeLists.txt renamed to dynamic_interface/CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
# License along with this library; if not, write to the Free Software
1717
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818

19-
add_executable(static_abstraction a.cpp b.cpp main.cpp)
20-
add_executable(static_abstraction_ut a.cpp b.cpp unittest.cpp)
21-
add_executable(static_abstraction_perf a.cpp b.cpp performance.cpp)
19+
add_executable(dynamic_interface main.cpp)
20+
add_executable(dynamic_interface_ut unittest.cpp)
2221

23-
target_compile_options(static_abstraction PRIVATE -O3 -DNDEBUG)
24-
target_compile_options(static_abstraction_ut PRIVATE -g)
25-
target_compile_options(static_abstraction_perf PRIVATE -O3 -DNDEBUG)
22+
target_compile_options(dynamic_interface PRIVATE -O3)
23+
target_compile_options(dynamic_interface_ut PRIVATE -O3)

static_abstraction/a.h renamed to dynamic_interface/a.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
#ifndef A_H
2323
#define A_H
2424

25-
#include "interface.h"
25+
#include "a_interface.h"
26+
27+
static volatile int g_count;
28+
#define NOINLINE __attribute__ ((noinline))
2629

2730
class A :
28-
public interface<A>
31+
public AInterface
2932
{
30-
struct details {
31-
void foo();
32-
};
33-
34-
details d;
35-
friend class interface<A>;
33+
public:
34+
void NOINLINE foo() override { g_count += 1; }
3635
};
3736

3837
#endif

dynamic_abstraction/interface.h renamed to dynamic_interface/a_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
// SOFTWARE.
2121

22-
#ifndef INTERFACE_H
23-
#define INTERFACE_H
22+
#ifndef A_INTERFACE_H
23+
#define A_INTERFACE_H
2424

25-
class interface
25+
class AInterface
2626
{
2727
public:
28-
virtual ~interface() = default;
28+
virtual ~AInterface() = default;
2929
virtual void foo() = 0;
3030
};
3131

dynamic_abstraction/b.h renamed to dynamic_interface/b.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@
2222
#ifndef B_H
2323
#define B_H
2424

25-
#include "interface.h"
25+
#include "b_interface.h"
26+
#include "a_interface.h"
2627

27-
struct B
28+
class B :
29+
public BInterface
2830
{
29-
B(interface *a);
31+
public:
32+
B(AInterface *a) :
33+
m_a{a}
34+
{ }
3035

31-
void bar();
32-
interface *m_a;
36+
void bar() override
37+
{ m_a->foo(); }
38+
39+
private:
40+
AInterface *m_a;
3341
};
3442

3543
#endif

dynamic_abstraction/a.h renamed to dynamic_interface/b_interface.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
// SOFTWARE.
2121

22-
#ifndef A_H
23-
#define A_H
22+
#ifndef B_INTERFACE_H
23+
#define B_INTERFACE_H
2424

25-
#include "interface.h"
26-
27-
class A :
28-
public interface
25+
class BInterface
2926
{
3027
public:
31-
void foo() override;
28+
virtual ~BInterface() = default;
29+
virtual void bar() = 0;
3230
};
3331

3432
#endif
File renamed without changes.

0 commit comments

Comments
 (0)