github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/libraries/Storage.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  /// @title Storage
     5  /// @notice Storage handles reading and writing to arbitary storage locations
     6  library Storage {
     7      /// @notice Returns an address stored in an arbitrary storage slot.
     8      ///         These storage slots decouple the storage layout from
     9      ///         solc's automation.
    10      /// @param _slot The storage slot to retrieve the address from.
    11      function getAddress(bytes32 _slot) internal view returns (address addr_) {
    12          assembly {
    13              addr_ := sload(_slot)
    14          }
    15      }
    16  
    17      /// @notice Stores an address in an arbitrary storage slot, `_slot`.
    18      /// @param _slot The storage slot to store the address in.
    19      /// @param _address The protocol version to store
    20      /// @dev WARNING! This function must be used cautiously, as it allows for overwriting addresses
    21      ///      in arbitrary storage slots.
    22      function setAddress(bytes32 _slot, address _address) internal {
    23          assembly {
    24              sstore(_slot, _address)
    25          }
    26      }
    27  
    28      /// @notice Returns a uint256 stored in an arbitrary storage slot.
    29      ///         These storage slots decouple the storage layout from
    30      ///         solc's automation.
    31      /// @param _slot The storage slot to retrieve the address from.
    32      function getUint(bytes32 _slot) internal view returns (uint256 value_) {
    33          assembly {
    34              value_ := sload(_slot)
    35          }
    36      }
    37  
    38      /// @notice Stores a value in an arbitrary storage slot, `_slot`.
    39      /// @param _slot The storage slot to store the address in.
    40      /// @param _value The protocol version to store
    41      /// @dev WARNING! This function must be used cautiously, as it allows for overwriting values
    42      ///      in arbitrary storage slots.
    43      function setUint(bytes32 _slot, uint256 _value) internal {
    44          assembly {
    45              sstore(_slot, _value)
    46          }
    47      }
    48  
    49      /// @notice Returns a bytes32 stored in an arbitrary storage slot.
    50      ///         These storage slots decouple the storage layout from
    51      ///         solc's automation.
    52      /// @param _slot The storage slot to retrieve the address from.
    53      function getBytes32(bytes32 _slot) internal view returns (bytes32 value_) {
    54          assembly {
    55              value_ := sload(_slot)
    56          }
    57      }
    58  
    59      /// @notice Stores a bytes32 value in an arbitrary storage slot, `_slot`.
    60      /// @param _slot The storage slot to store the address in.
    61      /// @param _value The bytes32 value to store.
    62      /// @dev WARNING! This function must be used cautiously, as it allows for overwriting values
    63      ///      in arbitrary storage slots.
    64      function setBytes32(bytes32 _slot, bytes32 _value) internal {
    65          assembly {
    66              sstore(_slot, _value)
    67          }
    68      }
    69  
    70      /// @notice Stores a bool value in an arbitrary storage slot, `_slot`.
    71      /// @param _slot The storage slot to store the bool in.
    72      /// @param _value The bool value to store
    73      /// @dev WARNING! This function must be used cautiously, as it allows for overwriting values
    74      ///      in arbitrary storage slots.
    75      function setBool(bytes32 _slot, bool _value) internal {
    76          assembly {
    77              sstore(_slot, _value)
    78          }
    79      }
    80  
    81      /// @notice Returns a bool stored in an arbitrary storage slot.
    82      /// @param _slot The storage slot to retrieve the bool from.
    83      function getBool(bytes32 _slot) internal view returns (bool value_) {
    84          assembly {
    85              value_ := sload(_slot)
    86          }
    87      }
    88  }