-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsuperclass.cpp
More file actions
80 lines (64 loc) · 2.19 KB
/
superclass.cpp
File metadata and controls
80 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* superclass.cpp
*
* Created on: 11 oct. 2013
* Author: Loic Oudot
*/
#include <iostream>
#include <time.h>
#include "java4cpp/java_classes.h"
#include "testsunit.h"
#include "superclass.h"
using namespace java4cpp::demos;
// SFINAE to detect presence of method baseMethod in a class
template<typename T>
class has_baseMethod
{
struct Fallback { int baseMethod; };
struct Derived : T, Fallback { };
template<typename U, U> struct Check;
typedef char ArrayOfOne[1];
typedef char ArrayOfTwo[2];
template<typename U>
static ArrayOfOne & func(Check<int Fallback::*, &U::baseMethod> *);
template<typename U>
static ArrayOfTwo & func(...);
public:
typedef has_baseMethod type;
static const bool value = sizeof(func<Derived>(0)) == 2;
};
void allSuperclass()
{
std::cout << "================================" << std::endl;
std::cout << "Superclass" << std::endl;
std::cout << "================================" << std::endl;
superclass();
casting();
}
void superclass()
{
std::cout << "superclass: ";
assertThat(Baseclass().baseMethod()).isEqualTo(1);
assertThat(Baseclass().overridenMethod()).isEqualTo(2);
assertThat(Superclass().baseMethod()).isEqualTo(1);
assertThat(Superclass().overridenMethod()).isEqualTo(3);
assertThat(Superclass::WithSuperclass().baseMethod()).isEqualTo(1);
assertThat(Superclass::WithSuperclass().overridenMethod()).isEqualTo(4);
assertThat(Superclass::WithoutSuperclass().overridenMethod()).isEqualTo(5);
assertThat(has_baseMethod<Superclass::WithSuperclass>::value).isTrue();
assertThat(has_baseMethod<Superclass::WithoutSuperclass>::value).isFalse();
std::cout << "ok" << std::endl;
}
void casting()
{
std::cout << "casting: ";
Baseclass baseclass;
assertThat(dynamic_cast<Baseclass*>(&baseclass)).isNotNull();
Superclass superclass;
assertThat(dynamic_cast<Baseclass*>(&superclass)).isNotNull();
Superclass::WithSuperclass withSuperclass;
assertThat(dynamic_cast<Baseclass*>(&withSuperclass)).isNotNull();
Superclass::WithoutSuperclass withoutSuperclass;
assertThat(dynamic_cast<Baseclass*>(&withoutSuperclass)).isNull();
std::cout << "ok" << std::endl;
}