Skip to content

Commit 5dff1b2

Browse files
committed
Generate operator<< for records deriving show
1 parent e4880a7 commit 5dff1b2

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/source/CppGenerator.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
232232

233233
writeHppFile(cppName, origin, refs.hpp, refs.hppFwds, writeCppPrototype)
234234

235-
if (r.consts.nonEmpty || r.derivingTypes.contains(DerivingType.Eq) || r.derivingTypes.contains(DerivingType.Ord)) {
235+
if (r.consts.nonEmpty || r.derivingTypes.contains(DerivingType.Eq) || r.derivingTypes.contains(DerivingType.Ord) || r.derivingTypes.contains(DerivingType.Show)) {
236236
writeCppFile(cppName, origin, refs.cpp, w => {
237237
generateCppConstants(w, r.consts, actualSelf)
238238

@@ -279,6 +279,16 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
279279
w.wl("return !(lhs < rhs);")
280280
}
281281
}
282+
if (r.derivingTypes.contains(DerivingType.Show)) {
283+
w.wl
284+
w.w(s"::std::ostream& operator<<(::std::ostream& os, const $actualSelf& self)").braced {
285+
w.wl("os << \"" ++ self ++ "{\";")
286+
for(f <- r.fields) {
287+
w.wl("os << \" " ++ idCpp.field(f.ident) ++ ":\" << self." ++ idCpp.field(f.ident) ++ ";")
288+
}
289+
w.wl("return os << \"}\";")
290+
}
291+
}
282292
})
283293
}
284294

0 commit comments

Comments
 (0)