|
gbase
|
CRTP base class that provides logging facilities to the derived class. More...
#include <gbase.h>
Public Member Functions | |
| GBase (const std::shared_ptr< GOptions > &gopt, std::string logger_name="") | |
| Construct a base that creates (and owns) a logger for the derived instance. | |
| GBase (const std::shared_ptr< GLogger > &logger) | |
| Construct a base that reuses an existing logger. | |
| virtual | ~GBase () |
| Destructor that logs object destruction when a logger is available. | |
| GBase (const GBase &)=default | |
| Copy constructor (shallow copy of the logger pointer). | |
| GBase & | operator= (const GBase &)=default |
| Copy assignment (shallow copy of the logger pointer). | |
| GBase (GBase &&) noexcept=default | |
| Move constructor. | |
| GBase & | operator= (GBase &&) noexcept=default |
| Move assignment operator. | |
Protected Attributes | |
| std::shared_ptr< GLogger > | log |
| Shared logger used by the derived class for emitting messages. | |
The template parameter Derived is the concrete type inheriting from this base:
Responsibilities:
std::shared_ptr<GLogger> for the derived object.GOptions for each derived object. 2) Reuse an externally managed logger (shared logger) across multiple objects.Ownership model:
std::shared_ptr so it can be shared safely and cheaply.Threading notes:
Usage guidance:
GOptions-based constructor for most components.| Derived | The concrete class inheriting from this base (CRTP pattern). |
|
inlineexplicit |
This constructor:
GOptions.logger_name used to select or label the logger instance according to the GLogger / GOptions conventions.Expected invariants after construction:
log is non-null (unless GLogger construction throws).| gopt | Shared configuration/options used to initialize GLogger. |
| logger_name | Optional logger identifier or channel name (may be empty). |
|
inlineexplicit |
This is intended for cases where logger construction is expensive or where a group of objects should share identical logging configuration and output destination.
The provided logger is stored as-is (shared ownership via std::shared_ptr). This constructor does not emit a constructor log message because it does not create or configure the logger; the calling code owns that responsibility.
Important:
logger is permitted by the type system; if null is passed, the derived object simply has no logger and lifecycle logging is suppressed.| logger | Shared logger instance to be reused by this object. |
Copying a GBase copies the std::shared_ptr<GLogger>, meaning both objects will refer to the same logger instance.
Implications:
Moves the internal std::shared_ptr<GLogger> from the source object. Marked noexcept to preserve move semantics in standard containers.
After the move:
log becomes null or remains in a valid, unspecified state per standard std::shared_ptr move semantics.
|
default |
After assignment, both objects will refer to the same logger instance. This operation does not emit any log messages.
|
defaultnoexcept |
Moves the internal std::shared_ptr<GLogger> from the source object. Marked noexcept to preserve move semantics in standard containers.
This operation does not emit any log messages.
This member is protected so derived classes can log with minimal ceremony.
Typical usage in derived classes:
log->info(...) log->debug(...) log->warning(...) Depending on how the base is constructed, this logger may be:
GOptions, orNullability:
GOptions-based constructor, this is expected to be non-null.Lifetime:
std::shared_ptr; the logger remains alive as long as any owner retains it.