Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | from __future__ import print_function |
| 3 | import sys |
| 4 | sys.path.append('.') |
| 5 | |
| 6 | from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 7 | from example import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_Tpl, B_Tpl, C_Tpl, D_Tpl |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame^] | 8 | from example import NCVirt, NonCopyable, Movable |
| 9 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 10 | |
| 11 | class 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 | |
| 28 | ex12 = ExampleVirt(10) |
| 29 | print(runExampleVirt(ex12, 20)) |
| 30 | try: |
| 31 | runExampleVirtVirtual(ex12) |
| 32 | except Exception as e: |
| 33 | print("Caught expected exception: " + str(e)) |
| 34 | |
| 35 | ex12p = ExtendedExampleVirt(10) |
| 36 | print(runExampleVirt(ex12p, 20)) |
| 37 | print(runExampleVirtBool(ex12p)) |
| 38 | runExampleVirtVirtual(ex12p) |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 39 | |
| 40 | sys.stdout.flush() |
| 41 | |
| 42 | class VI_AR(A_Repeat): |
| 43 | def unlucky_number(self): |
| 44 | return 99 |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 45 | class VI_AT(A_Tpl): |
| 46 | def unlucky_number(self): |
| 47 | return 999 |
| 48 | |
| 49 | class VI_CR(C_Repeat): |
| 50 | def lucky_number(self): |
| 51 | return C_Repeat.lucky_number(self) + 1.25 |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 52 | class VI_CT(C_Tpl): |
| 53 | pass |
| 54 | class VI_CCR(VI_CR): |
| 55 | def lucky_number(self): |
| 56 | return VI_CR.lucky_number(self) * 10 |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 57 | class VI_CCT(VI_CT): |
| 58 | def lucky_number(self): |
| 59 | return VI_CT.lucky_number(self) * 1000 |
| 60 | |
| 61 | |
| 62 | class VI_DR(D_Repeat): |
| 63 | def unlucky_number(self): |
| 64 | return 123 |
| 65 | def lucky_number(self): |
| 66 | return 42.0 |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 67 | class 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 | |
| 75 | classes = [ |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 76 | # 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 Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 82 | ] |
| 83 | |
| 84 | for 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 Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame^] | 92 | class 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 | |
| 102 | class 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 | |
| 111 | ncv1 = NCVirtExt() |
| 112 | print("2^2 * 3^2 =") |
| 113 | ncv1.print_nc(2, 3) |
| 114 | print("4 + 5 =") |
| 115 | ncv1.print_movable(4, 5) |
| 116 | ncv2 = NCVirtExt2() |
| 117 | print("7 + 7 =") |
| 118 | ncv2.print_movable(7, 7) |
| 119 | try: |
| 120 | ncv2.print_nc(9, 9) |
| 121 | print("Something's wrong: exception not raised!") |
| 122 | except RuntimeError as e: |
| 123 | # Don't print the exception message here because it differs under debug/non-debug mode |
| 124 | print("Caught expected exception") |