Skip to content

Commit 92ae395

Browse files
Critsium-xyclaude
andcommitted
module_json: give AbacusJson a read-only document view and pass general_info its inputs, removing the last access hack in the module
para_json_test.cpp needed `#define private public` for two independent reasons. Both are now addressed by real interfaces rather than by a friend declaration. 1) It read the private static `AbacusJson::doc` in 84 places to inspect what add_json() had produced, and reset it in 5 more. AbacusJson gains two public members next to the existing allocator(): static const rapidjson::Document& document(); // read-only view static void reset(); // start a fresh empty object All 84 reads are const (HasMember, operator[], Get*/Is*), so the const view is sufficient and the document cannot be mutated through it. The 5 resets were `doc.SetObject()` twice and `doc.Parse("{}")` three times; both leave an empty object, and add_json() itself already calls SetObject() when the document is not an object, so reset() covers both. 2) It populated a local `Parameter` -- whose `input` and `sys` members are private -- only to hand it to gen_general_info(). That function read six values from it, so it now takes them directly: void gen_general_info(const Input_para& inp, const std::time_t start_time, const std::string& stru_file); This follows the precedent set by gen_init(ucell, param.inp) in the same module. Its one caller, create_Json() in para_json.cpp, already receives `Parameter&` as an argument, so the call site reads no global state; the test now builds a plain `Input_para` (a struct with public members) and passes the time and structure path as values. general_info.h consequently drops its `parameter.h` include in favour of a forward declaration of Input_para, again matching init_info.h. Also deletes a commented-out duplicate of the add_json block in general_info.cpp that still referred to the removed `param`. No assertion or expected value changed. This target only builds under ENABLE_RAPIDJSON, which the local build box cannot provide (rapidjson is not installed and cannot be fetched), so it is verified by CI. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent b223fc1 commit 92ae395

5 files changed

Lines changed: 136 additions & 128 deletions

File tree

source/source_io/module_json/abacusjson.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ class AbacusJson
6666
return doc.GetAllocator();
6767
}
6868

69+
/// @brief Read-only view of the json tree accumulated so far, for inspecting
70+
/// what add_json() produced.
71+
static const rapidjson::Document& document()
72+
{
73+
return doc;
74+
}
75+
76+
/// @brief Discard everything accumulated so far, leaving an empty json object.
77+
/// The document is static, so a caller that starts a fresh tree must say
78+
/// so explicitly rather than inherit the previous one.
79+
static void reset()
80+
{
81+
doc.SetObject();
82+
}
83+
6984
/**
7085
* @brief: The template specialization method adds value to the doc tree
7186
*

source/source_io/module_json/general_info.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#include "para_json.h"
44
#include "abacusjson.h"
55
#include "source_base/parallel_global.h"
6+
#include "source_io/module_parameter/input_parameter.h"
67
#include "source_main/version.h"
78

89
// Add json objects to gener_info
910
namespace Json
1011
{
1112

1213
#ifdef __RAPIDJSON
13-
void gen_general_info(const Parameter& param)
14+
void gen_general_info(const Input_para& inp, const std::time_t start_time, const std::string& stru_file)
1415
{
1516

1617
#ifdef VERSION
@@ -26,7 +27,6 @@ void gen_general_info(const Parameter& param)
2627
#endif
2728

2829
// start_time
29-
std::time_t start_time = param.globalv.start_time;
3030
std::string start_time_str;
3131
convert_time(start_time, start_time_str);
3232

@@ -45,27 +45,15 @@ void gen_general_info(const Parameter& param)
4545

4646
AbacusJson::add_json({"general_info", "version"}, version, false);
4747
AbacusJson::add_json({"general_info", "commit"}, commit, false);
48-
AbacusJson::add_json({"general_info", "device"}, param.inp.device, false);
48+
AbacusJson::add_json({"general_info", "device"}, inp.device, false);
4949
AbacusJson::add_json({"general_info", "mpi_num"}, mpi_num, false);
5050
AbacusJson::add_json({"general_info", "omp_num"}, omp_num, false);
51-
AbacusJson::add_json({"general_info", "pseudo_dir"}, param.inp.pseudo_dir, false);
52-
AbacusJson::add_json({"general_info", "orbital_dir"}, param.inp.orbital_dir, false);
53-
AbacusJson::add_json({"general_info", "stru_file"}, param.globalv.global_in_stru, false);
54-
AbacusJson::add_json({"general_info", "kpt_file"}, param.inp.kpoint_file, false);
51+
AbacusJson::add_json({"general_info", "pseudo_dir"}, inp.pseudo_dir, false);
52+
AbacusJson::add_json({"general_info", "orbital_dir"}, inp.orbital_dir, false);
53+
AbacusJson::add_json({"general_info", "stru_file"}, stru_file, false);
54+
AbacusJson::add_json({"general_info", "kpt_file"}, inp.kpoint_file, false);
5555
AbacusJson::add_json({"general_info", "start_time"}, start_time_str, false);
5656
AbacusJson::add_json({"general_info", "end_time"}, end_time_str, false);
57-
58-
// AbacusJson::add_Json(version,false,"general_info", "version");
59-
// AbacusJson::add_Json(commit,false,"general_info", "commit");
60-
// AbacusJson::add_Json(param.inp.device,false,"general_info", "device");
61-
// AbacusJson::add_Json(mpi_num,false,"general_info", "mpi_num");
62-
// AbacusJson::add_Json(omp_num,false,"general_info", "omp_num");
63-
// AbacusJson::add_Json(param.inp.pseudo_dir,false,"general_info", "pseudo_dir");
64-
// AbacusJson::add_Json(param.inp.orbital_dir,false,"general_info", "orbital_dir");
65-
// AbacusJson::add_Json(param.inp.stru_file,false,"general_info", "stru_file");
66-
// AbacusJson::add_Json(param.inp.kpoint_file,false,"general_info", "kpt_file");
67-
// AbacusJson::add_Json(start_time_str,false,"general_info", "start_time");
68-
// AbacusJson::add_Json(end_time_str,false,"general_info", "end_time");
6957
}
7058
#endif
7159
} // namespace Json
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#ifndef GENERAL_INFO_H
22
#define GENERAL_INFO_H
3-
#include "source_io/module_parameter/parameter.h"
3+
#include <ctime>
4+
#include <string>
5+
6+
struct Input_para;
47

58
/**
69
* @brief In this part of the code to complete the general_info part of the json tree.
710
*/
811
namespace Json
912
{
1013
#ifdef __RAPIDJSON
11-
void gen_general_info(const Parameter& param);
14+
/**
15+
* @param inp: input parameters supplying device, pseudo_dir, orbital_dir and kpoint_file.
16+
* @param start_time: when the run started.
17+
* @param stru_file: path of the structure file actually read.
18+
*/
19+
void gen_general_info(const Input_para& inp, const std::time_t start_time, const std::string& stru_file);
1220
#endif
1321
}
14-
#endif
22+
#endif

source/source_io/module_json/para_json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void json_output()
3636
void create_Json(UnitCell* ucell, const Parameter& param)
3737
{
3838
#ifdef __RAPIDJSON
39-
gen_general_info(param);
39+
gen_general_info(param.inp, param.globalv.start_time, param.globalv.global_in_stru);
4040
gen_init(ucell, param.inp);
4141
// gen_stru(ucell, param.inp);
4242
#endif

0 commit comments

Comments
 (0)