Closed Bug 104102 Opened 24 years ago Closed 24 years ago

Let's add a new template auto pointer to allow for elegant code without goto

Categories

(Core Graveyard :: Security: UI, defect, P2)

1.0 Branch
defect

Tracking

(Not tracked)

VERIFIED WONTFIX
Future

People

(Reporter: KaiE, Assigned: KaiE)

Details

Currently, PSM uses a lot of goto statements. While goto might be fine for a C library, as NSS itself, it causes several problems for C++ code. For example, you can't put your declaration statements anywhere you want to, because a goto must not cross init of variables. At least I don't want to use goto in the new C++ code I'm writing. To allow for this, I'm suggesting the following new class. It is currently a very simple class, not sure how much we want to drive this. It does not use reference counting, it is currently designed to be used in one function only, and not to be passed around. The usage is: You construct it, as the template argument, with the type of the NSS struct that you wish to store in the auto pointer. The constructor takes the pointer to the NSS data object, and the name of the function that should be called for cleaning up when the scope is left. I wonder how much additional work must be put into this to make it compile on all of our platforms. template <class T> class nsNSSAutoCPtr { public: typedef void (*destroyer)(T* t); nsNSSAutoCPtr<T>( T* t, destroyer d) :data(t), data_destroyer(d) { } ~nsNSSAutoCPtr<T>() { if (data && data_destroyer) { (*data_destroyer)(data); } } operator T*() { return data; } T& operator*() { return *data; } T* operator->() { return data; } PRBool operator!() { return !data; } void setPtr( T *t) { data = t; } T* ptr() { return data; } private: T *data; destroyer data_destroyer; };
-> kaie Here is an example for how to use the class: nsNSSAutoCPtr<CERTCertNicknames> nicknames( CERT_NicknameStringsFromCertList(certList, NICKNAME_EXPIRED_STRING, NICKNAME_NOT_YET_VALID_STRING), CERT_FreeNicknames );
Assignee: ssaux → kaie
By the way, by overloading the operators, the existing code doesn't need to be changed. The class can just be used whereever the raw pointer is used. Operator -> works, too. The code already compiles on VC++ and gcc.
Status: NEW → ASSIGNED
cc rangansen What's the impact on overhead, code bload. Can the operators be inlined?
Priority: -- → P2
Target Milestone: --- → Future
Version: 1.01 → 2.1
It's completely inlined. There is only this header file, which defines all methods inline. There is no .cpp file, I don't think one will be necessary.
The feature suggested in this bug is no longer needed, marking WONTFIX. Some time ago I landed a helper class, implemented in security/manager/ssl/src/nsNSSCleaner.h. It is simpler and does require use of templates, it simulates by using #define and ## preprocessor operator. If you are interested to avoid goto, search security code for NSSCleanupAutoPtrClass to see examples how it can be used.
Status: ASSIGNED → RESOLVED
Closed: 24 years ago
Resolution: --- → WONTFIX
It is simpler and does require use of templates ^ not
Verified per kaie's comments.
Status: RESOLVED → VERIFIED
Product: PSM → Core
Version: psm2.1 → 1.0 Branch
Product: Core → Core Graveyard
You need to log in before you can comment on or make changes to this bug.