Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB

  1. //===- InMemoryStruct.h - Indirect Struct Access Smart Pointer --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_ADT_INMEMORYSTRUCT_H
  10. #define LLVM_ADT_INMEMORYSTRUCT_H
  11. #include <cassert>
  12. namespace llvm {
  13. /// \brief Helper object for abstracting access to an in-memory structure which
  14. /// may require some kind of temporary storage.
  15. ///
  16. /// This class is designed to be used for accessing file data structures which
  17. /// in the common case can be accessed from a direct pointer to a memory mapped
  18. /// object, but which in some cases may require indirect access to a temporary
  19. /// structure (which, for example, may have undergone endianness translation).
  20. template<typename T>
  21. class InMemoryStruct {
  22. typedef T value_type;
  23. typedef value_type &reference;
  24. typedef value_type *pointer;
  25. typedef const value_type &const_reference;
  26. typedef const value_type *const_pointer;
  27. /// \brief The smart pointer target.
  28. value_type *Target;
  29. /// \brief A temporary object which can be used as a target of the smart
  30. /// pointer.
  31. value_type Contents;
  32. private:
  33. public:
  34. InMemoryStruct() : Target(0) {}
  35. InMemoryStruct(reference Value) : Target(&Contents), Contents(Value) {}
  36. InMemoryStruct(pointer Value) : Target(Value) {}
  37. InMemoryStruct(const InMemoryStruct<T> &Value) { *this = Value; }
  38. void operator=(const InMemoryStruct<T> &Value) {
  39. if (Value.Target != &Value.Contents) {
  40. Target = Value.Target;
  41. } else {
  42. Target = &Contents;
  43. Contents = Value.Contents;
  44. }
  45. }
  46. const_reference operator*() const {
  47. assert(Target && "Cannot dereference null pointer");
  48. return *Target;
  49. }
  50. reference operator*() {
  51. assert(Target && "Cannot dereference null pointer");
  52. return *Target;
  53. }
  54. const_pointer operator->() const {
  55. return Target;
  56. }
  57. pointer operator->() {
  58. return Target;
  59. }
  60. operator bool() const { return Target != 0; }
  61. };
  62. }
  63. #endif