LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
parser::ParserFactory Class Reference

Factory class for creating parser instances. More...

#include <ParserFactory.hpp>

Collaboration diagram for parser::ParserFactory:
Collaboration graph

Public Types

using CreatorFunc = std::function< std::unique_ptr< IDataParser >()>
 Type alias for parser creator function.
 

Static Public Member Functions

static util::Result< std::unique_ptr< IDataParser >, error::Error > CreateFromFile (const std::filesystem::path &filepath)
 Creates a parser based on file extension.
 
static util::Result< std::unique_ptr< IDataParser >, error::Error > Create (ParserType type)
 Creates a parser of specific type.
 
static std::optional< error::Error > Register (const std::string &extension, CreatorFunc creator)
 Registers a custom parser creator for a file extension.
 
static bool IsRegistered (const std::string &extension)
 Checks if a parser is registered for an extension.
 
static std::vector< std::string > GetSupportedExtensions ()
 Gets all registered file extensions.
 

Static Private Member Functions

static void InitializeDefaults ()
 Initializes default parsers (XML, JSON, etc.)
 
static void EnsureInitialized ()
 Ensures default parsers are registered (called automatically)
 

Static Private Attributes

static std::map< std::string, CreatorFunc > s_creators
 Map of file extensions to parser creators.
 

Detailed Description

Factory class for creating parser instances.

Provides centralized parser creation with support for:

  • Automatic parser selection based on file extension
  • Explicit parser type specification
  • Runtime registration of custom parsers
  • Extension to parser type mapping
Thread Safety
The factory uses a static map for parser creators. Registration should be done during application initialization before concurrent access begins.
Design Pattern
Implements the Factory Method pattern with additional registry functionality for extensibility.

Member Typedef Documentation

◆ CreatorFunc

using parser::ParserFactory::CreatorFunc = std::function<std::unique_ptr<IDataParser>()>

Type alias for parser creator function.

Member Function Documentation

◆ Create()

util::Result< std::unique_ptr< IDataParser >, error::Error > parser::ParserFactory::Create ( ParserType  type)
static

Creates a parser of specific type.

Parameters
typeThe type of parser to create
Returns
Result with parser instance or error
Example:
if (result.isOk()) {
auto parser = result.unwrap();
}
static util::Result< std::unique_ptr< IDataParser >, error::Error > Create(ParserType type)
Creates a parser of specific type.
Definition ParserFactory.cpp:141
Data parsing components and interfaces.
Definition IControler.hpp:22
@ XML
XML log file parser.
Here is the call graph for this function:

◆ CreateFromFile()

util::Result< std::unique_ptr< IDataParser >, error::Error > parser::ParserFactory::CreateFromFile ( const std::filesystem::path &  filepath)
static

Creates a parser based on file extension.

Examines the file extension and returns an appropriate parser. Falls back to XML parser if extension is not recognized.

Parameters
filepathPath to the file to parse
Returns
Result with parser instance or error
Example:
auto result = ParserFactory::CreateFromFile("log.xml");
if (result.isOk()) {
auto parser = result.unwrap();
parser->ParseData("log.xml");
} else {
// Handle error
std::cerr << result.unwrapErr().message() << std::endl;
}
static util::Result< std::unique_ptr< IDataParser >, error::Error > CreateFromFile(const std::filesystem::path &filepath)
Creates a parser based on file extension.
Definition ParserFactory.cpp:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnsureInitialized()

void parser::ParserFactory::EnsureInitialized ( )
staticprivate

Ensures default parsers are registered (called automatically)

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSupportedExtensions()

std::vector< std::string > parser::ParserFactory::GetSupportedExtensions ( )
static

Gets all registered file extensions.

Returns
Vector of registered extensions
Example:
// Use in file dialog filter
static std::vector< std::string > GetSupportedExtensions()
Gets all registered file extensions.
Definition ParserFactory.cpp:219
Here is the call graph for this function:

◆ InitializeDefaults()

void parser::ParserFactory::InitializeDefaults ( )
staticprivate

Initializes default parsers (XML, JSON, etc.)

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRegistered()

bool parser::ParserFactory::IsRegistered ( const std::string &  extension)
static

Checks if a parser is registered for an extension.

Parameters
extensionFile extension to check
Returns
true if parser is registered, false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Register()

std::optional< error::Error > parser::ParserFactory::Register ( const std::string &  extension,
CreatorFunc  creator 
)
static

Registers a custom parser creator for a file extension.

Allows runtime registration of parsers for custom file formats. The creator function will be called whenever a file with the specified extension needs parsing.

Parameters
extensionFile extension (e.g., ".log", ".txt")
creatorFunction that creates parser instances
Returns
Optional error if registration failed, std::nullopt on success
Example:
auto error = ParserFactory::Register(".mylog", []() {
return std::make_unique<MyLogParser>();
});
if (error) {
// Handle registration error
util::Logger::Error("Registration failed: {}", error->message());
}
static std::optional< error::Error > Register(const std::string &extension, CreatorFunc creator)
Registers a custom parser creator for a file extension.
Definition ParserFactory.cpp:175
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:303
Definition Error.hpp:17
Here is the call graph for this function:

Member Data Documentation

◆ s_creators

std::map< std::string, ParserFactory::CreatorFunc > parser::ParserFactory::s_creators
staticprivate

Map of file extensions to parser creators.


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