1//===- FragmentRef.h ------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_LD_FRAGMENT_REFERENCE_H
10#define MCLD_LD_FRAGMENT_REFERENCE_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <mcld/ADT/SizeTraits.h>
16#include <mcld/ADT/TypeTraits.h>
17#include <mcld/LD/Fragment.h>
18
19namespace mcld
20{
21
22class Layout;
23
24/// compunteFragmentSize - compute the specific Fragment size
25uint64_t computeFragmentSize(const Layout& pLayout,
26                             const Fragment& pFrag);
27
28/** \class FragmentRef
29 *  \brief FragmentRef is a reference of a Fragment's contetnt.
30 *
31 */
32class FragmentRef
33{
34public:
35  typedef uint64_t Offset; // FIXME: use SizeTraits<T>::Offset
36  typedef NonConstTraits<unsigned char>::pointer Address;
37  typedef ConstTraits<unsigned char>::pointer ConstAddress;
38
39public:
40  FragmentRef();
41  FragmentRef(Fragment& pFrag, Offset pOffset = 0);
42  ~FragmentRef();
43
44  // -----  modifiers  ----- //
45  FragmentRef& assign(const FragmentRef& pCopy);
46
47  FragmentRef& assign(Fragment& pFrag, Offset pOffset = 0);
48
49  /// memcpy - copy memory
50  /// copy memory from the fragment to the pDesc.
51  /// @pDest - the destination address
52  /// @pNBytes - copies pNBytes from the fragment[offset()+pOffset]
53  /// @pOffset - additional offset.
54  ///            the start address offset from fragment[offset()]
55  void memcpy(void* pDest, size_t pNBytes, Offset pOffset = 0) const;
56
57  // -----  observers  ----- //
58  Fragment* frag()
59  { return m_pFragment; }
60
61  const Fragment* frag() const
62  { return m_pFragment; }
63
64  Offset offset() const
65  { return m_Offset; }
66
67  // -----  dereference  ----- //
68  Address deref();
69
70  ConstAddress deref() const;
71
72  Address operator*()
73  { return deref(); }
74
75  ConstAddress operator*() const
76  { return deref(); }
77
78private:
79  Fragment* m_pFragment;
80  Offset m_Offset;
81};
82
83} // namespace of mcld
84
85#endif
86
87