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#include <include/BashVersion.h>
18
38 private:
39 size_t max_programs = 10; // Maximum number of programs to keep in the pool
40 std::vector<std::shared_ptr<bpp::bpp_program>> programs;
41 std::unordered_map<std::string, size_t> program_indices; // Maps file paths to program indices in the pool
42 std::unordered_map<std::string, bool> open_files; // Maps file paths to whether they are currently open
43 std::unordered_map<std::string, std::string> unsaved_changes; // Maps file paths to their unsaved contents
45 std::recursive_mutex pool_mutex; // Mutex to protect access to the pool
46
47 // Pool snapshots:
48 struct Snapshot {
49 std::vector<std::shared_ptr<bpp::bpp_program>> programs_snapshot; // Snapshot of the current programs in the pool
50 std::unordered_map<std::string, size_t> program_indices_snapshot; // Snapshot of the current program indices
51 std::unordered_map<std::string, bool> open_files_snapshot; // Snapshot of the current open files
52 std::unordered_map<std::string, std::string> unsaved_changes_snapshot; // Snapshot of the current unsaved changes
53 };
54 std::unique_ptr<Snapshot> snapshot = std::make_unique<Snapshot>();
55 mutable std::recursive_mutex snapshot_mutex; // Mutex to protect access to the snapshot
56
57 bool utf16_mode = false; // Whether to use UTF-16 mode for character counting
58
60 void _remove_program(size_t index);
61 std::shared_ptr<bpp::bpp_program> _parse_program(const std::string& file_path);
62
63 // Configurable settings
64 std::shared_ptr<std::vector<std::string>> include_paths = std::make_shared<std::vector<std::string>>();
65 bool suppress_warnings = false;
66
67 void update_snapshot();
68 Snapshot load_snapshot() const;
69 public:
70 explicit ProgramPool(size_t max_programs = 10);
71
79 void add_include_path(const std::string& path);
80 void set_suppress_warnings(bool suppress);
81 void set_utf16_mode(bool mode);
82 bool get_utf16_mode() const;
83 void set_target_bash_version(const BashVersion& version);
84
93 void set_unsaved_file_contents(const std::string& file_path, const std::string& contents);
94 void remove_unsaved_file_contents(const std::string& file_path);
95
105 std::string get_file_contents(const std::string& file_path);
106
121 std::shared_ptr<bpp::bpp_program> get_program(const std::string& file_path, bool jump_queue = false);
122
129 bool has_program(const std::string& file_path);
130
137 std::shared_ptr<bpp::bpp_program> re_parse_program(const std::string& file_path);
138
144 void open_file(const std::string& file_path);
145
154 void close_file(const std::string& file_path);
155
156 void clean();
157};
Manages a pool of bpp_program objects for efficient reuse and access.
Definition ProgramPool.h:37
bool utf16_mode
Definition ProgramPool.h:57
void set_target_bash_version(const BashVersion &version)
Definition ProgramPool.cpp:39
void remove_unsaved_file_contents(const std::string &file_path)
Definition ProgramPool.cpp:51
std::unordered_map< std::string, std::string > unsaved_changes
Definition ProgramPool.h:43
bool get_utf16_mode() const
Definition ProgramPool.cpp:35
void close_file(const std::string &file_path)
Mark a file as closed in the program pool.
Definition ProgramPool.cpp:280
std::recursive_mutex pool_mutex
Definition ProgramPool.h:45
std::shared_ptr< bpp::bpp_program > _parse_program(const std::string &file_path)
Definition ProgramPool.cpp:139
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:43
bool suppress_warnings
Definition ProgramPool.h:65
void clean()
Definition ProgramPool.cpp:309
std::vector< std::shared_ptr< bpp::bpp_program > > programs
Definition ProgramPool.h:40
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:185
void open_file(const std::string &file_path)
Mark a file as open in the program pool.
Definition ProgramPool.cpp:266
std::shared_ptr< std::vector< std::string > > include_paths
Definition ProgramPool.h:64
std::string get_file_contents(const std::string &file_path)
Get the contents of a file, considering unsaved changes if present.
Definition ProgramPool.cpp:62
std::unordered_map< std::string, bool > open_files
Definition ProgramPool.h:42
BashVersion target_bash_version
Definition ProgramPool.h:44
Snapshot load_snapshot() const
Definition ProgramPool.cpp:101
void update_snapshot()
Definition ProgramPool.cpp:86
std::recursive_mutex snapshot_mutex
Definition ProgramPool.h:55
void set_utf16_mode(bool mode)
Definition ProgramPool.cpp:31
void _remove_program(size_t index)
Definition ProgramPool.cpp:107
bool has_program(const std::string &file_path)
Check if a program for the given file path exists in the pool.
Definition ProgramPool.cpp:234
std::unique_ptr< Snapshot > snapshot
Definition ProgramPool.h:54
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:23
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:241
void _remove_oldest_program()
Definition ProgramPool.cpp:82
std::unordered_map< std::string, size_t > program_indices
Definition ProgramPool.h:41
size_t max_programs
Definition ProgramPool.h:39
void set_suppress_warnings(bool suppress)
Definition ProgramPool.cpp:27
Represents a Bash version to target for code generation.
Definition BashVersion.h:20
Definition ProgramPool.h:48
std::unordered_map< std::string, std::string > unsaved_changes_snapshot
Definition ProgramPool.h:52
std::unordered_map< std::string, size_t > program_indices_snapshot
Definition ProgramPool.h:50
std::vector< std::shared_ptr< bpp::bpp_program > > programs_snapshot
Definition ProgramPool.h:49
std::unordered_map< std::string, bool > open_files_snapshot
Definition ProgramPool.h:51