AutoIncrementing.sol 934 B

1234567891011121314151617181920212223242526272829
  1. pragma solidity ^0.4.24;
  2. /**
  3. * @title AutoIncrementing
  4. * @author Matt Condon (@shrugs)
  5. * @dev Provides an auto-incrementing uint256 id acquired by the `Counter#nextId` getter.
  6. * Use this for issuing ERC721 ids or keeping track of request ids, anything you want, really.
  7. *
  8. * Include with `using AutoIncrementing for AutoIncrementing.Counter;`
  9. * @notice Does not allow an Id of 0, which is popularly used to signify a null state in solidity.
  10. * Does not protect from overflows, but if you have 2^256 ids, you have other problems.
  11. * (But actually, it's generally impossible to increment a counter this many times, energy wise
  12. * so it's not something you have to worry about.)
  13. */
  14. library AutoIncrementing {
  15. struct Counter {
  16. uint256 prevId; // default: 0
  17. }
  18. function nextId(Counter storage _counter)
  19. internal
  20. returns (uint256)
  21. {
  22. _counter.prevId = _counter.prevId + 1;
  23. return _counter.prevId;
  24. }
  25. }