[08ed947] | 1 | // see also C++ investigation in ~/plg2/cfa2/mycode/string/raii/ctor-calls.cpp |
---|
| 2 | |
---|
| 3 | #include <string.hfa> |
---|
| 4 | #include <string_res.hfa> |
---|
| 5 | |
---|
| 6 | /* |
---|
| 7 | call_substrOfPart: |
---|
| 8 | In all cases, of both HL and LL, there is a temporary object. |
---|
| 9 | It represents narrowing the range, from full-string, to [1..3]. |
---|
| 10 | The only matter to control is whether that temporary shares edits with the string from which it was created. |
---|
| 11 | In HL, two knobs control it, and the |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | void calltest_HL() { |
---|
| 15 | |
---|
| 16 | #define HELPER_BODY(param) \ |
---|
| 17 | sout | "early in helper with " | param; \ |
---|
| 18 | param[0] = '+'; \ |
---|
| 19 | sout | "late in helper with " | param; |
---|
| 20 | |
---|
| 21 | void helper1( string q ) { HELPER_BODY(q) } |
---|
| 22 | void helper2( string & q ) { HELPER_BODY(q) } |
---|
| 23 | #undef HELPER_BODY |
---|
| 24 | |
---|
| 25 | string fred; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | sout | "==="; |
---|
| 29 | sout | "HL: substring of part"; |
---|
| 30 | |
---|
| 31 | sout | "---"; |
---|
| 32 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 33 | fred = "abcd"; |
---|
| 34 | sout | "before helper with " | fred; |
---|
| 35 | helper1( fred(1,3) ); |
---|
| 36 | sout | "after helper with " | fred; |
---|
| 37 | |
---|
| 38 | sout | "---"; |
---|
| 39 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 40 | fred = "abcd"; |
---|
| 41 | sout | "before helper with " | fred; |
---|
| 42 | helper2( fred(1,3) ); |
---|
| 43 | sout | "after helper with " | fred; |
---|
| 44 | |
---|
| 45 | sout | "---"; |
---|
| 46 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 47 | fred = "abcd"; |
---|
| 48 | sout | "before helper with " | fred; |
---|
| 49 | helper2( (string){ fred(1,3) } ); |
---|
| 50 | sout | "after helper with " | fred; |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | sout | "==="; |
---|
| 54 | sout | "HL: substring of whole"; |
---|
| 55 | |
---|
| 56 | sout | "---"; |
---|
| 57 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 58 | fred = "abcd"; |
---|
| 59 | sout | "before helper with " | fred; |
---|
| 60 | helper1( fred(0,4) ); |
---|
| 61 | sout | "after helper with " | fred; |
---|
| 62 | |
---|
| 63 | sout | "---"; |
---|
| 64 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 65 | fred = "abcd"; |
---|
| 66 | sout | "before helper with " | fred; |
---|
| 67 | helper2( fred(0,4) ); |
---|
| 68 | sout | "after helper with " | fred; |
---|
| 69 | |
---|
| 70 | sout | "---"; |
---|
| 71 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 72 | fred = "abcd"; |
---|
| 73 | sout | "before helper with " | fred; |
---|
| 74 | helper2( (string){ fred(0,4) } ); |
---|
| 75 | sout | "after helper with " | fred; |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | sout | "==="; |
---|
| 80 | sout | "HL: whole original string"; |
---|
| 81 | |
---|
| 82 | sout | "---"; |
---|
| 83 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 84 | fred = "abcd"; |
---|
| 85 | sout | "before helper with " | fred; |
---|
| 86 | helper1( fred ); |
---|
| 87 | sout | "after helper with " | fred; |
---|
| 88 | |
---|
| 89 | sout | "---"; |
---|
| 90 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 91 | fred = "abcd"; |
---|
| 92 | sout | "before helper with " | fred; |
---|
| 93 | helper2( fred ); |
---|
| 94 | sout | "after helper with " | fred; |
---|
| 95 | |
---|
| 96 | sout | "---"; |
---|
| 97 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 98 | fred = "abcd"; |
---|
| 99 | sout | "before helper with " | fred; |
---|
| 100 | helper2( (string){ fred } ); |
---|
| 101 | sout | "after helper with " | fred; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void calltest_LL() { |
---|
| 105 | |
---|
| 106 | #define HELPER_BODY(param) \ |
---|
| 107 | sout | "early in helper with " | param; \ |
---|
| 108 | assignAt(param, 0, '+'); \ |
---|
| 109 | sout | "late in helper with " | param; |
---|
| 110 | |
---|
| 111 | void helper1( string_res & q ) { HELPER_BODY(q) } // arg should always be a temporary constructed with COPY_VALUE |
---|
| 112 | void helper2( string_res & q ) { HELPER_BODY(q) } // arg can refer to whatever you want |
---|
| 113 | #undef HELPER_BODY |
---|
| 114 | |
---|
| 115 | /* |
---|
| 116 | In LL, both functions are translated to the same thing. |
---|
| 117 | "The only allowed call of #1" becomes respecting the restriction of the comment at helper1. |
---|
| 118 | A sys call of #2 is different from the only allowed call of #1. |
---|
| 119 | A trans call of #2 is the same as the only allowed call of #1. |
---|
| 120 | */ |
---|
| 121 | |
---|
| 122 | string_res fred; |
---|
| 123 | |
---|
| 124 | sout | "==="; |
---|
| 125 | sout | "LL: substring of part"; |
---|
| 126 | |
---|
| 127 | sout | "---"; |
---|
| 128 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 129 | fred = "abcd"; |
---|
| 130 | sout | "before helper with " | fred; |
---|
| 131 | helper1( (string_res){ fred, COPY_VALUE, 1, 3 } ); |
---|
| 132 | sout | "after helper with " | fred; |
---|
| 133 | |
---|
| 134 | sout | "---"; |
---|
| 135 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 136 | fred = "abcd"; |
---|
| 137 | sout | "before helper with " | fred; |
---|
| 138 | helper2( (string_res){ fred, SHARE_EDITS, 1, 3 } ); |
---|
| 139 | sout | "after helper with " | fred; |
---|
| 140 | |
---|
| 141 | sout | "---"; |
---|
| 142 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 143 | fred = "abcd"; |
---|
| 144 | sout | "before helper with " | fred; |
---|
| 145 | helper2( (string_res){ fred, COPY_VALUE, 1, 3 } ); |
---|
| 146 | sout | "after helper with " | fred; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | sout | "==="; |
---|
| 150 | sout | "LL: substring of whole"; |
---|
| 151 | |
---|
| 152 | sout | "---"; |
---|
| 153 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 154 | fred = "abcd"; |
---|
| 155 | sout | "before helper with " | fred; |
---|
| 156 | helper1( (string_res){ fred, COPY_VALUE, 0, 4 } ); |
---|
| 157 | sout | "after helper with " | fred; |
---|
| 158 | |
---|
| 159 | sout | "---"; |
---|
| 160 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 161 | fred = "abcd"; |
---|
| 162 | sout | "before helper with " | fred; |
---|
| 163 | helper2( (string_res){ fred, SHARE_EDITS, 0, 4 } ); |
---|
| 164 | sout | "after helper with " | fred; |
---|
| 165 | |
---|
| 166 | sout | "---"; |
---|
| 167 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 168 | fred = "abcd"; |
---|
| 169 | sout | "before helper with " | fred; |
---|
| 170 | helper2( (string_res){ fred, COPY_VALUE, 0, 4 } ); |
---|
| 171 | sout | "after helper with " | fred; |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | sout | "==="; |
---|
| 175 | sout | "LL: whole original string"; |
---|
| 176 | |
---|
| 177 | sout | "---"; |
---|
| 178 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy. |
---|
| 179 | fred = "abcd"; |
---|
| 180 | sout | "before helper with " | fred; |
---|
| 181 | helper1( (string_res){ fred, COPY_VALUE } ); |
---|
| 182 | sout | "after helper with " | fred; |
---|
| 183 | |
---|
| 184 | sout | "---"; |
---|
| 185 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects. |
---|
| 186 | fred = "abcd"; |
---|
| 187 | sout | "before helper with " | fred; |
---|
| 188 | helper2( fred ); |
---|
| 189 | sout | "after helper with " | fred; |
---|
| 190 | |
---|
| 191 | sout | "---"; |
---|
| 192 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one. |
---|
| 193 | fred = "abcd"; |
---|
| 194 | sout | "before helper with " | fred; |
---|
| 195 | helper2( (string_res){ fred, COPY_VALUE } ); |
---|
| 196 | sout | "after helper with " | fred; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | int main() { |
---|
| 202 | |
---|
| 203 | calltest_HL(); |
---|
| 204 | calltest_LL(); |
---|
| 205 | |
---|
| 206 | } |
---|