blob: 121f330c8bc727eff36614eadc92938cab955ac2 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001#!/usr/bin/env python
2from __future__ import print_function
3import sys
4sys.path.append('.')
5
6from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -04007from example import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_Tpl, B_Tpl, C_Tpl, D_Tpl
Jason Rhinelandered148792016-07-21 21:31:05 -04008from example import NCVirt, NonCopyable, Movable
9
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040010
11class ExtendedExampleVirt(ExampleVirt):
12 def __init__(self, state):
13 super(ExtendedExampleVirt, self).__init__(state + 1)
14 self.data = "Hello world"
15
16 def run(self, value):
17 print('ExtendedExampleVirt::run(%i), calling parent..' % value)
18 return super(ExtendedExampleVirt, self).run(value + 1)
19
20 def run_bool(self):
21 print('ExtendedExampleVirt::run_bool()')
22 return False
23
24 def pure_virtual(self):
25 print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)
26
27
28ex12 = ExampleVirt(10)
29print(runExampleVirt(ex12, 20))
30try:
31 runExampleVirtVirtual(ex12)
32except Exception as e:
33 print("Caught expected exception: " + str(e))
34
35ex12p = ExtendedExampleVirt(10)
36print(runExampleVirt(ex12p, 20))
37print(runExampleVirtBool(ex12p))
38runExampleVirtVirtual(ex12p)
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040039
40sys.stdout.flush()
41
42class VI_AR(A_Repeat):
43 def unlucky_number(self):
44 return 99
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040045class VI_AT(A_Tpl):
46 def unlucky_number(self):
47 return 999
48
49class VI_CR(C_Repeat):
50 def lucky_number(self):
51 return C_Repeat.lucky_number(self) + 1.25
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040052class VI_CT(C_Tpl):
53 pass
54class VI_CCR(VI_CR):
55 def lucky_number(self):
56 return VI_CR.lucky_number(self) * 10
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040057class VI_CCT(VI_CT):
58 def lucky_number(self):
59 return VI_CT.lucky_number(self) * 1000
60
61
62class VI_DR(D_Repeat):
63 def unlucky_number(self):
64 return 123
65 def lucky_number(self):
66 return 42.0
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040067class VI_DT(D_Tpl):
68 def say_something(self, times):
69 print("VI_DT says:" + (' quack' * times))
70 def unlucky_number(self):
71 return 1234
72 def lucky_number(self):
73 return -4.25
74
75classes = [
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -040076 # A_Repeat, A_Tpl, # abstract (they have a pure virtual unlucky_number)
77 VI_AR, VI_AT,
78 B_Repeat, B_Tpl,
79 C_Repeat, C_Tpl,
80 VI_CR, VI_CT, VI_CCR, VI_CCT,
81 D_Repeat, D_Tpl, VI_DR, VI_DT
Jason Rhinelander0ca96e22016-08-05 17:02:33 -040082]
83
84for cl in classes:
85 print("\n%s:" % cl.__name__)
86 obj = cl()
87 obj.say_something(3)
88 print("Unlucky = %d" % obj.unlucky_number())
89 if hasattr(obj, "lucky_number"):
90 print("Lucky = %.2f" % obj.lucky_number())
91
Jason Rhinelandered148792016-07-21 21:31:05 -040092class NCVirtExt(NCVirt):
93 def get_noncopyable(self, a, b):
94 # Constructs and returns a new instance:
95 nc = NonCopyable(a*a, b*b)
96 return nc
97 def get_movable(self, a, b):
98 # Return a referenced copy
99 self.movable = Movable(a, b)
100 return self.movable
101
102class NCVirtExt2(NCVirt):
103 def get_noncopyable(self, a, b):
104 # Keep a reference: this is going to throw an exception
105 self.nc = NonCopyable(a, b)
106 return self.nc
107 def get_movable(self, a, b):
108 # Return a new instance without storing it
109 return Movable(a, b)
110
111ncv1 = NCVirtExt()
112print("2^2 * 3^2 =")
113ncv1.print_nc(2, 3)
114print("4 + 5 =")
115ncv1.print_movable(4, 5)
116ncv2 = NCVirtExt2()
117print("7 + 7 =")
118ncv2.print_movable(7, 7)
119try:
120 ncv2.print_nc(9, 9)
121 print("Something's wrong: exception not raised!")
122except RuntimeError as e:
123 # Don't print the exception message here because it differs under debug/non-debug mode
124 print("Caught expected exception")