Skip to content

Commit

Permalink
odb: refactoring out serialization code for std::variant
Browse files Browse the repository at this point in the history
- 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 <fgaray@google.com>
  • Loading branch information
fgaray committed Jan 5, 2024
1 parent 51b02b4 commit f03eb69
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
51 changes: 51 additions & 0 deletions src/odb/include/odb/dbStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ostream>
#include <string>
#include <unordered_map>
#include <variant>

#include "ZException.h"
#include "dbObject.h"
Expand All @@ -50,6 +51,8 @@ namespace odb {

class _dbDatabase;

inline constexpr size_t kTemplateRecursionLimit = 16;

class dbOStream
{
_dbDatabase* _db;
Expand Down Expand Up @@ -173,6 +176,9 @@ class dbOStream
template <size_t I = 0, typename... Ts>
constexpr dbOStream& operator<<(const std::tuple<Ts...>& 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 {
Expand Down Expand Up @@ -222,6 +228,23 @@ class dbOStream
return *this;
}

template <uint32_t I = 0, typename... Ts>
dbOStream& operator<<(const std::variant<Ts...>& 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<I>(v);
}
return ((*this).operator<< <I + 1>(v));
}
}

double lefarea(int value) { return ((double) value * _lef_area_factor); }

double lefdist(int value) { return ((double) value * _lef_dist_factor); }
Expand Down Expand Up @@ -381,6 +404,9 @@ class dbIStream
template <size_t I = 0, typename... Ts>
constexpr dbIStream& operator>>(std::tuple<Ts...>& 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 {
Expand All @@ -398,9 +424,34 @@ class dbIStream
return *this;
}

template <uint32_t I = 0, typename... Ts>
dbIStream& operator>>(std::variant<Ts...>& 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 <uint32_t I = 0, typename... Ts>
dbIStream& variantHelper(uint32_t index, std::variant<Ts...>& 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<I>(v);
}
return ((*this).operator>><I + 1>(v));
}
}
};

} // namespace odb
5 changes: 5 additions & 0 deletions src/odb/src/codeGenerator/schema/scan/dbScanPin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "dbScanPin",
"type": "dbObject",
"fields": [
{
"name": "pin_",
"type": "std::variant<dbId<_dbBTerm>, dbId<_dbITerm>>",
"flags": ["private", "no-template"]
}
],
"h_includes": [
"dbBTerm.h",
Expand Down
24 changes: 2 additions & 22 deletions src/odb/src/db/dbScanPin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions src/odb/src/db/dbScanPin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<_dbBTerm>, dbId<_dbITerm>> pin_;
// User Code End Fields
};
dbIStream& operator>>(dbIStream& stream, _dbScanPin& obj);
dbOStream& operator<<(dbOStream& stream, const _dbScanPin& obj);
Expand Down

0 comments on commit f03eb69

Please sign in to comment.