Bash++
Bash++ compiler internal documentation
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
ProgramPool Class Reference

Manages a pool of bpp_program objects for efficient reuse and access. More...

#include <ProgramPool.h>

Classes

struct  Snapshot
 

Public Member Functions

 ProgramPool (size_t max_programs=10)
 
void add_include_path (const std::string &path)
 Add an include path for use by all future programs to be added to the pool.
 
void set_suppress_warnings (bool suppress)
 
void set_utf16_mode (bool mode)
 
bool get_utf16_mode () const
 
std::shared_ptr< bpp::bpp_programget_program (const std::string &file_path, bool jump_queue=false)
 Get or create a program for the given file path.
 
bool has_program (const std::string &file_path)
 Check if a program for the given file path exists in the pool.
 
std::shared_ptr< bpp::bpp_programre_parse_program (const std::string &file_path)
 Re-parse a program for the given file path.
 
std::shared_ptr< bpp::bpp_programre_parse_program (const std::string &file_path, std::pair< std::string, std::string > replacement_file_contents)
 Re-parse a program for the given file path, with unsaved changes parsed instead of the file on disk.
 
void open_file (const std::string &file_path)
 Mark a file as open in the program pool.
 
void close_file (const std::string &file_path)
 Mark a file as closed in the program pool.
 
void clean ()
 

Private Member Functions

void _remove_oldest_program ()
 
void _remove_program (size_t index)
 
std::shared_ptr< bpp::bpp_program_parse_program (const std::string &file_path, std::optional< std::pair< std::string, std::string > > replacement_file_contents=std::nullopt)
 
void update_snapshot ()
 
Snapshot load_snapshot () const
 

Private Attributes

size_t max_programs = 10
 
std::vector< std::shared_ptr< bpp::bpp_program > > programs
 
std::unordered_map< std::string, size_t > program_indices
 
std::unordered_map< std::string, bool > open_files
 
std::recursive_mutex pool_mutex
 
std::atomic< std::shared_ptr< Snapshot > > snapshot
 
bool utf16_mode = false
 
std::shared_ptr< std::vector< std::string > > include_paths = std::make_shared<std::vector<std::string>>()
 
bool suppress_warnings = false
 

Detailed Description

Manages a pool of bpp_program objects for efficient reuse and access.

Copyright (C) 2025 Andrew S. Rightenburg Bash++: Bash with classes

Each parsed program has its data stored in a bpp_program object. The ProgramPool class allows for efficient management of these objects, including adding, retrieving, and removing programs based on file paths.

By default, it keeps a maximum of 10 programs in the pool. When an 11th program is added, the oldest program is removed to make space.

Having a program in the pool means that it has been parsed and is ready for use. I.e., we do not have to re-parse the program every time we want to request information from it, Such as "where was this entity defined?" or "what's the class of this object?"

The pool is designed to be thread-safe, allowing multiple threads to access and modify the pool concurrently.

Constructor & Destructor Documentation

◆ ProgramPool()

ProgramPool::ProgramPool ( size_t  max_programs = 10)
explicit

Copyright (C) 2025 Andrew S. Rightenburg Bash++: Bash with classes

Member Function Documentation

◆ _parse_program()

std::shared_ptr< bpp::bpp_program > ProgramPool::_parse_program ( const std::string &  file_path,
std::optional< std::pair< std::string, std::string > >  replacement_file_contents = std::nullopt 
)
private

◆ _remove_oldest_program()

void ProgramPool::_remove_oldest_program ( )
private

◆ _remove_program()

void ProgramPool::_remove_program ( size_t  index)
private

◆ add_include_path()

void ProgramPool::add_include_path ( const std::string &  path)

Add an include path for use by all future programs to be added to the pool.

This mirrors the -I option in bpp

Parameters
pathThe include path to add.

◆ clean()

void ProgramPool::clean ( )

◆ close_file()

void ProgramPool::close_file ( const std::string &  file_path)

Mark a file as closed in the program pool.

If the program pool discovers that all files for a particular program have been closed, it will remove the program from the pool.

Parameters
file_pathThe file which has been closed

◆ get_program()

std::shared_ptr< bpp::bpp_program > ProgramPool::get_program ( const std::string &  file_path,
bool  jump_queue = false 
)

Get or create a program for the given file path.

If a program in the pool reports that it controls the file at the given path, that program is returned. If no program exists for that file, we create a new program, add it to the pool, and return the new program.

Parameters
file_pathThe source file path to get or create a program for.
jump_queueWhether to jump the request queue. If true, the request is processed immediately. However, we will also refuse to create a new program – if none exists, and you've asked to jump the queue, we will simply return nullptr.

◆ get_utf16_mode()

bool ProgramPool::get_utf16_mode ( ) const

◆ has_program()

bool ProgramPool::has_program ( const std::string &  file_path)

Check if a program for the given file path exists in the pool.

Parameters
file_pathThe source file path to check for a program.
Returns
true if a program exists in the pool for the given file path, false otherwise.

◆ load_snapshot()

ProgramPool::Snapshot ProgramPool::load_snapshot ( ) const
private

◆ open_file()

void ProgramPool::open_file ( const std::string &  file_path)

Mark a file as open in the program pool.

Parameters
file_pathThe file which has been opened

◆ re_parse_program() [1/2]

std::shared_ptr< bpp::bpp_program > ProgramPool::re_parse_program ( const std::string &  file_path)

Re-parse a program for the given file path.

Parameters
file_pathThe source file which has been modified, triggering the re-parse.
Returns
std::shared_ptr<bpp::bpp_program> The re-parsed program

◆ re_parse_program() [2/2]

std::shared_ptr< bpp::bpp_program > ProgramPool::re_parse_program ( const std::string &  file_path,
std::pair< std::string, std::string >  replacement_file_contents 
)

Re-parse a program for the given file path, with unsaved changes parsed instead of the file on disk.

Parameters
file_pathThe source file which has been modified, triggering the re-parse.
replacement_file_contentsA pair: the first element is the path to the file with unsaved changes, the second element is the new contents of the file to use instead of the file on disk.
Returns
std::shared_ptr<bpp::bpp_program>

◆ set_suppress_warnings()

void ProgramPool::set_suppress_warnings ( bool  suppress)

◆ set_utf16_mode()

void ProgramPool::set_utf16_mode ( bool  mode)

◆ update_snapshot()

void ProgramPool::update_snapshot ( )
private

Member Data Documentation

◆ include_paths

std::shared_ptr<std::vector<std::string> > ProgramPool::include_paths = std::make_shared<std::vector<std::string>>()
private

◆ max_programs

size_t ProgramPool::max_programs = 10
private

◆ open_files

std::unordered_map<std::string, bool> ProgramPool::open_files
private

◆ pool_mutex

std::recursive_mutex ProgramPool::pool_mutex
private

◆ program_indices

std::unordered_map<std::string, size_t> ProgramPool::program_indices
private

◆ programs

std::vector<std::shared_ptr<bpp::bpp_program> > ProgramPool::programs
private

◆ snapshot

std::atomic<std::shared_ptr<Snapshot> > ProgramPool::snapshot
private

◆ suppress_warnings

bool ProgramPool::suppress_warnings = false
private

◆ utf16_mode

bool ProgramPool::utf16_mode = false
private

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