Advertisement
tinyevil

Untitled

Jul 29th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. // Invariant for this type is: key_1 == key_2
  2. // no function here breaks it
  3. struct Bar{
  4.     private{
  5.         field other:shared_ptr[Bar];
  6.        
  7.         field key_1:i32;
  8.         field key_2:i32;
  9.     }
  10.    
  11.     function new(k:i32):Bar{
  12.         return Bar{key_1 = k, key_2 = k};
  13.     }
  14.    
  15.     function set_other(self:ref Bar, other:shared_ptr[Bar]){
  16.         self->other = other;
  17.     }
  18.    
  19.     function foo(bar:ref Bar){
  20.         print(bar->key_1, bar->key_2);
  21.     }
  22.  
  23.     function copy(target:out Bar, source:ref Bar){
  24.         target->key_1 = source->key_1;
  25.        
  26.         if ( not source->other.empty() ){
  27.             let other = source->other.get(); //ref Bar
  28.             print(other->key_1, other->key_2); // prints 2 1
  29.         }
  30.        
  31.         target->key_2 = source->key_2;
  32.     }
  33. };
  34.  
  35.  
  36. function foo(){
  37.     var a:shared_ptr[Bar] = shared_ptr::new(Bar::new(1));
  38.     var b:shared_ptr[Bar] = shared_ptr::new(Bar::new(2));
  39.    
  40.     // copies
  41.     external_call(a, b);
  42.    
  43.     *a = *b;
  44. }
  45.  
  46.  
  47. // compiler does not see the source code for this function
  48. function external_call(a b:shared_ptr[Bar]){
  49.     b->set_other(a);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement