mirror of https://github.com/KLayout/klayout.git
WIP: converters for protocol buffers
This commit is contained in:
parent
7d3b3dcb14
commit
edfac96db2
|
|
@ -863,7 +863,7 @@ private:
|
||||||
template <class T>
|
template <class T>
|
||||||
void write (tl::ProtocolBufferWriterBase &writer, int tag, const T &v) const
|
void write (tl::ProtocolBufferWriterBase &writer, int tag, const T &v) const
|
||||||
{
|
{
|
||||||
writer.write (tag, m_c.to_string (v));
|
writer.write (tag, m_c.pb_encode (v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// read incarnations
|
// read incarnations
|
||||||
|
|
@ -940,11 +940,11 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
void read (tl::ProtocolBufferReaderBase &reader, const T &v) const
|
void read (tl::ProtocolBufferReaderBase &reader, T &v) const
|
||||||
{
|
{
|
||||||
std::string vv;
|
typename Converter::pb_type vv;
|
||||||
reader.read (vv);
|
reader.read (vv);
|
||||||
m_c.from_string (vv, v);
|
m_c.pb_decode (vv, v);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1671,12 +1671,14 @@ pb_make_element_with_parent_ref (Iter (Parent::*begin) () const, Iter (Parent::*
|
||||||
template <class Value>
|
template <class Value>
|
||||||
struct PBStdConverter
|
struct PBStdConverter
|
||||||
{
|
{
|
||||||
std::string to_string (const Value &v) const
|
typedef std::string pb_type;
|
||||||
|
|
||||||
|
pb_type pb_encode (const Value &v) const
|
||||||
{
|
{
|
||||||
return tl::to_string (v);
|
return tl::to_string (v);
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_string (const std::string &s, Value &v) const
|
void pb_decode (const pb_type &s, Value &v) const
|
||||||
{
|
{
|
||||||
tl::from_string (s, v);
|
tl::from_string (s, v);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -359,3 +359,103 @@ TEST (100_BasicStruct)
|
||||||
EXPECT_EQ (error, "");
|
EXPECT_EQ (error, "");
|
||||||
EXPECT_EQ (root == Root (), true);
|
EXPECT_EQ (root == Root (), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TestClass
|
||||||
|
{
|
||||||
|
enum enum_type { A, B, C };
|
||||||
|
|
||||||
|
TestClass () : e (A) { }
|
||||||
|
enum_type e;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TestClassEnumConverter
|
||||||
|
{
|
||||||
|
typedef uint32_t pb_type;
|
||||||
|
|
||||||
|
pb_type pb_encode (TestClass::enum_type e) const
|
||||||
|
{
|
||||||
|
switch (e) {
|
||||||
|
case TestClass::A:
|
||||||
|
return 17;
|
||||||
|
case TestClass::B:
|
||||||
|
return 18;
|
||||||
|
case TestClass::C:
|
||||||
|
return 19;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pb_decode (uint32_t value, TestClass::enum_type &e) const
|
||||||
|
{
|
||||||
|
switch (value) {
|
||||||
|
case 17:
|
||||||
|
e = TestClass::A;
|
||||||
|
break;
|
||||||
|
case 18:
|
||||||
|
e = TestClass::B;
|
||||||
|
break;
|
||||||
|
case 19:
|
||||||
|
e = TestClass::C;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
e = TestClass::enum_type (0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST (101_Converter)
|
||||||
|
{
|
||||||
|
TestClass tc;
|
||||||
|
|
||||||
|
tl::PBStruct<TestClass> structure ("pbtest-tc", 1,
|
||||||
|
tl::pb_make_member (&TestClass::e, 2, TestClassEnumConverter ())
|
||||||
|
);
|
||||||
|
|
||||||
|
tc.e = TestClass::A;
|
||||||
|
std::string fn = tl::combine_path (tl::testtmp (), "pb_101a.pb");
|
||||||
|
|
||||||
|
{
|
||||||
|
tl::OutputStream os (fn);
|
||||||
|
tl::ProtocolBufferWriter writer (os);
|
||||||
|
structure.write (writer, tc);
|
||||||
|
}
|
||||||
|
|
||||||
|
tc = TestClass ();
|
||||||
|
|
||||||
|
std::string error;
|
||||||
|
try {
|
||||||
|
tl::InputStream is (fn);
|
||||||
|
tl::ProtocolBufferReader reader (is);
|
||||||
|
structure.parse (reader, tc);
|
||||||
|
} catch (tl::Exception &ex) {
|
||||||
|
error = ex.msg ();
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ (tc.e, TestClass::A);
|
||||||
|
|
||||||
|
tc.e = TestClass::B;
|
||||||
|
|
||||||
|
fn = tl::combine_path (tl::testtmp (), "pb_101b.pb");
|
||||||
|
|
||||||
|
{
|
||||||
|
tl::OutputStream os (fn);
|
||||||
|
tl::ProtocolBufferWriter writer (os);
|
||||||
|
structure.write (writer, tc);
|
||||||
|
}
|
||||||
|
|
||||||
|
tc = TestClass ();
|
||||||
|
|
||||||
|
error.clear ();
|
||||||
|
try {
|
||||||
|
tl::InputStream is (fn);
|
||||||
|
tl::ProtocolBufferReader reader (is);
|
||||||
|
structure.parse (reader, tc);
|
||||||
|
} catch (tl::Exception &ex) {
|
||||||
|
error = ex.msg ();
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ (tc.e, TestClass::B);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue