1 //===- InMemoryStruct.h - Indirect Struct Access Smart Pointer --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_INMEMORYSTRUCT_H
11 #define LLVM_ADT_INMEMORYSTRUCT_H
17 /// \brief Helper object for abstracting access to an in-memory structure which
18 /// may require some kind of temporary storage.
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).
25 class InMemoryStruct {
27 typedef value_type &reference;
28 typedef value_type *pointer;
29 typedef const value_type &const_reference;
30 typedef const value_type *const_pointer;
32 /// \brief The smart pointer target.
35 /// \brief A temporary object which can be used as a target of the smart
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; }
47 void operator=(const InMemoryStruct<T> &Value) {
48 if (Value.Target != &Value.Contents) {
49 Target = Value.Target;
52 Contents = Value.Contents;
56 const_reference operator*() const {
57 assert(Target && "Cannot dereference null pointer");
60 reference operator*() {
61 assert(Target && "Cannot dereference null pointer");
65 const_pointer operator->() const {
68 pointer operator->() {
72 operator bool() const { return Target != 0; }