|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0; |
|
|
|
|
|
* @title Counters |
|
* @author Matt Condon (@shrugs) |
|
* @dev Provides counters that can only be incremented, decremented or reset. This can be used e.g. to track the number |
|
* of elements in a mapping, issuing ERC721 ids, or counting request ids. |
|
* |
|
* Include with `using Counters for Counters.Counter;` |
|
*/ |
|
library Counters { |
|
struct Counter { |
|
|
|
|
|
|
|
uint256 _value; |
|
} |
|
|
|
function current(Counter storage counter) internal view returns (uint256) { |
|
return counter._value; |
|
} |
|
|
|
function increment(Counter storage counter) internal { |
|
unchecked { |
|
counter._value += 1; |
|
} |
|
} |
|
|
|
function decrement(Counter storage counter) internal { |
|
uint256 value = counter._value; |
|
require(value > 0, "Counter: decrement overflow"); |
|
unchecked { |
|
counter._value = value - 1; |
|
} |
|
} |
|
|
|
function reset(Counter storage counter) internal { |
|
counter._value = 0; |
|
} |
|
} |
|
|