//
// Who to blame: Geoff Chesshire
//
#include "GenericGrid.h"
#ifdef USE_STL
#include "RCVector.h"
RCVector_STATIC_MEMBER_DATA(GenericGrid)
#endif // USE_STL
//
// class GenericGrid:
//
// Public member functions:
//
// Default constructor.
// Create a GenericGrid.
/*! \fn GenericGrid()
默认构造函数
*/
GenericGrid::GenericGrid():
ReferenceCounting() {
className = "GenericGrid"; //命名
rcData = new GenericGridData; // 创建核心数据对象
isCounted = LogicalTrue;
// 核心数据对象引用计数+1,引用计数器表明有多少个对象引用了它
rcData->incrementReferenceCount();
updateReferences(); // 关联核心数据对象
}
//
// Copy constructor. (Does a deep copy by default.)
/*! \fn GenericGrid::GenericGrid(const GenericGrid& x, const CopyType ct)
对于深层拷贝,调用x.rcData的成员函数virtualConstructor(ct)进行复制生成当前的rcData的指向。
对于浅层拷贝,直接将当前的rcData指针指向x.rcData的指向,并更新引用计数器
*/
GenericGrid::GenericGrid(
const GenericGrid& x,
const CopyType ct):
ReferenceCounting(x, ct) {
className = "GenericGrid";
switch (ct) {
case DEEP: // 深层拷贝
case NOCOPY:
rcData = (GenericGridData*)
((ReferenceCounting*)x.rcData)->virtualConstructor(ct); // 复制
isCounted = LogicalTrue;
rcData->incrementReferenceCount(); // 计数器+1
break;
case SHALLOW: // 浅层拷贝
rcData = x.rcData; // 共享核心数据
isCounted = x.isCounted;
if (isCounted) rcData->incrementReferenceCount();
break;
} // end switch
updateReferences();
}
//
// Destructor.
// 解构函数,计数器-1并释放核心数据内存空间
GenericGrid::~GenericGrid()
{ if (isCounted && rcData->decrementReferenceCount() == 0) delete rcData; }
//
// Assignment operator. (Does a deep copy.)
/*! \fn GenericGrid& GenericGrid::operator=(const GenericGrid& x)
首先确保不是自身赋值,对于同类条件下的类赋值操作,直接采用ReferenceCounting的赋值操作;
对于不同类(基类和衍生类之间的操作)情况,调用x.virtualConstructor()复制,
并转化为GenericGrid达到深层拷贝的目的。
*/
GenericGrid& GenericGrid::operator=(const GenericGrid& x) {
// ReferenceCounting::operator=(x);
if (rcData != x.rcData) { // 确保不是自身赋值
if (rcData->getClassName() == x.rcData->getClassName()) {
// 非引用条件下的类赋值操作
(ReferenceCounting&)*rcData = (ReferenceCounting&)*x.rcData;
updateReferences();
} else {
// 引用条件下的类赋值操作,用virtualConstructor()
GenericGrid& y =
*(GenericGrid*)x.virtualConstructor();
reference(y); delete &y;
} // end if
} // end if
return *this;
}
//
// Make a reference. (Does a shallow copy.)
/*! /fn void GenericGrid::reference(const GenericGrid& x)
首先确保不是自身引用,如果计数器-1后为0,则表明该对象没有其它引用,可以删除rcData指向的
GenericGridData对象,并将rcData指向x.rcData使两者共享一个GenericGridData对象,最后完成必
要的计数器操作。
*/
void GenericGrid::reference(const GenericGrid& x) {
ReferenceCounting::reference(x);
if (rcData != x.rcData) { // 确保不是自引用
// 首先计数器-1,并删除原有的核心数据对象
if (isCounted && rcData->decrementReferenceCount() == 0)
delete rcData;
// 指向指定的核心数据对象
rcData = x.rcData;
isCounted = x.isCounted;
if (isCounted) rcData->incrementReferenceCount();
updateReferences();
} // end if
}
/*! /fn void GenericGrid::reference(GenericGridData& x)
首先确保不是自身引用,如果计数器-1后为0,则表明该对象没有其它引用,可以删除rcData原有指向
,并将rcData指向作为参数的GenericGridData对象,最后完成必要的计数器操作。
*/
void GenericGrid::reference(GenericGridData& x) {
if (rcData != &x) {
if (rcData->decrementReferenceCount() == 0) delete rcData;
rcData = &x;
isCounted = !x.uncountedReferencesMayExist();
if (isCounted) rcData->incrementReferenceCount();
updateReferences();
} // end if
}
//
// Break a reference. (Replaces with a deep copy.)
/*! /fn void GenericGrid::breakReference()
采用赋值操作复制当前GenericGrid对象并实施引用操作
*/
void GenericGrid::breakReference() {
// ReferenceCounting::breakReference();
// 要求核心数据对象必须被多于一个对象引用
if (!isCounted || rcData->getReferenceCount() != 1) {
GenericGrid x = *this; // Uses the (deep) copy constructor. ???
reference(x);
} // end if
}
//
// Check that the data structure is self-consistent.
/*! /fn void GenericGrid::consistencyCheck()
检查rcData指针的合法性,并调用rcData->consistencyCheck()检查GenericGridData对象内部合法性
*/
void GenericGrid::consistencyCheck() const {
ReferenceCounting::consistencyCheck();
if (rcData == NULL) {
cerr << className << "::consistencyCheck(): "
<< "rcData == NULL for "
<< getClassName() << " " << getGlobalID() << "." << endl;
assert(rcData != NULL);
}
rcData->consistencyCheck();
}
//
// "Get" and "put" database operations.
// 文件IO操作
/*! /fn Integer GenericGrid::get(const GenericDataBase& db,const aString& name, bool getMapping)
GenericGrid的文件读取操作,直接调用GenericGridData的get方法,并做引用更新
*/
Integer GenericGrid::
get(const GenericDataBase& db, /*!< 数据库文件对象 */
const aString& name, /*!< 欲读取的对象名称 */
bool getMapping /* =true */ /*!< 是否读取映射数据 */
)
{
// 核心数据的读取
Integer returnValue = rcData->get(db, name,getMapping);
// 更新引用
updateReferences();
return returnValue;
}
/*! /fn Integer GenericGrid::put(const GenericDataBase& db,const aString& name, bool putMapping)
GenericGrid的文件写入操作,直接调用GenericGridData的put方法
*/
Integer GenericGrid::
put( GenericDataBase& db,
const aString& name,
bool putMapping /* = true */
) const
{ return rcData->put(db, name,putMapping); }
//
// Set references to reference-counted data.
/*! /fn void GenericGrid::updateReferences(const Integer )
GenericGrid的引用更新,一个空函数
*/
void GenericGrid::updateReferences(const Integer /* what */) { }
//
// Update the grid, sharing the data of another grid.
/*! /fn Integer GenericGrid::update(GenericGrid& x, const Integer what, const Integer how)
直接调用GenericGridData的update方法,并做引用更新
*/
Integer GenericGrid::update(
GenericGrid& x,
const Integer what,
const Integer how) {
Integer upd = rcData->update(*((GenericGrid&)x).rcData, what, how);
updateReferences(what);
return upd;
}
//
// Destroy optional grid data.
// 删除指定的数据
void GenericGrid::destroy(const Integer what) {
rcData->destroy(what);
updateReferences();
}
//
// Initialize the GenericGrid.
// 初始化GenericGrid对象
void GenericGrid::initialize() { rcData->initialize(); }
//
// Stream output operator.
// 流输出操作符重载
ostream& operator<<(ostream& s, const GenericGrid& g) {
s << (ReferenceCounting&)g << endl;
char _[80];
sprintf(_, " computedGeometry() = %o (octal)",
(unsigned)g.computedGeometry());
return s << _;
}
//
// class GenericGridData:
// GenericGridData的成员函数
/*! /fn GenericGridData::GenericGridData()
调用父类构造函数后设置类名,并调用initialize()做初始化
*/
GenericGridData::GenericGridData():
ReferenceCounting() {
// 命名并初始化
className = "GenericGridData";
initialize();
}
/*! /fn GenericGridData::GenericGridData()(const GenericGridData& x, const CopyType ct)
调用父类构造函数后设置类名,并调用initialize()做初始化然后用赋值操作符做拷贝深层操作
*/
GenericGridData::GenericGridData(
const GenericGridData& x,
const CopyType ct):
ReferenceCounting() {
className = "GenericGridData";
initialize();
if (ct != NOCOPY) *this = x;
}
// 解构函数
GenericGridData::~GenericGridData() { }
GenericGridData& GenericGridData::operator=(const GenericGridData& x) {
ReferenceCounting::operator=(x);
computedGeometry = NOTHING;
return *this;
}
void GenericGridData::reference(const GenericGridData& x)
{ ReferenceCounting::reference(x); }
void GenericGridData::breakReference()
{ ReferenceCounting::breakReference(); }
void GenericGridData::consistencyCheck() const
{ ReferenceCounting::consistencyCheck(); }
/*! \fn Integer GenericGridData::get(const GenericDataBase& db,const aString& name,bool getMapping)
调用GenericDataBase对象dir的get方法读取数据计算区域几何数据,但是几何数据临时存在哪里??这个不清楚
*/
Integer GenericGridData::
get(const GenericDataBase& db,
const aString& name,
bool getMapping /* =true */ ) // for AMR grids we may not get the mapping.
{
Integer returnValue = 0;
GenericDataBase& dir = *db.virtualConstructor();
db.find(dir, name, getClassName());
initialize();
// 调用dir的读取方法将数据读取计算区域几何数据到当前GenericData的computedGeometry成员中
returnValue |= dir.get(computedGeometry, "computedGeometry");
const Integer computedGeometry0 = computedGeometry;
// 根据computedGeometry成员的计算区域几何数据更新当前GenericGridData类
GenericGridData::update(*this,
computedGeometry & EVERYTHING, COMPUTEnothing);
computedGeometry = computedGeometry0;
//释放临时的GenericDataBase& dir
delete &dir;
return returnValue;
}
/*! \fn Integer GenericGridData::put(const GenericDataBase& db,const aString& name,bool putMapping)
调用GenericDataBase对象dir的put方法输出
*/
Integer GenericGridData::
put(GenericDataBase& db,
const aString& name,
bool putMapping /* = true */
) const
{
Integer returnValue = 0;
GenericDataBase& dir = *db.virtualConstructor();
db.create(dir, name, getClassName());
returnValue |= dir.put(computedGeometry, "computedGeometry");
delete &dir;
return returnValue;
}
/*! \fn Integer GenericGridData::update(GenericGridData&, const Integer what,const Integer)
什么也没干?
*/
Integer GenericGridData::update(
GenericGridData& /*x*/,
const Integer what,
const Integer /*how*/) {
// *wdh if (what & INT_MIN) cerr << "Warning: update() was called with default arguments. This is not recommended." << endl;
return NOTHING;
}
void GenericGridData::destroy(const Integer what)
{ computedGeometry &= ~what; }
void GenericGridData::geometryHasChanged(const Integer what)
{ computedGeometry &= ~what; }
void GenericGridData::initialize()
{ computedGeometry = NOTHING; destroy(~NOTHING); }