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/src/EnumClassTest.cpp b/test/cpp/src/EnumClassTest.cpp
new file mode 100644
index 0000000..3b21063
--- /dev/null
+++ b/test/cpp/src/EnumClassTest.cpp
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Test file to verify that pure_enums=enum_class generated code compiles and works correctly.
+ * This exercises the enum_class option using ThriftTest types.
+ */
+
+#include <iostream>
+#include <cassert>
+#include <type_traits>
+
+// Include generated thrift types with enum_class option
+#include "ThriftTest_types.h"
+
+using namespace thrift::test;
+
+int main() {
+ std::cout << "Testing pure_enums=enum_class with ThriftTest types..." << std::endl;
+
+ // Compile-time verification that Numberz is an enum class
+ static_assert(std::is_enum<Numberz>::value, "Numberz should be an enum type");
+ // enum class doesn't implicitly convert to int, which is a key characteristic
+ static_assert(!std::is_convertible<Numberz, int>::value,
+ "Numberz should be enum class (not implicitly convertible to int)");
+ std::cout << " ✓ Compile-time verification: Numberz is enum class" << std::endl;
+
+ // Test 1: Verify enum class can be used with scoped names
+ {
+ Numberz num = Numberz::ONE;
+ assert(static_cast<int>(num) == 1);
+ std::cout << " ✓ Enum class scoped access works (Numberz::ONE)" << std::endl;
+ }
+
+ // Test 2: Verify different enum values
+ {
+ Numberz two = Numberz::TWO;
+ Numberz five = Numberz::FIVE;
+ assert(static_cast<int>(two) == 2);
+ assert(static_cast<int>(five) == 5);
+ std::cout << " ✓ Multiple enum class values work" << std::endl;
+ }
+
+ // Test 3: Verify enum class comparison
+ {
+ Numberz a = Numberz::THREE;
+ Numberz b = Numberz::THREE;
+ Numberz c = Numberz::FIVE;
+ assert(a == b);
+ assert(a != c);
+ std::cout << " ✓ Enum class comparison works" << std::endl;
+ }
+
+ // Test 4: Verify enum class in switch statement
+ {
+ Numberz num = Numberz::EIGHT;
+ bool found = false;
+ switch(num) {
+ case Numberz::ONE:
+ break;
+ case Numberz::TWO:
+ break;
+ case Numberz::EIGHT:
+ found = true;
+ break;
+ default:
+ break;
+ }
+ assert(found);
+ std::cout << " ✓ Enum class in switch statements works" << std::endl;
+ }
+
+ std::cout << "\n✅ All pure_enums=enum_class tests passed!" << std::endl;
+ std::cout << " Verified at compile-time: enum class properties enforced" << std::endl;
+ return 0;
+}