Bash++
Bash++ compiler internal documentation
ProgramPool.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <memory>
10#include <thread>
11#include <mutex>
12#include <unordered_map>
13#include <string>
14#include <vector>
15#include <optional>
16
17#include <bpp_include/bpp.h>
18#include <include/BashVersion.h>
19
39 private:
40 size_t max_programs = 10; // Maximum number of programs to keep in the pool
41 std::vector<std::shared_ptr<bpp::bpp_program>> programs;
42 std::unordered_map<std::string, size_t> program_indices; // Maps file paths to program indices in the pool
43 std::unordered_map<std::string, bool> open_files; // Maps file paths to whether they are currently open
44 std::unordered_map<std::string, std::string> unsaved_changes; // Maps file paths to their unsaved contents
46 std::recursive_mutex pool_mutex; // Mutex to protect access to the pool
47
48 // Pool snapshots:
49 struct Snapshot {
50 std::vector<std::shared_ptr<bpp::bpp_program>> programs_snapshot; // Snapshot of the current programs in the pool
51 std::unordered_map<std::string, size_t> program_indices_snapshot; // Snapshot of the current program indices
52 std::unordered_map<std::string, bool> open_files_snapshot; // Snapshot of the current open files
53 std::unordered_map<std::string, std::string> unsaved_changes_snapshot; // Snapshot of the current unsaved changes
54 };
55 std::unique_ptr<Snapshot> snapshot = std::make_unique<Snapshot>();
56 mutable std::recursive_mutex snapshot_mutex; // Mutex to protect access to the snapshot
57
58 bool utf16_mode = false; // Whether to use UTF-16 mode for character counting
59
61 void _remove_program(size_t index);
62 std::shared_ptr<bpp::bpp_program> _parse_program(const std::string& file_path);
63
64 // Configurable settings
65 std::shared_ptr<std::vector<std::string>> include_paths = std::make_shared<std::vector<std::string>>();
66 bool suppress_warnings = false;
67
68 void update_snapshot();
69 Snapshot load_snapshot() const;
70 public:
71 explicit ProgramPool(size_t max_programs = 10);
72
80 void add_include_path(const std::string& path);
81 void set_suppress_warnings(bool suppress);
82 void set_utf16_mode(bool mode);
83 bool get_utf16_mode() const;
84 void set_target_bash_version(const BashVersion& version);
85
94 void set_unsaved_file_contents(const std::string& file_path, const std::string& contents);
95 void remove_unsaved_file_contents(const std::string& file_path);
96
106 std::string get_file_contents(const std::string& file_path);
107
122 std::shared_ptr<bpp::bpp_program> get_program(const std::string& file_path, bool jump_queue = false);
123
130 bool has_program(const std::string& file_path);
131
138 std::shared_ptr<bpp::bpp_program> re_parse_program(const std::string& file_path);
139
145 void open_file(const std::string& file_path);
146
155 void close_file(const std::string& file_path);
156
157 void clean();
158};
Manages a pool of bpp_program objects for efficient reuse and access.
Definition ProgramPool.h:38
bool utf16_mode
Definition ProgramPool.h:58
void set_target_bash_version(const BashVersion &version)
Definition ProgramPool.cpp:40
void remove_unsaved_file_contents(const std::string &file_path)
Definition ProgramPool.cpp:52
std::unordered_map< std::string, std::string > unsaved_changes
Definition ProgramPool.h:44
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:282
std::recursive_mutex pool_mutex
Definition ProgramPool.h:46
std::shared_ptr< bpp::bpp_program > _parse_program(const std::string &file_path)
Definition ProgramPool.cpp:140
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.
Definition ProgramPool.cpp:44
bool suppress_warnings
Definition ProgramPool.h:66
void clean()
Definition ProgramPool.cpp:311
std::vector< std::shared_ptr< bpp::bpp_program > > programs
Definition ProgramPool.h:41
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:187
void open_file(const std::string &file_path)
Mark a file as open in the program pool.
Definition ProgramPool.cpp:268
std::shared_ptr< std::vector< std::string > > include_paths
Definition ProgramPool.h:65
std::string get_file_contents(const std::string &file_path)
Get the contents of a file, considering unsaved changes if present.
Definition ProgramPool.cpp:63
std::unordered_map< std::string, bool > open_files
Definition ProgramPool.h:43
BashVersion target_bash_version
Definition ProgramPool.h:45
Snapshot load_snapshot() const
Definition ProgramPool.cpp:102
void update_snapshot()
Definition ProgramPool.cpp:87
std::recursive_mutex snapshot_mutex
Definition ProgramPool.h:56
void set_utf16_mode(bool mode)
Definition ProgramPool.cpp:32
void _remove_program(size_t index)
Definition ProgramPool.cpp:108
bool has_program(const std::string &file_path)
Check if a program for the given file path exists in the pool.
Definition ProgramPool.cpp:236
std::unique_ptr< Snapshot > snapshot
Definition ProgramPool.h:55
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:243
void _remove_oldest_program()
Definition ProgramPool.cpp:83
std::unordered_map< std::string, size_t > program_indices
Definition ProgramPool.h:42
size_t max_programs
Definition ProgramPool.h:40
void set_suppress_warnings(bool suppress)
Definition ProgramPool.cpp:28
Represents a Bash version to target for code generation.
Definition BashVersion.h:21
Definition ProgramPool.h:49
std::unordered_map< std::string, std::string > unsaved_changes_snapshot
Definition ProgramPool.h:53
std::unordered_map< std::string, size_t > program_indices_snapshot
Definition ProgramPool.h:51
std::vector< std::shared_ptr< bpp::bpp_program > > programs_snapshot
Definition ProgramPool.h:50
std::unordered_map< std::string, bool > open_files_snapshot
Definition ProgramPool.h:52