What is a common naming convention for RAII classes? [closed]
Asked Answered
S

1

7

In C++, when using the Resource Acquisition is Initialization (RAII) pattern, are there any common conventions for naming the classes?

In my case, I have classes which do the following kinds of things and I would like names which are likely to invoke a useful meaning to a first time reader when seeing one of these on the stack:

  • A class to suppress logging (which can be nested).
  • A class to put in place an observer.
  • A class to record the current object being processed for the current thread.
  • A derived class to process the object in addition to the base class behavior (in prior line).

As a first cut, I have used names like these (in corresponding order to above), but hope to improve upon them:

  • class SuppressLogger
  • class ScopedObserver
  • class WithCurrentObject
  • class WithObjectProcessed : public WithCurrentObject
Songful answered 2/7, 2012 at 18:18 Comment(2)
I don't see anything wrong with those names. I might prepend Log to the last two (e.g. LogWithCurrentObject), but depending on the code, that might not be necessary.Esch
In case you are wondering how to name a RAII wrapper of a resource, you can check out for example the wrappers defined in the standard library or QMutexLocker.Granulocyte
L
10

RAII should be used all throughout the language. Since it should be the default, there's is no naming convention to follow.

Lott answered 2/7, 2012 at 18:27 Comment(1)
True, but there's a difference between a class that happens to implement RAII, and a class whose focus is RAII (to the extent of not having any methods other than a constructor and destructor). I assume William is asking about the latter.Egress

© 2022 - 2024 — McMap. All rights reserved.