From f03eb69e0107634b408f1f5367d3b61215f51d04 Mon Sep 17 00:00:00 2001 From: Felipe Garay Date: Thu, 4 Jan 2024 15:18:11 -0800 Subject: [PATCH] odb: refactoring out serialization code for std::variant - Makes the code in dbScanPin more simple - std::variant now is available to be use for serialization by others - setting template recursion limit to 16 for std::tuple and std::variant Signed-off-by: Felipe Garay --- src/odb/include/odb/dbStream.h | 51 +++++++++++++++++++ .../codeGenerator/schema/scan/dbScanPin.json | 5 ++ src/odb/src/db/dbScanPin.cpp | 24 +-------- src/odb/src/db/dbScanPin.h | 2 - 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/odb/include/odb/dbStream.h b/src/odb/include/odb/dbStream.h index c14a6517838..ce215a080d1 100644 --- a/src/odb/include/odb/dbStream.h +++ b/src/odb/include/odb/dbStream.h @@ -39,6 +39,7 @@ #include #include #include +#include #include "ZException.h" #include "dbObject.h" @@ -50,6 +51,8 @@ namespace odb { class _dbDatabase; +inline constexpr size_t kTemplateRecursionLimit = 16; + class dbOStream { _dbDatabase* _db; @@ -173,6 +176,9 @@ class dbOStream template constexpr dbOStream& operator<<(const std::tuple& tup) { + static_assert(I <= kTemplateRecursionLimit, + "OpenROAD disallows of std::tuple larger than 16 " + "elements. You should look into alternate solutions"); if constexpr (I == sizeof...(Ts)) { return *this; } else { @@ -222,6 +228,23 @@ class dbOStream return *this; } + template + dbOStream& operator<<(const std::variant& v) + { + static_assert(I <= kTemplateRecursionLimit, + "OpenROAD disallows of std::variants larger than 16 " + "elements. You should look into alternate solutions"); + if constexpr (I == sizeof...(Ts)) { + return *this; + } else { + if (I == v.index()) { + *this << (uint32_t) v.index(); + *this << std::get(v); + } + return ((*this).operator<< (v)); + } + } + double lefarea(int value) { return ((double) value * _lef_area_factor); } double lefdist(int value) { return ((double) value * _lef_dist_factor); } @@ -381,6 +404,9 @@ class dbIStream template constexpr dbIStream& operator>>(std::tuple& tup) { + static_assert(I <= kTemplateRecursionLimit, + "OpenROAD disallows of std::tuple larger than 16 " + "elements. You should look into alternate solutions"); if constexpr (I == sizeof...(Ts)) { return *this; } else { @@ -398,9 +424,34 @@ class dbIStream return *this; } + template + dbIStream& operator>>(std::variant& v) + { + uint32_t index = 0; + *this >> index; + return variantHelper(index, v); + } + double lefarea(int value) { return ((double) value * _lef_area_factor); } double lefdist(int value) { return ((double) value * _lef_dist_factor); } + + private: + template + dbIStream& variantHelper(uint32_t index, std::variant& v) + { + static_assert(I <= kTemplateRecursionLimit, + "OpenROAD disallows of std::variants larger than 16 " + "elements. You should look into alternate solutions"); + if constexpr (I == sizeof...(Ts)) { + return *this; + } else { + if (I == index) { + *this >> std::get(v); + } + return ((*this).operator>>(v)); + } + } }; } // namespace odb diff --git a/src/odb/src/codeGenerator/schema/scan/dbScanPin.json b/src/odb/src/codeGenerator/schema/scan/dbScanPin.json index 0c2c6a85c28..2e2aec5fb4e 100644 --- a/src/odb/src/codeGenerator/schema/scan/dbScanPin.json +++ b/src/odb/src/codeGenerator/schema/scan/dbScanPin.json @@ -2,6 +2,11 @@ "name": "dbScanPin", "type": "dbObject", "fields": [ + { + "name": "pin_", + "type": "std::variant, dbId<_dbITerm>>", + "flags": ["private", "no-template"] + } ], "h_includes": [ "dbBTerm.h", diff --git a/src/odb/src/db/dbScanPin.cpp b/src/odb/src/db/dbScanPin.cpp index 09e85f5036a..51380bd4896 100644 --- a/src/odb/src/db/dbScanPin.cpp +++ b/src/odb/src/db/dbScanPin.cpp @@ -111,33 +111,13 @@ _dbScanPin::_dbScanPin(_dbDatabase* db, const _dbScanPin& r) dbIStream& operator>>(dbIStream& stream, _dbScanPin& obj) { - // User Code Begin >> - int index = 0; - stream >> index; - if (index == 0) { - dbId<_dbBTerm> bterm; - stream >> bterm; - obj.pin_ = bterm; - } else if (index == 1) { - dbId<_dbITerm> iterm; - stream >> iterm; - obj.pin_ = iterm; - } - // User Code End >> + stream >> obj.pin_; return stream; } dbOStream& operator<<(dbOStream& stream, const _dbScanPin& obj) { - // User Code Begin << - int index = obj.pin_.index(); - stream << index; - if (index == 0) { - stream << std::get<0>(obj.pin_); - } else if (index == 1) { - stream << std::get<1>(obj.pin_); - } - // User Code End << + stream << obj.pin_; return stream; } diff --git a/src/odb/src/db/dbScanPin.h b/src/odb/src/db/dbScanPin.h index 0315f757e06..84cf20d5b77 100644 --- a/src/odb/src/db/dbScanPin.h +++ b/src/odb/src/db/dbScanPin.h @@ -64,9 +64,7 @@ class _dbScanPin : public _dbObject const _dbScanPin& rhs) const; void out(dbDiff& diff, char side, const char* field) const; - // User Code Begin Fields std::variant, dbId<_dbITerm>> pin_; - // User Code End Fields }; dbIStream& operator>>(dbIStream& stream, _dbScanPin& obj); dbOStream& operator<<(dbOStream& stream, const _dbScanPin& obj);