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>

Collaboration diagram for ProgramPool:
Collaboration graph
[legend]

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
 
void set_target_bash_version (const BashVersion &version)
 
void set_unsaved_file_contents (const std::string &file_path, const std::string &contents)
 Set the unsaved contents for a file to reflect in-editor changes.
 
void remove_unsaved_file_contents (const std::string &file_path)
 
std::string get_file_contents (const std::string &file_path)
 Get the contents of a file, considering unsaved changes if present.
 
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.
 
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)
 
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::unordered_map< std::string, std::string > unsaved_changes
 
BashVersion target_bash_version = {5, 2}
 
std::recursive_mutex pool_mutex
 
std::unique_ptr< Snapshotsnapshot = std::make_unique<Snapshot>()
 
std::recursive_mutex snapshot_mutex
 
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)
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_file_contents()

std::string ProgramPool::get_file_contents ( const std::string &  file_path)

Get the contents of a file, considering unsaved changes if present.

If there are unsaved changes for the given file path, those contents are returned. Otherwise, the contents are read from the file on disk.

Parameters
file_pathThe file path to get the contents of.
Returns
std::string The contents of the file.

◆ 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()

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

◆ remove_unsaved_file_contents()

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

◆ set_suppress_warnings()

void ProgramPool::set_suppress_warnings ( bool  suppress)

◆ set_target_bash_version()

void ProgramPool::set_target_bash_version ( const BashVersion version)

◆ set_unsaved_file_contents()

void ProgramPool::set_unsaved_file_contents ( const std::string &  file_path,
const std::string &  contents 
)

Set the unsaved contents for a file to reflect in-editor changes.

Future re-parses of the relevant program will use these contents

Parameters
file_pathThe file path to set unsaved contents for.
contentsThe unsaved contents of the file.

◆ 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::unique_ptr<Snapshot> ProgramPool::snapshot = std::make_unique<Snapshot>()
private

◆ snapshot_mutex

std::recursive_mutex ProgramPool::snapshot_mutex
mutableprivate

◆ suppress_warnings

bool ProgramPool::suppress_warnings = false
private

◆ target_bash_version

BashVersion ProgramPool::target_bash_version = {5, 2}
private

◆ unsaved_changes

std::unordered_map<std::string, std::string> ProgramPool::unsaved_changes
private

◆ utf16_mode

bool ProgramPool::utf16_mode = false
private

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