github.com/nmanchovski/burrow@v0.25.0/vent/test/EventsTest.sol (about)

     1  pragma solidity >=0.4.20;
     2  
     3  contract EventsTest {
     4  
     5      event UpdateTestEvents(
     6          bytes32 indexed name,
     7          bytes32 indexed key,
     8          bytes32 indexed description);
     9  
    10      event DeleteTestEvents(
    11          bytes32 indexed name,
    12          bytes32 indexed key,
    13          int __DELETE__);
    14  
    15      bytes32 constant TABLE_EVENTS_TEST = "TEST_EVENTS";
    16  
    17      struct Thing {
    18          string name;
    19          string description;
    20          bool exists;
    21      }
    22  
    23      int length;
    24      mapping(string => Thing) things;
    25  
    26      function addThing(string calldata _name, string calldata _description) external {
    27          Thing storage thing = things[_name];
    28          if (!thing.exists) {
    29              length++;
    30          }
    31          thing.name = _name;
    32          thing.description = _description;
    33          thing.exists = true;
    34          emit UpdateTestEvents(prefix32(_name), TABLE_EVENTS_TEST, prefix32(_description));
    35      }
    36  
    37      function removeThing(string calldata _name) external {
    38          Thing storage thing = things[_name];
    39          if (thing.exists) {
    40              length--;
    41              delete things[_name];
    42              emit DeleteTestEvents(prefix32(_name), TABLE_EVENTS_TEST, 0);
    43          }
    44      }
    45  
    46      function count() external view returns (int size) {
    47          return length;
    48      }
    49  
    50      function description(string calldata _name) external view returns (string memory _description) {
    51          return things[_name].description;
    52      }
    53  
    54      function prefix32(string memory _str) private pure returns (bytes32 str32) {
    55          assembly {
    56          // We load one word256 after the start address of _str so that we get the first 32 bytes. Note: strings
    57          // with length less than 32 bytes are padded to a multiple of 32 bytes so we are not eating consecutive
    58          // memory here.
    59              str32 := mload(add(_str, 32))
    60          }
    61      }
    62  }