Browse Source

Allow to handle Y_SAVELOAD_DEFINE() without args

yurial 1 year ago
parent
commit
74b9f490cb
2 changed files with 37 additions and 16 deletions
  1. 24 16
      util/ysaveload.h
  2. 13 0
      util/ysaveload_ut.cpp

+ 24 - 16
util/ysaveload.h

@@ -726,24 +726,32 @@ static inline void LoadMany(S* s, Ts&... t) {
     ApplyToMany([&](auto& v) { Load(s, v); }, t...);
 }
 
-#define Y_SAVELOAD_DEFINE(...)                 \
-    inline void Save(IOutputStream* s) const { \
-        ::SaveMany(s, __VA_ARGS__);            \
-    }                                          \
-                                               \
-    inline void Load(IInputStream* s) {        \
-        ::LoadMany(s, __VA_ARGS__);            \
-    }                                          \
+#define Y_SAVELOAD_DEFINE(...)                                    \
+    inline void Save(IOutputStream* s) const {                    \
+        [s](auto&&... args) {                                     \
+            ::SaveMany(s, std::forward<decltype(args)>(args)...); \
+        }(__VA_ARGS__);                                           \
+    }                                                             \
+                                                                  \
+    inline void Load(IInputStream* s) {                           \
+        [s](auto&&... args) {                                     \
+            ::LoadMany(s, std::forward<decltype(args)>(args)...); \
+        }(__VA_ARGS__);                                           \
+    }                                                             \
     Y_SEMICOLON_GUARD
 
-#define Y_SAVELOAD_DEFINE_OVERRIDE(...)          \
-    void Save(IOutputStream* s) const override { \
-        ::SaveMany(s, __VA_ARGS__);              \
-    }                                            \
-                                                 \
-    void Load(IInputStream* s) override {        \
-        ::LoadMany(s, __VA_ARGS__);              \
-    }                                            \
+#define Y_SAVELOAD_DEFINE_OVERRIDE(...)                           \
+    void Save(IOutputStream* s) const override {                  \
+        [s](auto&&... args) {                                     \
+            ::SaveMany(s, std::forward<decltype(args)>(args)...); \
+        }(__VA_ARGS__);                                           \
+    }                                                             \
+                                                                  \
+    void Load(IInputStream* s) override {                         \
+        [s](auto&&... args) {                                     \
+            ::LoadMany(s, std::forward<decltype(args)>(args)...); \
+        }(__VA_ARGS__);                                           \
+    }                                                             \
     Y_SEMICOLON_GUARD
 
 template <class T>

+ 13 - 0
util/ysaveload_ut.cpp

@@ -22,6 +22,7 @@ static inline char* AllocateFromPool(TMemoryPool& pool, size_t len) {
 class TSaveLoadTest: public TTestBase {
     UNIT_TEST_SUITE(TSaveLoadTest);
     UNIT_TEST(TestSaveLoad)
+    UNIT_TEST(TestSaveLoadEmptyStruct)
     UNIT_TEST(TestNewStyle)
     UNIT_TEST(TestNewNewStyle)
     UNIT_TEST(TestList)
@@ -61,6 +62,10 @@ class TSaveLoadTest: public TTestBase {
         Y_SAVELOAD_DEFINE(Str, Int);
     };
 
+    struct TNewNewStyleEmptyHelper {
+        Y_SAVELOAD_DEFINE();
+    };
+
 private:
     inline void TestNewNewStyle() {
         TString ss;
@@ -375,6 +380,14 @@ private:
         }
     }
 
+    inline void TestSaveLoadEmptyStruct() {
+        TBufferStream S_;
+        TNewNewStyleEmptyHelper h;
+
+        Save(&S_, h);
+        Load(&S_, h);
+    }
+
     void TestList() {
         TBufferStream s;