前言
今天看到一個程式,用到了智能指標,
virtual tmp<volScalarField> rho() const;
借此機會把有關智能指標的知識體系重新梳理一遍
智能指標autoPtr的由來:
首先要說明智能指標本質上是模板類,是對原有指標的改進,相比更安全,

of對autoPtr的描述如下:
An auto-pointer similar to the STL auto_ptr but with automatic casting
to a reference to the type and with pointer allocation checking on access.
of中的智能指標autoPtr很像原有的auto_ptr,但不是對原有的封裝,而是重新寫了一遍
再看std::auto_ptr
std::auto_ptr的定義大致如下:
template <typename _Tp>
class auto_ptr
{
private:
_Tp *_M_ptr;
public:
explicit auto_ptr(_Tp *__p = 0) throw();
auto_ptr(auto_ptr &__a) throw();
auto_ptr &operator=(auto_ptr &__a) throw();
~auto_ptr();
_Tp &operator*() const throw();
_Tp *operator->() const throw();
_Tp *get() const throw();
_Tp *release() throw();
void reset(_Tp *__p = 0) throw();
};
再看咱of中的autoPtr是何其相似,
template<class T>
class autoPtr
{
mutable T* ptr_;
public:
typedef T Type;
inline explicit autoPtr(T* = nullptr);
inline autoPtr(const autoPtr<T>&);
inline autoPtr(const autoPtr<T>&, const bool reuse);
inline ~autoPtr();
inline bool empty() const;
inline bool valid() const;
inline T* ptr();
inline void set(T*);
inline void reset(T* = nullptr);
inline void clear();
inline T& operator()();
inline const T& operator()() const;
inline T& operator*();
inline const T& operator*() const;
inline operator const T&() const;
inline T* operator->();
inline const T* operator->() const;
inline void operator=(T*);
inline void operator=(const autoPtr<T>&);
};
在autoPtr中,我們也能看到在autoPtr中加了很多unique_ptr的元素,比如說reset(),
那為什么要用智能指標呢,他的應用場景是哪些,下次我們自己寫的時候要什么時候用
為什么要用智能指標:
舉個例子,比如說我們要實作插值演算法,用matlab寫,這很簡單
result = function(input)
現在我們學習C++了,知道了可以傳指標或參考,可以這樣寫
function(&result, input);
相比之下of更傾向于使用matlab的書寫方式
因為簡單
不僅是看起來簡單,寫起來也簡單,可以更直觀的表達想法
對于沒接觸過C或C++的人來說,不必了解參考左值右值等一系列知識
在of中寫動量方程,
fvVectorMatrix UEqn
(
fvm::ddt(rho, U)
+ fvm::div(rhoPhi, U)
+ turbulence->divDevRhoReff(rho, U)
);
首先這是個類fvVectorMatrix的建構式,還是個拷貝構造
那這就需要括號內運算子多載以及函式回傳型別都是fvVectorMatrix類
對于需要引入方程的人來說顯式寫法更直觀更簡單,如果寫成function(&result, input)這樣,一個兩個還好,方程多了會非常亂
但是C++作為效率最高的語言,參考這個概念的提出肯定有他的道理
參考是什么,很多說是別名
實際上參考的本質是指標常量,如果換C語言的寫法是這樣的
int* const rb = &a;
matlab以簡單易用著稱,但用過matlab的人都知道matlab的效率極低,
本科時候當時不會向量化編程,參加數學建模比賽跑一個回圈,跑了整整24小時,筆記本散熱也不大行,后來送修主板了
為什么matlab效率低,很關鍵的一點是matlab一直都是復制拷貝
C/C++指標傳地址效率就高很多,況且C++參考的本質就是指標,只不過是const修飾地址的指標
在簡單易用和效率之間,matlab選擇了前者,C++選擇了后者
openfoam是一個非常強大的張量計算程式,既不能舍棄易用性抬高門檻,又不能反復使用復制拷貝降低效率,稀疏矩陣那么大拷貝來拷貝去算一個程式跑好幾年成本太高
openfoam使用智能指標解決了這個問題,看起來不難讀懂又能保證效率
這也就回答了為什么要使用智能指標
再回到我們剛剛說的動量方程拷貝構造上,首先在書寫方法上依舊是matlab的顯式書寫方法,但實際上是C++的隱式移動拷貝
在哪里看到用指標了,可以打開fvm命名空間的內容
namespace fvm
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
tmp<fvMatrix<Type>>
d2dt2
(
const GeometricField<Type, fvPatchField, volMesh>& vf
)
{
return fv::d2dt2Scheme<Type>::New
(
vf.mesh(),
vf.mesh().d2dt2Scheme("d2dt2(" + vf.name() + ')')
).ref().fvmD2dt2(vf);//這里回傳的可是fvMatrix<Type>型別指標哦
}
template<class Type>
tmp<fvMatrix<Type>>
d2dt2
(
const dimensionedScalar& rho,
const GeometricField<Type, fvPatchField, volMesh>& vf
)
{
return fv::d2dt2Scheme<Type>::New
(
vf.mesh(),
vf.mesh().d2dt2Scheme("d2dt2(" + rho.name() + ',' + vf.name() + ')')
).ref().fvmD2dt2(rho, vf);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fvm
看到了嘛,隨便一個fvm命名空間下檔案,遍布tmp智能指標,
這里在指標賦值時就已經完成了類的初始化,但又因為只是指標,可以用顯式的方法去寫,只要保證回傳型別相同即可
fvm += fvc::surfaceIntegrate
(
faceFlux*tinterpScheme_().correction(vf)
);
表面上是大型矩陣相加減,實際上是智能指標這個地址在代表執行
打個比方,這就像高啟強要和趙立冬或孟德海商量一件事,趙和孟這個級別的不方便出面
出面的都是龔開疆或王秘書這樣的人,又能傳達指示又不消耗大量資源,好處就是雙方都留有余地
王秘書見到高啟強第一句話就是,“你知道我是代表誰來的嗎”
實際在問,你知道我的哪個物件的智能指標嗎
智能指標智能的點就在于不需要或者出問題的時候能自動銷毀,打開相關解構式
template<class T>
inline Foam::tmp<T>::~tmp()
{
clear();
}
template<class T>
inline void Foam::tmp<T>::clear() const
{
if (isTmp() && ptr_)
{
if (ptr_->unique())
{
delete ptr_;
ptr_ = 0;
}
else
{
ptr_->operator--();
ptr_ = 0;
}
}
}
tmp析構時對該智能指標進行了delete,autoPtr類似
記得狂飆里調查組一來最先銷毀的也是龔開疆這個智能指標,,,
這樣openfoam無需g++ -o優化也能有很好的運行效率
autoPtr與tmp的使用場合與區別
在openfoam中,autoPtr是強參考型別智能指標,tmp是弱參考型別智能指標
那我們在什么時候使用autoPtr以及tmp呢
autoPtr多使用在transport models ,boundry conditions,discretization schemes,turbulenceModel,interpolation schemes,gradient schemes或fvOptions這種動態多型中,更適合析構頻次高的地方,智能指標autoPtr能夠自動析構,因而被廣泛使用
autoPtr<incompressible::RASModel> turbulence
(
incompressible::RASModel::New(U, phi, laminarTransport)
);
autoPtr一旦有所指向只能移動,不能復制,同名同型別只能指向一個物件
再說tmp,之前有博客說tmp類似shared_ptr,實際上tmp的自我介紹中并沒有像autoPtr一樣提及相關類auto_ptr,和shared_ptr也不是繼承關系,但實作功能很接近
A class for managing temporary objects.
tmp的自我介紹中說是管理臨時變數的類,這個介紹更像是我們日常做的副本,類似現在做的博客,害怕自己忘做份筆記,日后翻看,當然這個博客的建立首先是自己已經做好了理解,因而類似的,tmp的構造需要autoPtr在前面已經做好了指定,tmp配合進行副本參考
tmp的銷毀和shared_ptr一致,具體可以見shared_ptr
一起探索openfoam也是相當有趣的一件事,非常歡迎私信討論
指正的價值要比打賞更重要,下面是個人聯系方式,希望能結交到志同道合的朋友

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/545550.html
標籤:其他
