Skip to content

Commit 0f945db

Browse files
Make logger_ const pointer and modify SimulationLogger with const qualifiers as necessary
1 parent e1742e7 commit 0f945db

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

Code/Source/solver/BoundaryCondition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class BoundaryCondition {
8080
using StringDoubleMap = std::map<std::string, double>;
8181

8282
/// @brief Data members for BC
83-
const faceType* face_ = nullptr; ///< Face associated with the BC (can be null)
83+
const faceType* face_ = nullptr; ///< Face associated with the BC (not owned by BoundaryCondition)
8484
int global_num_nodes_ = 0; ///< Global number of nodes on the face
8585
int local_num_nodes_ = 0; ///< Local number of nodes on this processor
8686
std::vector<std::string> array_names_; ///< Names of arrays to read from VTP file
@@ -91,7 +91,7 @@ class BoundaryCondition {
9191
std::string vtp_file_path_; ///< Path to VTP file (empty if uniform)
9292
std::map<int, int> global_node_map_; ///< Maps global node IDs to local array indices
9393
std::unique_ptr<VtkVtpData> vtp_data_; ///< VTP data object
94-
SimulationLogger* logger_ = nullptr; ///< Logger for warnings/info
94+
const SimulationLogger* logger_ = nullptr; ///< Logger for warnings/info (not owned by BoundaryCondition)
9595

9696
public:
9797
/// @brief Tolerance for point matching in VTP files

Code/Source/solver/SimulationLogger.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SimulationLogger {
4747
this->initialize(file_name, cout_write);
4848
}
4949

50-
void initialize(const std::string& file_name, bool cout_write=false)
50+
void initialize(const std::string& file_name, bool cout_write=false) const
5151
{
5252
log_file_.open(file_name);
5353
if (log_file_.fail()) {
@@ -65,7 +65,7 @@ class SimulationLogger {
6565
log_file_.close();
6666
}
6767

68-
template <class T> SimulationLogger& operator<< (const T& value)
68+
template <class T> const SimulationLogger& operator<< (const T& value) const
6969
{
7070
if (file_name_ == "") {
7171
return *this;
@@ -81,7 +81,7 @@ class SimulationLogger {
8181
}
8282

8383
// Special handling for vector<string>
84-
SimulationLogger& operator<< (const std::vector<std::string>& values)
84+
const SimulationLogger& operator<< (const std::vector<std::string>& values) const
8585
{
8686
if (file_name_ == "") {
8787
return *this;
@@ -101,7 +101,7 @@ class SimulationLogger {
101101
return *this;
102102
}
103103

104-
SimulationLogger& operator<<(std::ostream&(*value)(std::ostream& o))
104+
const SimulationLogger& operator<<(std::ostream&(*value)(std::ostream& o)) const
105105
{
106106
if (file_name_ == "") {
107107
return *this;
@@ -119,7 +119,7 @@ class SimulationLogger {
119119
/// @brief Log a message with automatic space separation
120120
/// @param args Arguments to log
121121
template<typename... Args>
122-
SimulationLogger& log_message(const Args&... args) {
122+
const SimulationLogger& log_message(const Args&... args) const {
123123
if (file_name_ == "") return *this;
124124

125125
bool first = true;
@@ -129,10 +129,14 @@ class SimulationLogger {
129129
}
130130

131131
private:
132-
bool cout_write_ = false;
133-
bool initialized_ = false;
134-
std::string file_name_;
135-
std::ofstream log_file_;
132+
// These members are marked mutable because they can be modified in const member functions.
133+
// While logging writes to a file (modifying these members), it doesn't change the logical
134+
// state of the logger - this is exactly what mutable is designed for: implementation
135+
// details that need to change even when the object's public state remains constant.
136+
mutable bool cout_write_ = false;
137+
mutable bool initialized_ = false;
138+
mutable std::string file_name_;
139+
mutable std::ofstream log_file_;
136140
};
137141

138142
#endif

0 commit comments

Comments
 (0)