Chombo + EB  3.0
LayoutDataI.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _LAYOUTDATAI_H_
12 #define _LAYOUTDATAI_H_
13 
14 #include "MayDay.H"
15 #include "DataIterator.H"
16 #include "SPMD.H"
17 #include "CH_Timer.H"
18 #include "NamespaceHeader.H"
19 
20 
21 // template < > void LayoutData<Real>::allocate();
22 
23 // template < > void LayoutData<int>::allocate();
24 
25 template<class T>
26 inline const T& LayoutData<T>::operator [] (const DataIndex& a_index) const
27 {
28  CH_assert(m_boxLayout.check(a_index));
29 
30  // using a DataIndex for data on another processor -
31  // if you are reading this error in a debugger, then you haven't used
32  // addBox(Box, int) correctly, or you are using a LayoutIterator to
33  // access a data holder.
34  // CH_assert(m_boxLayout.procID(a_index) == procID());
35 
36  return *(m_vector[a_index.datInd()]);
37 }
38 
39 template<class T>
40 inline const T& LayoutData<T>::operator [] (const DataIterator& a_index) const
41 {
42  return (*this)[a_index()];
43 }
44 
45 template<class T>
46 inline T& LayoutData<T>::operator [] (const DataIndex& a_index)
47 {
48  CH_assert(m_boxLayout.check(a_index));
49 
50  // using a DataIndex for data on another processor -
51  // if you are reading this error in a debugger, then you haven't used
52  // addBox(Box, int) correctly, or you are using a LayoutIterator to
53  // access a data holder.
54  //CH_assert(m_boxLayout.procID(a_index) == procID());
55 
56  return *(m_vector[a_index.datInd()]);
57 }
58 
59 template<class T>
60 inline T& LayoutData<T>::operator [] (const DataIterator& a_index)
61 {
62 
63  return (*this)[a_index()];
64 }
65 
66 template<class T>
67 inline Box LayoutData<T>::box(const DataIndex& a_index) const
68 {
69  return m_boxLayout.get(a_index);
70 }
71 
72 template<class T>
73 inline Box LayoutData<T>::box(const DataIterator& a_index) const
74 {
75  return m_boxLayout.get(a_index());
76 }
77 
78 template<class T>
80 {
81  return m_boxLayout.dataIterator();
82 }
83 
84 template<class T>
86 {
87  return m_boxLayout.timedDataIterator();
88 }
89 
90 template<class T>
92 {
93  m_boxLayout.close();
94  m_callDelete = true;
95 }
96 
97 template<class T>
99  :
100  m_boxLayout(a_dp)
101 {
102  CH_assert(a_dp.isClosed());
103  m_callDelete = true;
104  allocate();
105 }
106 
107 template<class T>
108 inline void LayoutData<T>::define(const BoxLayout& a_dp)
109 {
110  CH_assert(a_dp.isClosed());
111  m_boxLayout = a_dp;
112  allocate();
113 }
114 
115 template<class T>
117 {
118  CH_TIME("~LayoutData");
119  if (m_callDelete == true)
120  {
122  for (; it.ok(); ++it)
123  {
124  delete m_vector[it().datInd()];
125  }
126 #if CH_USE_MEMORY_TRACKING
127  m_vector.bytes -= sizeof(T)*it.size();
128 #endif
129  }
130 }
131 
132 template<class T>
134 {
135  m_callDelete = true;
136 
137  for (unsigned int i = 0; i < m_vector.size(); ++i)
138  {
139  delete m_vector[i];
140  m_vector[i] = NULL;
141  }
142 #if CH_USE_MEMORY_TRACKING
143  this->m_vector.bytes -= sizeof(T)*m_vector.size();
144 #endif
145 
147 #if CH_USE_MEMORY_TRACKING
148  m_vector.bytes += sizeof(T)*it.size();
149 #endif
150  m_vector.resize(it.size(), NULL);
151 
152  for (; it.ok(); ++it)
153  {
154  unsigned int index = it().datInd();
155  if (m_vector[index] == NULL)
156  {
157  m_vector[index] = new T;
158  if (m_vector[index] == NULL)
159  {
160  MayDay::Error("OutOfMemory in LayoutData::allocate");
161  }
162  }
163  }
164 }
165 
166 #include "NamespaceFooter.H"
167 #endif
virtual ~LayoutData()
Definition: LayoutDataI.H:116
int datInd() const
Definition: DataIndex.H:64
bool m_callDelete
Definition: LayoutData.H:129
LayoutData()
Definition: LayoutDataI.H:91
#define CH_assert(cond)
Definition: CHArray.H:37
Vector< T * > m_vector
Definition: LayoutData.H:122
A not-necessarily-disjoint collective of boxes.
Definition: BoxLayout.H:146
DataIterator dataIterator() const
Definition: LayoutDataI.H:79
Box box(const DataIndex &a_index) const
Definition: LayoutDataI.H:67
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:110
Definition: DataIterator.H:140
BoxLayout m_boxLayout
Definition: LayoutData.H:116
void resize(unsigned int isize)
Definition: Vector.H:323
int size() const
Definition: DataIterator.H:168
#define CH_TIME(name)
Definition: CH_Timer.H:59
void allocate()
Definition: LayoutDataI.H:133
TimedDataIterator timedDataIterator() const
Definition: LayoutDataI.H:85
size_t size() const
Definition: Vector.H:177
virtual void define(const BoxLayout &a_dp)
Definition: LayoutDataI.H:108
static void Error(const char *const a_msg=m_nullString, int m_exitCode=CH_DEFAULT_ERROR_CODE)
Print out message to cerr and exit with the specified exit code.
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
Definition: DataIndex.H:112
const T & operator[](const DataIndex &a_index) const
const accessor function
Definition: LayoutDataI.H:26
bool isClosed() const
Definition: BoxLayout.H:696
Definition: TimedDataIterator.H:23