Bash++
Bash++ compiler internal documentation
ProgramPool.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <memory>
9#include <thread>
10#include <mutex>
11#include <unordered_map>
12#include <string>
13#include <vector>
14#include <optional>
15
16#include "../bpp_include/bpp.h"
17
37 private:
38 size_t max_programs = 10; // Maximum number of programs to keep in the pool
39 std::vector<std::shared_ptr<bpp::bpp_program>> programs;
40 std::unordered_map<std::string, size_t> program_indices; // Maps file paths to program indices in the pool
41 std::unordered_map<std::string, bool> open_files; // Maps file paths to whether they are currently open
42 std::recursive_mutex pool_mutex; // Mutex to protect access to the pool
43
44 // Pool snapshots:
45 struct Snapshot {
46 std::vector<std::shared_ptr<bpp::bpp_program>> programs_snapshot; // Snapshot of the current programs in the pool
47 std::unordered_map<std::string, size_t> program_indices_snapshot; // Snapshot of the current program indices
48 std::unordered_map<std::string, bool> open_files_snapshot; // Snapshot of the current open files
49 };
50 std::atomic<std::shared_ptr<Snapshot>> snapshot; // Atomic snapshot for thread-safe access
51
52 bool utf16_mode = false; // Whether to use UTF-16 mode for character counting
53
55 void _remove_program(size_t index);
56 std::shared_ptr<bpp::bpp_program> _parse_program(
57 const std::string& file_path,
58 std::optional<std::pair<std::string, std::string>> replacement_file_contents = std::nullopt);
59
60 // Configurable settings
61 std::shared_ptr<std::vector<std::string>> include_paths = std::make_shared<std::vector<std::string>>();
62 bool suppress_warnings = false;
63
64 void update_snapshot();
65 Snapshot load_snapshot() const;
66 public:
67 explicit ProgramPool(size_t max_programs = 10);
68
76 void add_include_path(const std::string& path);
77 void set_suppress_warnings(bool suppress);
78 void set_utf16_mode(bool mode);
79 bool get_utf16_mode() const;
80
95 std::shared_ptr<bpp::bpp_program> get_program(const std::string& file_path, bool jump_queue = false);
96
103 bool has_program(const std::string& file_path);
104
111 std::shared_ptr<bpp::bpp_program> re_parse_program(const std::string& file_path);
112
122 std::shared_ptr<bpp::bpp_program> re_parse_program(
123 const std::string& file_path,
124 std::pair<std::string, std::string> replacement_file_contents);
125
131 void open_file(const std::string& file_path);
132
141 void close_file(const std::string& file_path);
142
143 void clean();
144};
Manages a pool of bpp_program objects for efficient reuse and access.
Definition ProgramPool.h:36
bool utf16_mode
Definition ProgramPool.h:52
std::atomic< std::shared_ptr< Snapshot > > snapshot
Definition ProgramPool.h:50
bool get_utf16_mode() const
Definition ProgramPool.cpp:36
void close_file(const std::string &file_path)
Mark a file as closed in the program pool.
Definition ProgramPool.cpp:276
std::recursive_mutex pool_mutex
Definition ProgramPool.h:42
bool suppress_warnings
Definition ProgramPool.h:62
void clean()
Definition ProgramPool.cpp:304
std::vector< std::shared_ptr< bpp::bpp_program > > programs
Definition ProgramPool.h:39
std::shared_ptr< bpp::bpp_program > get_program(const std::string &file_path, bool jump_queue=false)
Get or create a program for the given file path.
Definition ProgramPool.cpp:159
void open_file(const std::string &file_path)
Mark a file as open in the program pool.
Definition ProgramPool.cpp:262
std::shared_ptr< std::vector< std::string > > include_paths
Definition ProgramPool.h:61
std::unordered_map< std::string, bool > open_files
Definition ProgramPool.h:41
Snapshot load_snapshot() const
Definition ProgramPool.cpp:55
void update_snapshot()
Definition ProgramPool.cpp:44
void set_utf16_mode(bool mode)
Definition ProgramPool.cpp:32
void _remove_program(size_t index)
Definition ProgramPool.cpp:63
bool has_program(const std::string &file_path)
Check if a program for the given file path exists in the pool.
Definition ProgramPool.cpp:206
void add_include_path(const std::string &path)
Add an include path for use by all future programs to be added to the pool.
Definition ProgramPool.cpp:24
std::shared_ptr< bpp::bpp_program > re_parse_program(const std::string &file_path)
Re-parse a program for the given file path.
Definition ProgramPool.cpp:213
void _remove_oldest_program()
Definition ProgramPool.cpp:40
std::unordered_map< std::string, size_t > program_indices
Definition ProgramPool.h:40
size_t max_programs
Definition ProgramPool.h:38
void set_suppress_warnings(bool suppress)
Definition ProgramPool.cpp:28
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)
Definition ProgramPool.cpp:98
Definition ProgramPool.h:45
std::unordered_map< std::string, size_t > program_indices_snapshot
Definition ProgramPool.h:47
std::vector< std::shared_ptr< bpp::bpp_program > > programs_snapshot
Definition ProgramPool.h:46
std::unordered_map< std::string, bool > open_files_snapshot
Definition ProgramPool.h:48