github.com/Tri-stone/burrow@v0.25.0/tests/jobs_fixtures/app35-library_handling/lib-and-contract.sol (about)

     1  pragma solidity >=0.0.0;
     2  
     3  library Set {
     4    // We define a new struct datatype that will be used to
     5    // hold its data in the calling contract.
     6    struct Data { mapping(uint => bool) flags; }
     7  
     8    // Note that the first parameter is of type "storage
     9    // reference" and thus only its storage address and not
    10    // its contents is passed as part of the call.  This is a
    11    // special feature of library functions.  It is idiomatic
    12    // to call the first parameter 'self', if the function can
    13    // be seen as a method of that object.
    14    function insert(Data storage self, uint value)
    15        public returns (bool)
    16    {
    17        if (self.flags[value])
    18            return false; // already there
    19        self.flags[value] = true;
    20        return true;
    21    }
    22  
    23    function remove(Data storage self, uint value)
    24        public returns (bool)
    25    {
    26        if (!self.flags[value])
    27            return false; // not there
    28        self.flags[value] = false;
    29        return true;
    30    }
    31  
    32    function contains(Data storage self, uint value)
    33        public returns (bool)
    34    {
    35        return self.flags[value];
    36    }
    37  }
    38  
    39  
    40  contract C {
    41      Set.Data knownValues;
    42  
    43      function register(uint value) public {
    44          // The library functions can be called without a
    45          // specific instance of the library, since the
    46          // "instance" will be the current contract.
    47          if (!Set.insert(knownValues, value))
    48              revert();
    49      }
    50      // In this contract, we can also directly access knownValues.flags, if we want.
    51  }