gbase
Loading...
Searching...
No Matches
GBase< Derived > Class Template Reference

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).
 
GBaseoperator= (const GBase &)=default
 Copy assignment (shallow copy of the logger pointer).
 
 GBase (GBase &&) noexcept=default
 Move constructor.
 
GBaseoperator= (GBase &&) noexcept=default
 Move assignment operator.
 

Protected Attributes

std::shared_ptr< GLoggerlog
 Shared logger used by the derived class for emitting messages.
 

Detailed Description

template<typename Derived>
class GBase< Derived >

The template parameter Derived is the concrete type inheriting from this base:

class MyType : public GBase<MyType> { ... };
CRTP base class that provides logging facilities to the derived class.
Definition gbase.h:116

Responsibilities:

  • Construct and hold a std::shared_ptr<GLogger> for the derived object.
  • Emit standard lifecycle log messages (constructor/destructor) when a logger exists.
  • Support two usage modes: 1) Create a dedicated logger from GOptions for each derived object. 2) Reuse an externally managed logger (shared logger) across multiple objects.

Ownership model:

  • The logger is stored as a std::shared_ptr so it can be shared safely and cheaply.
  • Copying a GBase performs a shallow copy of the logger pointer (shared ownership).

Threading notes:

  • This class does not add synchronization; it only stores and forwards a logger pointer.
  • Whether logging is thread-safe depends on the GLogger implementation and its sinks.

Usage guidance:

  • Prefer the GOptions-based constructor for most components.
  • Prefer the shared-logger constructor when constructing many short-lived objects, or when logger creation/configuration is known to be expensive for a particular subsystem.
Template Parameters
DerivedThe concrete class inheriting from this base (CRTP pattern).

Definition at line 115 of file gbase.h.

Constructor & Destructor Documentation

◆ GBase() [1/4]

template<typename Derived >
GBase< Derived >::GBase ( const std::shared_ptr< GOptions > & gopt,
std::string logger_name = "" )
inlineexplicit

This constructor:

  • Creates a GLogger using the provided GOptions.
  • Uses the derived type name (via compiler RTTI) as the logical component name.
  • Optionally accepts an additional logger_name used to select or label the logger instance according to the GLogger / GOptions conventions.
  • Emits a constructor log message through the newly created logger.

Expected invariants after construction:

  • The protected member log is non-null (unless GLogger construction throws).
Parameters
goptShared configuration/options used to initialize GLogger.
logger_nameOptional logger identifier or channel name (may be empty).

Definition at line 134 of file gbase.h.

◆ GBase() [2/4]

template<typename Derived >
GBase< Derived >::GBase ( const std::shared_ptr< GLogger > & logger)
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:

  • Passing a null logger is permitted by the type system; if null is passed, the derived object simply has no logger and lifecycle logging is suppressed.
Parameters
loggerShared logger instance to be reused by this object.

Definition at line 156 of file gbase.h.

◆ ~GBase()

template<typename Derived >
virtual GBase< Derived >::~GBase ( )
inlinevirtual

Behavior:

  • If log is non-null, emits a standard destructor message.
  • If log is null, performs no logging.

Rationale:

  • The destructor must be safe in all shutdown orders; it therefore checks log before attempting to log.

Definition at line 170 of file gbase.h.

◆ GBase() [3/4]

template<typename Derived >
GBase< Derived >::GBase ( const GBase< Derived > & )
default

Copying a GBase copies the std::shared_ptr<GLogger>, meaning both objects will refer to the same logger instance.

Implications:

  • This is usually desirable: copies of an object retain the same logging channel/sinks.
  • If a derived type requires independent loggers per instance, it should avoid copying or provide a custom copy policy at the derived level.

◆ GBase() [4/4]

template<typename Derived >
GBase< Derived >::GBase ( GBase< Derived > && )
defaultnoexcept

Moves the internal std::shared_ptr<GLogger> from the source object. Marked noexcept to preserve move semantics in standard containers.

After the move:

  • The moved-to object receives the logger pointer.
  • The moved-from object's log becomes null or remains in a valid, unspecified state per standard std::shared_ptr move semantics.

Member Function Documentation

◆ operator=() [1/2]

template<typename Derived >
GBase & GBase< Derived >::operator= ( const GBase< Derived > & )
default

After assignment, both objects will refer to the same logger instance. This operation does not emit any log messages.

◆ operator=() [2/2]

template<typename Derived >
GBase & GBase< Derived >::operator= ( GBase< Derived > && )
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.

Field Documentation

◆ log

template<typename Derived >
std::shared_ptr<GLogger> GBase< Derived >::log
protected

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:

  • A dedicated logger created from GOptions, or
  • A shared logger passed in from external code.

Nullability:

  • When constructed with the GOptions-based constructor, this is expected to be non-null.
  • When constructed with the shared-logger constructor, this is whatever was provided (including the possibility of null).

Lifetime:

  • This is a std::shared_ptr; the logger remains alive as long as any owner retains it.

Definition at line 256 of file gbase.h.


The documentation for this class was generated from the following file: