]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/include/llvm/ADT/InMemoryStruct.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / include / llvm / ADT / InMemoryStruct.h
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
10 #ifndef LLVM_ADT_INMEMORYSTRUCT_H
11 #define LLVM_ADT_INMEMORYSTRUCT_H
12
13 #include <cassert>
14
15 namespace llvm {
16
17 /// \brief Helper object for abstracting access to an in-memory structure which
18 /// may require some kind of temporary storage.
19 ///
20 /// This class is designed to be used for accessing file data structures which
21 /// in the common case can be accessed from a direct pointer to a memory mapped
22 /// object, but which in some cases may require indirect access to a temporary
23 /// structure (which, for example, may have undergone endianness translation).
24 template<typename T>
25 class InMemoryStruct {
26   typedef T value_type;
27   typedef value_type &reference;
28   typedef value_type *pointer;
29   typedef const value_type &const_reference;
30   typedef const value_type *const_pointer;
31
32   /// \brief The smart pointer target.
33   value_type *Target;
34
35   /// \brief A temporary object which can be used as a target of the smart
36   /// pointer.
37   value_type Contents;
38
39 private:
40
41 public:
42   InMemoryStruct() : Target(0) {}
43   InMemoryStruct(reference Value) : Target(&Contents), Contents(Value) {}
44   InMemoryStruct(pointer Value) : Target(Value) {}
45   InMemoryStruct(const InMemoryStruct<T> &Value) { *this = Value; }
46   
47   void operator=(const InMemoryStruct<T> &Value) {
48     if (Value.Target != &Value.Contents) {
49       Target = Value.Target;
50     } else {
51       Target = &Contents;
52       Contents = Value.Contents;
53     }
54   }
55   
56   const_reference operator*() const {
57     assert(Target && "Cannot dereference null pointer");
58     return *Target;
59   }
60   reference operator*() {
61     assert(Target && "Cannot dereference null pointer");
62     return *Target;
63   }
64
65   const_pointer operator->() const {
66     return Target;
67   }
68   pointer operator->() {
69     return Target;
70   }
71
72   operator bool() const { return Target != 0; }
73 };
74
75 }
76
77 #endif