Add moveable_types=forward_setter option for perfect forwarding setters
Adds `forward_setter` value to `moveable_types` option, generating perfect forwarding setters for complex types while preserving traditional setters for primitives. Also fixes missing `operator<` implementation that caused link errors when structs are used as map keys.
**Forward Setter Generation** (`compiler/cpp/src/thrift/generate/t_cpp_generator.cc`):
- Parse `moveable_types=forward_setter` option
- Complex types (strings, containers, structs) → template setters with `std::forward<T_>`
- Primitive types → traditional const-ref setters
- Template implementations in `.tcc` file (auto-included in header)
- Legacy `moveable_types` behavior unchanged
**Compiler Unit Tests** (`compiler/cpp/tests/cpp/`):
- New `test_forward_setter.thrift` fixture
- Dedicated `t_cpp_generator_forward_setter_tests.cc` (91 assertions, 9 test cases)
- Verify `.tcc` generation and template implementations
**Integration Tests** (`test/cpp/src/`):
- `ForwardSetterTest.cpp` - validates lvalue/rvalue/temporary/literal setters with move semantics
- `PrivateOptionalTest.cpp` - SFINAE + static_assert verify optional fields are private
- `EnumClassTest.cpp` - type_traits + static_assert verify true enum class semantics
**CMakeLists.txt** (`test/cpp/`):
- Separate gen-cpp-{forward,private,enumclass} directories
**Makefile.am** (`test/cpp/`):
- Library targets for each option variant
- Proper `BUILT_SOURCES` dependencies
- Include path ordering: option-specific directory before standard `gen-cpp`
```cpp
// Generated with --gen cpp:moveable_types=forward_setter
struct TestStruct {
int32_t primitive_field;
std::string complex_field;
void __set_primitive_field(const int32_t val); // Traditional
template <typename T_>
void __set_complex_field(T_&& val); // Perfect forwarding
};
// In .tcc file:
template <typename T_>
void TestStruct::__set_complex_field(T_&& val) {
this->complex_field = ::std::forward<T_>(val);
__isset.complex_field = true;
}
```
- [ ] Did you create an [Apache Jira](https://issues.apache.org/jira/projects/THRIFT/issues/) ticket? ([Request account here](https://selfserve.apache.org/jira-account.html), not required for trivial changes)
- [ ] If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
- [x] Did you squash your changes to a single commit? (not required, but preferred)
- [x] Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: zsy056 <1074382+zsy056@users.noreply.github.com>
diff --git a/test/cpp/Makefile.am b/test/cpp/Makefile.am
index 11bf873..f2d07ec 100644
--- a/test/cpp/Makefile.am
+++ b/test/cpp/Makefile.am
@@ -22,7 +22,13 @@
gen-cpp/ThriftTest_types.cpp \
gen-cpp/ThriftTest_constants.cpp \
gen-cpp/SecondService.cpp \
- gen-cpp/Service.cpp
+ gen-cpp/Service.cpp \
+ gen-cpp-forward/ThriftTest_types.cpp \
+ gen-cpp-forward/ThriftTest_constants.cpp \
+ gen-cpp-private/ThriftTest_types.cpp \
+ gen-cpp-private/ThriftTest_constants.cpp \
+ gen-cpp-enumclass/ThriftTest_types.cpp \
+ gen-cpp-enumclass/ThriftTest_constants.cpp
noinst_LTLIBRARIES = libtestgencpp.la libstresstestgencpp.la
nodist_libtestgencpp_la_SOURCES = \
@@ -41,6 +47,40 @@
libtestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
+# Libraries for option-specific tests
+noinst_LTLIBRARIES += \
+ libforwardsettertestgencpp.la \
+ libprivateoptonaltestgencpp.la \
+ libenumclasstestgencpp.la
+
+nodist_libforwardsettertestgencpp_la_SOURCES = \
+ gen-cpp-forward/ThriftTest_types.cpp \
+ gen-cpp-forward/ThriftTest_types.h \
+ gen-cpp-forward/ThriftTest_types.tcc \
+ gen-cpp-forward/ThriftTest_constants.cpp \
+ gen-cpp-forward/ThriftTest_constants.h \
+ src/ThriftTest_extras.cpp
+
+libforwardsettertestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
+
+nodist_libprivateoptonaltestgencpp_la_SOURCES = \
+ gen-cpp-private/ThriftTest_types.cpp \
+ gen-cpp-private/ThriftTest_types.h \
+ gen-cpp-private/ThriftTest_constants.cpp \
+ gen-cpp-private/ThriftTest_constants.h \
+ src/ThriftTest_extras.cpp
+
+libprivateoptonaltestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
+
+nodist_libenumclasstestgencpp_la_SOURCES = \
+ gen-cpp-enumclass/ThriftTest_types.cpp \
+ gen-cpp-enumclass/ThriftTest_types.h \
+ gen-cpp-enumclass/ThriftTest_constants.cpp \
+ gen-cpp-enumclass/ThriftTest_constants.h \
+ src/ThriftTest_extras.cpp
+
+libenumclasstestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
+
nodist_libstresstestgencpp_la_SOURCES = \
gen-cpp/StressTest_types.h \
gen-cpp/Service.cpp \
@@ -54,7 +94,10 @@
TestServer \
TestClient \
StressTest \
- StressTestNonBlocking
+ StressTestNonBlocking \
+ ForwardSetterTest \
+ PrivateOptionalTest \
+ EnumClassTest
# we currently do not run the testsuite, stop c++ server issue
# TESTS = \
@@ -94,12 +137,52 @@
libstresstestgencpp.la \
$(top_builddir)/lib/cpp/libthriftnb.la \
-levent
+
+ForwardSetterTest_SOURCES = \
+ src/ForwardSetterTest.cpp
+
+ForwardSetterTest_CPPFLAGS = -Igen-cpp-forward $(AM_CPPFLAGS)
+ForwardSetterTest_LDADD = \
+ libforwardsettertestgencpp.la \
+ $(top_builddir)/lib/cpp/libthrift.la
+
+PrivateOptionalTest_SOURCES = \
+ src/PrivateOptionalTest.cpp
+
+PrivateOptionalTest_CPPFLAGS = -Igen-cpp-private $(AM_CPPFLAGS)
+PrivateOptionalTest_LDADD = \
+ libprivateoptonaltestgencpp.la \
+ $(top_builddir)/lib/cpp/libthrift.la
+
+EnumClassTest_SOURCES = \
+ src/EnumClassTest.cpp
+
+EnumClassTest_CPPFLAGS = -Igen-cpp-enumclass $(AM_CPPFLAGS)
+EnumClassTest_LDADD = \
+ libenumclasstestgencpp.la \
+ $(top_builddir)/lib/cpp/libthrift.la
+
#
# Common thrift code generation rules
#
gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/SecondService.cpp gen-cpp/SecondService.h gen-cpp/SecondService.tcc: $(top_srcdir)/test/ThriftTest.thrift $(THRIFT)
$(THRIFT) --gen cpp:templates,cob_style -r $<
+# Generate ThriftTest with forward_setter option
+gen-cpp-forward/ThriftTest_types.cpp gen-cpp-forward/ThriftTest_types.h gen-cpp-forward/ThriftTest_types.tcc gen-cpp-forward/ThriftTest_constants.cpp: $(top_srcdir)/test/ThriftTest.thrift $(THRIFT)
+ $(MKDIR_P) gen-cpp-forward
+ $(THRIFT) --gen cpp:moveable_types=forward_setter -out gen-cpp-forward $<
+
+# Generate ThriftTest with private_optional option
+gen-cpp-private/ThriftTest_types.cpp gen-cpp-private/ThriftTest_types.h gen-cpp-private/ThriftTest_constants.cpp: $(top_srcdir)/test/ThriftTest.thrift $(THRIFT)
+ $(MKDIR_P) gen-cpp-private
+ $(THRIFT) --gen cpp:private_optional -out gen-cpp-private $<
+
+# Generate ThriftTest with enum_class option
+gen-cpp-enumclass/ThriftTest_types.cpp gen-cpp-enumclass/ThriftTest_types.h gen-cpp-enumclass/ThriftTest_constants.cpp: $(top_srcdir)/test/ThriftTest.thrift $(THRIFT)
+ $(MKDIR_P) gen-cpp-enumclass
+ $(THRIFT) --gen cpp:pure_enums=enum_class -out gen-cpp-enumclass $<
+
gen-cpp/Service.cpp: $(top_srcdir)/test/StressTest.thrift $(THRIFT)
$(THRIFT) --gen cpp $<
@@ -111,7 +194,7 @@
AM_LDFLAGS = $(BOOST_LDFLAGS) $(LIBEVENT_LDFLAGS) $(ZLIB_LIBS)
clean-local:
- $(RM) -r gen-cpp/
+ $(RM) -r gen-cpp/ gen-cpp-forward/ gen-cpp-private/ gen-cpp-enumclass/
style-local:
$(CPPSTYLE_CMD)
@@ -123,4 +206,7 @@
src/TestClient.cpp \
src/TestServer.cpp \
src/StressTest.cpp \
- src/StressTestNonBlocking.cpp
+ src/StressTestNonBlocking.cpp \
+ src/ForwardSetterTest.cpp \
+ src/PrivateOptionalTest.cpp \
+ src/EnumClassTest.cpp