Skip to content

Commit 71fac5a

Browse files
authored
Remove Singleton from converter_main.cc (#1498)
As part of our ongoing efforts to remove the dependency on Singleton, this commit rewrites converter/converter_main.cc without the Singleton class dependency. There should be no observable change in how the converter_main works. PiperOrigin-RevId: 914130679
1 parent 86db828 commit 71fac5a

2 files changed

Lines changed: 12 additions & 28 deletions

File tree

src/converter/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ mozc_cc_binary(
744744
"//base:file_util",
745745
"//base:init_mozc",
746746
"//base:number_util",
747-
"//base:singleton",
748747
"//base:system_util",
749748
"//base/container:flat_set",
750749
"//base/protobuf:text_format",
@@ -760,6 +759,7 @@ mozc_cc_binary(
760759
"//protocol:engine_builder_cc_proto",
761760
"//request:conversion_request",
762761
"//request:request_test_util",
762+
"@com_google_absl//absl/base:no_destructor",
763763
"@com_google_absl//absl/flags:flag",
764764
"@com_google_absl//absl/log",
765765
"@com_google_absl//absl/log:check",

src/converter/converter_main.cc

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <utility>
4040
#include <vector>
4141

42+
#include "absl/base/no_destructor.h"
4243
#include "absl/flags/flag.h"
4344
#include "absl/log/check.h"
4445
#include "absl/log/log.h"
@@ -53,7 +54,6 @@
5354
#include "base/init_mozc.h"
5455
#include "base/number_util.h"
5556
#include "base/protobuf/text_format.h"
56-
#include "base/singleton.h"
5757
#include "base/system_util.h"
5858
#include "composer/composer.h"
5959
#include "config/config_handler.h"
@@ -112,31 +112,15 @@ int FindCandidate(const Segment& segment, absl::string_view value) {
112112
return -1;
113113
}
114114

115-
// Wrapper class for pos id printing
116-
class PosIdPrintUtil {
117-
public:
118-
PosIdPrintUtil(const PosIdPrintUtil&) = delete;
119-
PosIdPrintUtil& operator=(const PosIdPrintUtil&) = delete;
120-
static std::string IdToString(int id) {
121-
return Singleton<PosIdPrintUtil>::get()->IdToStringInternal(id);
122-
}
123-
124-
private:
125-
PosIdPrintUtil()
126-
: pos_id_printer_(InputFileStream(absl::GetFlag(FLAGS_id_def))) {}
127-
128-
std::string IdToStringInternal(int id) const {
129-
const absl::string_view pos_string = pos_id_printer_.IdToString(id);
130-
if (pos_string.empty()) {
131-
return absl::StrCat(id);
132-
}
133-
return absl::StrFormat("%s (%d)", pos_string, id);
115+
std::string IdToString(int id) {
116+
static const absl::NoDestructor<internal::PosIdPrinter> printer(
117+
internal::PosIdPrinter(InputFileStream(absl::GetFlag(FLAGS_id_def))));
118+
const absl::string_view pos_string = printer->IdToString(id);
119+
if (pos_string.empty()) {
120+
return absl::StrCat(id);
134121
}
135-
136-
internal::PosIdPrinter pos_id_printer_;
137-
138-
friend class Singleton<PosIdPrintUtil>;
139-
};
122+
return absl::StrFormat("%s (%d)", pos_string, id);
123+
}
140124

141125
std::string SegmentTypeToString(Segment::SegmentType type) {
142126
#define RETURN_STR(val) \
@@ -230,8 +214,8 @@ void PrintCandidate(const Segment& parent, size_t candidates_size, int num,
230214
cand.content_key);
231215
lines.push_back(absl::StrFormat("cost: %d scost: %d wcost: %d", cand.cost,
232216
cand.structure_cost, cand.wcost));
233-
lines.push_back("lid: " + PosIdPrintUtil::IdToString(cand.lid));
234-
lines.push_back("rid: " + PosIdPrintUtil::IdToString(cand.rid));
217+
lines.push_back("lid: " + IdToString(cand.lid));
218+
lines.push_back("rid: " + IdToString(cand.rid));
235219
lines.push_back("attr: " + CandidateAttributesToString(cand.attributes));
236220
lines.push_back("num_style: " + NumberStyleToString(cand.style));
237221
const std::string segbdd_str = InnerSegmentBoundaryToString(cand);

0 commit comments

Comments
 (0)