GrResourceHandle.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrResourceHandle_DEFINED
  8. #define GrResourceHandle_DEFINED
  9. #include "include/core/SkTypes.h"
  10. // Opaque handle to a resource. Users should always use the macro below to create a specific
  11. // template instantiation of GrResourceHandle.
  12. template <typename kind> class GrResourceHandle {
  13. public:
  14. GrResourceHandle(int value) : fValue(value) {
  15. SkASSERT(this->isValid());
  16. }
  17. GrResourceHandle() : fValue(kInvalid_ResourceHandle) {}
  18. bool operator==(const GrResourceHandle& other) const { return other.fValue == fValue; }
  19. bool isValid() const { return kInvalid_ResourceHandle != fValue; }
  20. int toIndex() const { SkASSERT(this->isValid()); return fValue; }
  21. private:
  22. static const int kInvalid_ResourceHandle = -1;
  23. int fValue;
  24. };
  25. // Creates a type "name", which is a specfic template instantiation of GrResourceHandle.
  26. #define GR_DEFINE_RESOURCE_HANDLE_CLASS(name) \
  27. struct name##Kind {}; \
  28. using name = GrResourceHandle<name##Kind>;
  29. #endif