Skip to content

Commit

Permalink
Mark C++ methods as const when possible (#84)
Browse files Browse the repository at this point in the history
* Add const-self support to c++

* Update tests
  • Loading branch information
Manishearth authored Sep 2, 2021
1 parent 9407746 commit d98d31b
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 41 deletions.
8 changes: 4 additions & 4 deletions example/cpp/ICU4XFixedDecimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ICU4XFixedDecimal {
static ICU4XFixedDecimal new_(int32_t v);
void multiply_pow10(int16_t power);
void negate();
template<typename W> diplomat::result<std::monostate, std::monostate> to_string_to_writeable(W& to);
diplomat::result<std::string, std::monostate> to_string();
template<typename W> diplomat::result<std::monostate, std::monostate> to_string_to_writeable(W& to) const;
diplomat::result<std::string, std::monostate> to_string() const;
inline const capi::ICU4XFixedDecimal* AsFFI() const { return this->inner.get(); }
inline capi::ICU4XFixedDecimal* AsFFIMut() { return this->inner.get(); }
inline ICU4XFixedDecimal(capi::ICU4XFixedDecimal* i) : inner(i) {}
Expand All @@ -44,13 +44,13 @@ inline void ICU4XFixedDecimal::multiply_pow10(int16_t power) {
inline void ICU4XFixedDecimal::negate() {
capi::ICU4XFixedDecimal_negate(this->inner.get());
}
template<typename W> inline diplomat::result<std::monostate, std::monostate> ICU4XFixedDecimal::to_string_to_writeable(W& to) {
template<typename W> inline diplomat::result<std::monostate, std::monostate> ICU4XFixedDecimal::to_string_to_writeable(W& to) const {
capi::DiplomatWriteable to_writer = diplomat::WriteableTrait<W>::Construct(to);
auto diplomat_result_raw_out_value = capi::ICU4XFixedDecimal_to_string(this->inner.get(), &to_writer);
diplomat::result<std::monostate, std::monostate> diplomat_result_out_value(diplomat_result_raw_out_value.is_ok);
return diplomat_result_out_value;
}
inline diplomat::result<std::string, std::monostate> ICU4XFixedDecimal::to_string() {
inline diplomat::result<std::string, std::monostate> ICU4XFixedDecimal::to_string() const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
auto diplomat_result_raw_out_value = capi::ICU4XFixedDecimal_to_string(this->inner.get(), &diplomat_writeable_out);
Expand Down
8 changes: 4 additions & 4 deletions example/cpp/ICU4XFixedDecimalFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ struct ICU4XFixedDecimalFormatDeleter {
class ICU4XFixedDecimalFormat {
public:
static ICU4XFixedDecimalFormatResult try_new(const ICU4XLocale& locale, const ICU4XDataProvider& provider, ICU4XFixedDecimalFormatOptions options);
template<typename W> void format_write_to_writeable(const ICU4XFixedDecimal& value, W& write);
std::string format_write(const ICU4XFixedDecimal& value);
template<typename W> void format_write_to_writeable(const ICU4XFixedDecimal& value, W& write) const;
std::string format_write(const ICU4XFixedDecimal& value) const;
inline const capi::ICU4XFixedDecimalFormat* AsFFI() const { return this->inner.get(); }
inline capi::ICU4XFixedDecimalFormat* AsFFIMut() { return this->inner.get(); }
inline ICU4XFixedDecimalFormat(capi::ICU4XFixedDecimalFormat* i) : inner(i) {}
Expand All @@ -54,11 +54,11 @@ inline ICU4XFixedDecimalFormatResult ICU4XFixedDecimalFormat::try_new(const ICU4
}
return ICU4XFixedDecimalFormatResult{ .fdf = std::move(diplomat_optional_out_value_fdf), .success = std::move(diplomat_raw_struct_out_value.success) };
}
template<typename W> inline void ICU4XFixedDecimalFormat::format_write_to_writeable(const ICU4XFixedDecimal& value, W& write) {
template<typename W> inline void ICU4XFixedDecimalFormat::format_write_to_writeable(const ICU4XFixedDecimal& value, W& write) const {
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write);
capi::ICU4XFixedDecimalFormat_format_write(this->inner.get(), value.AsFFI(), &write_writer);
}
inline std::string ICU4XFixedDecimalFormat::format_write(const ICU4XFixedDecimal& value) {
inline std::string ICU4XFixedDecimalFormat::format_write(const ICU4XFixedDecimal& value) const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
capi::ICU4XFixedDecimalFormat_format_write(this->inner.get(), value.AsFFI(), &diplomat_writeable_out);
Expand Down
4 changes: 2 additions & 2 deletions example/cpp/docs/decimal_ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

Creates a new :cpp:class:`ICU4XFixedDecimalFormat` from locale data. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html#method.try_new>`__ for more information.

.. cpp:function:: template<typename W> void format_write_to_writeable(const ICU4XFixedDecimal& value, W& write)
.. cpp:function:: template<typename W> void format_write_to_writeable(const ICU4XFixedDecimal& value, W& write) const

Formats a :cpp:class:`ICU4XFixedDecimal` to a string. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html#method.format>`__ for more information.

.. cpp:function:: std::string format_write(const ICU4XFixedDecimal& value)
.. cpp:function:: std::string format_write(const ICU4XFixedDecimal& value) const

Formats a :cpp:class:`ICU4XFixedDecimal` to a string. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html#method.format>`__ for more information.

Expand Down
4 changes: 2 additions & 2 deletions example/cpp/docs/fixed_decimal_ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

Invert the sign of the :cpp:class:`ICU4XFixedDecimal`. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.negate>`__ for more information.

.. cpp:function:: template<typename W> diplomat::result<std::monostate, std::monostate> to_string_to_writeable(W& to)
.. cpp:function:: template<typename W> diplomat::result<std::monostate, std::monostate> to_string_to_writeable(W& to) const

Format the :cpp:class:`ICU4XFixedDecimal` as a string. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.write_to>`__ for more information.

.. cpp:function:: diplomat::result<std::string, std::monostate> to_string()
.. cpp:function:: diplomat::result<std::string, std::monostate> to_string() const

Format the :cpp:class:`ICU4XFixedDecimal` as a string. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.write_to>`__ for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct FooDeleter {
};
class Foo {
public:
MyStruct get_struct();
MyStruct get_struct() const;
inline const capi::Foo* AsFFI() const { return this->inner.get(); }
inline capi::Foo* AsFFIMut() { return this->inner.get(); }
inline Foo(capi::Foo* i) : inner(i) {}
Expand All @@ -37,7 +37,7 @@ class Foo {

#include "MyStruct.hpp"

inline MyStruct Foo::get_struct() {
inline MyStruct Foo::get_struct() const {
capi::MyStruct diplomat_raw_struct_out_value = capi::Foo_get_struct(this->inner.get());
return MyStruct{ .a = std::move(diplomat_raw_struct_out_value.a), .b = std::move(static_cast<MyEnum>(diplomat_raw_struct_out_value.b)) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ struct MyStructDeleter {
};
class MyStruct {
public:
template<typename W> void write_to_writeable(W& out);
std::string write();
template<typename W> void write_unit_to_writeable(W& out);
std::string write_unit();
template<typename W> diplomat::result<std::monostate, uint8_t> write_result_to_writeable(W& out);
diplomat::result<std::string, uint8_t> write_result();
uint8_t write_no_rearrange(capi::DiplomatWriteable& out);
template<typename W> void write_to_writeable(W& out) const;
std::string write() const;
template<typename W> void write_unit_to_writeable(W& out) const;
std::string write_unit() const;
template<typename W> diplomat::result<std::monostate, uint8_t> write_result_to_writeable(W& out) const;
diplomat::result<std::string, uint8_t> write_result() const;
uint8_t write_no_rearrange(capi::DiplomatWriteable& out) const;
inline const capi::MyStruct* AsFFI() const { return this->inner.get(); }
inline capi::MyStruct* AsFFIMut() { return this->inner.get(); }
inline MyStruct(capi::MyStruct* i) : inner(i) {}
Expand All @@ -41,27 +41,27 @@ class MyStruct {
};


template<typename W> inline void MyStruct::write_to_writeable(W& out) {
template<typename W> inline void MyStruct::write_to_writeable(W& out) const {
capi::DiplomatWriteable out_writer = diplomat::WriteableTrait<W>::Construct(out);
capi::MyStruct_write(this->inner.get(), &out_writer);
}
inline std::string MyStruct::write() {
inline std::string MyStruct::write() const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
capi::MyStruct_write(this->inner.get(), &diplomat_writeable_out);
return diplomat_writeable_string;
}
template<typename W> inline void MyStruct::write_unit_to_writeable(W& out) {
template<typename W> inline void MyStruct::write_unit_to_writeable(W& out) const {
capi::DiplomatWriteable out_writer = diplomat::WriteableTrait<W>::Construct(out);
capi::MyStruct_write_unit(this->inner.get(), &out_writer);
}
inline std::string MyStruct::write_unit() {
inline std::string MyStruct::write_unit() const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
capi::MyStruct_write_unit(this->inner.get(), &diplomat_writeable_out);
return diplomat_writeable_string;
}
template<typename W> inline diplomat::result<std::monostate, uint8_t> MyStruct::write_result_to_writeable(W& out) {
template<typename W> inline diplomat::result<std::monostate, uint8_t> MyStruct::write_result_to_writeable(W& out) const {
capi::DiplomatWriteable out_writer = diplomat::WriteableTrait<W>::Construct(out);
auto diplomat_result_raw_out_value = capi::MyStruct_write_result(this->inner.get(), &out_writer);
diplomat::result<std::monostate, uint8_t> diplomat_result_out_value(diplomat_result_raw_out_value.is_ok);
Expand All @@ -71,7 +71,7 @@ template<typename W> inline diplomat::result<std::monostate, uint8_t> MyStruct::
}
return diplomat_result_out_value;
}
inline diplomat::result<std::string, uint8_t> MyStruct::write_result() {
inline diplomat::result<std::string, uint8_t> MyStruct::write_result() const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
auto diplomat_result_raw_out_value = capi::MyStruct_write_result(this->inner.get(), &diplomat_writeable_out);
Expand All @@ -82,7 +82,7 @@ inline diplomat::result<std::string, uint8_t> MyStruct::write_result() {
}
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string));
}
inline uint8_t MyStruct::write_no_rearrange(capi::DiplomatWriteable& out) {
inline uint8_t MyStruct::write_no_rearrange(capi::DiplomatWriteable& out) const {
capi::DiplomatWriteable out_writer = diplomat::WriteableTrait<W>::Construct(out);
return capi::MyStruct_write_no_rearrange(this->inner.get(), &out_writer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct MyStruct {
uint8_t a;
uint8_t b;
static MyStruct new_(uint8_t a, uint8_t b);
uint8_t get_a();
uint8_t get_a() const;
void set_b(uint8_t b);
};

Expand All @@ -39,7 +39,7 @@ inline MyStruct MyStruct::new_(uint8_t a, uint8_t b) {
capi::MyStruct diplomat_raw_struct_out_value = capi::MyStruct_new(a, b);
return MyStruct{ .a = std::move(diplomat_raw_struct_out_value.a), .b = std::move(diplomat_raw_struct_out_value.b) };
}
inline uint8_t MyStruct::get_a() {
inline uint8_t MyStruct::get_a() const {
return capi::MyStruct_get_a((capi::MyStruct*) &this);
}
inline void MyStruct::set_b(uint8_t b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct MyStructDeleter {
class MyStruct {
public:
static MyStruct new_(uint8_t a, uint8_t b);
uint8_t get_a();
uint8_t get_a() const;
void set_b(uint8_t b);
inline const capi::MyStruct* AsFFI() const { return this->inner.get(); }
inline capi::MyStruct* AsFFIMut() { return this->inner.get(); }
Expand All @@ -41,7 +41,7 @@ class MyStruct {
inline MyStruct MyStruct::new_(uint8_t a, uint8_t b) {
return MyStruct(capi::MyStruct_new(a, b));
}
inline uint8_t MyStruct::get_a() {
inline uint8_t MyStruct::get_a() const {
return capi::MyStruct_get_a(this->inner.get());
}
inline void MyStruct::set_b(uint8_t b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct FooDeleter {
};
class Foo {
public:
Bar to_bar();
Bar to_bar() const;
inline const capi::Foo* AsFFI() const { return this->inner.get(); }
inline capi::Foo* AsFFIMut() { return this->inner.get(); }
inline Foo(capi::Foo* i) : inner(i) {}
Expand All @@ -37,7 +37,7 @@ class Foo {

#include "Bar.hpp"

inline Bar Foo::to_bar() {
inline Bar Foo::to_bar() const {
capi::Bar diplomat_raw_struct_out_value = capi::Foo_to_bar(this->inner.get());
return Bar{ .y = std::move(Foo(diplomat_raw_struct_out_value.y)) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ struct MyStructDeleter {
};
struct MyStruct {
public:
void something();
void something() const;
};


inline void MyStruct::something() {
inline void MyStruct::something() const {
capi::MyStruct_something((capi::MyStruct*) &this);
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ struct MyStructDeleter {
};
struct MyStruct {
public:
template<typename W> void write_to_writeable(W& to);
std::string write();
template<typename W> void write_to_writeable(W& to) const;
std::string write() const;
};


template<typename W> inline void MyStruct::write_to_writeable(W& to) {
template<typename W> inline void MyStruct::write_to_writeable(W& to) const {
capi::DiplomatWriteable to_writer = diplomat::WriteableTrait<W>::Construct(to);
capi::MyStruct_write((capi::MyStruct*) &this, &to_writer);
}
inline std::string MyStruct::write() {
inline std::string MyStruct::write() const {
std::string diplomat_writeable_string;
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
capi::MyStruct_write((capi::MyStruct*) &this, &diplomat_writeable_out);
Expand Down
11 changes: 10 additions & 1 deletion tool/src/cpp/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,12 @@ pub fn gen_method_interface<W: fmt::Write>(
write!(out, "inline ")?;
}

if method.self_param.is_none() && is_header {
let mut is_const = false;
if let Some(ref param) = method.self_param {
if let ast::TypeName::Reference(_, mutable) = param.ty {
is_const = !mutable;
}
} else if is_header {
write!(out, "static ")?;
}

Expand Down Expand Up @@ -332,6 +337,10 @@ pub fn gen_method_interface<W: fmt::Write>(
}

write!(out, ")")?;

if is_const {
write!(out, " const")?
}
Ok(params_to_gen)
}

Expand Down

0 comments on commit d98d31b

Please sign in to comment.