Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
Trivial serialization for C++ - tscpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Avionics
Software Development
Third Party
Trivial serialization for C++ - tscpp
Commits
6ae53573
Commit
6ae53573
authored
7 years ago
by
Federico
Browse files
Options
Downloads
Patches
Plain Diff
Added exceptions to InputArchive
parent
f3530e05
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tscpp.cpp
+15
-14
15 additions, 14 deletions
tscpp.cpp
tscpp.h
+52
-2
52 additions, 2 deletions
tscpp.h
with
67 additions
and
16 deletions
tscpp.cpp
+
15
−
14
View file @
6ae53573
...
...
@@ -90,7 +90,7 @@ string peekTypeName(const void *buffer, int bufSize)
{
const
char
*
buf
=
reinterpret_cast
<
const
char
*>
(
buffer
);
int
nameSize
=
strnlen
(
buf
,
bufSize
);
if
(
nameSize
>=
bufSize
)
return
"
<corrupted>
"
;
if
(
nameSize
>=
bufSize
)
return
""
;
return
buf
;
}
...
...
@@ -117,30 +117,31 @@ void OutputArchive::serializeImpl(const char *name, const void *data, int size)
void
InputArchive
::
unserializeImpl
(
const
char
*
name
,
void
*
data
,
int
size
)
{
auto
pos
=
is
.
tellg
();
pos
=
is
.
tellg
();
int
nameSize
=
strlen
(
name
);
unique_ptr
<
char
[]
>
unserializedName
(
new
char
[
nameSize
+
1
]);
is
.
read
(
unserializedName
.
get
(),
nameSize
+
1
);
if
(
is
.
eof
())
{
is
.
seekg
(
pos
);
throw
1
;
//FIXME
}
if
(
is
.
eof
())
errorImpl
(
"eof"
);
if
(
memcmp
(
unserializedName
.
get
(),
name
,
nameSize
+
1
))
{
is
.
seekg
(
pos
);
throw
2
;
//FIXME
}
errorImpl
(
"wrong type found"
,
true
);
//NOTE: we are writing on top of a constructed type without calling its
//destructor. However, since it is trivially copyable, we at least aren't
//overwriting pointers to allocated memory.
is
.
read
(
reinterpret_cast
<
char
*>
(
data
),
size
);
if
(
is
.
eof
())
if
(
is
.
eof
())
errorImpl
(
"eof"
);
}
void
InputArchive
::
errorImpl
(
const
string
&
errorStr
,
bool
printName
)
{
is
.
seekg
(
pos
);
throw
3
;
//FIXME
if
(
printName
==
false
)
throw
TscppException
(
errorStr
);
else
{
string
type
;
getline
(
is
,
type
,
'\0'
);
is
.
seekg
(
pos
);
throw
TscppException
(
errorStr
,
type
);
}
}
...
...
This diff is collapsed.
Click to expand it.
tscpp.h
+
52
−
2
View file @
6ae53573
...
...
@@ -29,6 +29,7 @@
#include
<type_traits>
#include
<functional>
#include
<stdexcept>
#include
<ostream>
#include
<istream>
#include
<cstring>
...
...
@@ -174,8 +175,8 @@ int unserializeUnknown(const TypePool& tp, const void *buffer, int bufSize);
* \endcode
* \param buffer pointer to buffer where the serialized type is
* \param bufSize buffer size
* \return the serialized type name, or "
<corrupted>
" if the buffer does not
*
contain a type
name
* \return the serialized type name, or "" if the buffer does not
contain a type
* name
*/
std
::
string
peekTypeName
(
const
void
*
buffer
,
int
bufSize
);
...
...
@@ -250,17 +251,25 @@ public:
*/
void
unserializeImpl
(
const
char
*
name
,
void
*
data
,
int
size
);
/**
* \internal
*/
void
errorImpl
(
const
std
::
string
&
errorStr
,
bool
printName
=
false
);
private
:
InputArchive
(
const
InputArchive
&
)
=
delete
;
InputArchive
&
operator
=
(
const
InputArchive
&
)
=
delete
;
std
::
istream
&
is
;
std
::
streampos
pos
;
};
/**
* Unserialize a type
* \param ia archive where the type has been serialized
* \param t type to unserialize
* \throws TscppException if the type found in the stream is not the one
* expected, or if the stream eof is found
*/
template
<
typename
T
>
InputArchive
&
operator
>>
(
InputArchive
&
ia
,
T
&
t
)
...
...
@@ -272,4 +281,45 @@ InputArchive& operator>>(InputArchive& ia, T& t)
return
ia
;
}
/**
* Exception class thrown by the input archives
*/
class
TscppException
:
public
std
::
runtime_error
{
public:
/**
* \internal
*/
TscppException
(
const
std
::
string
&
what
)
:
runtime_error
(
what
)
{}
/**
* \internal
*/
TscppException
(
const
std
::
string
&
what
,
const
std
::
string
&
t
)
:
runtime_error
(
what
),
t
(
t
)
{}
/**
* If the exception is thrown because an unknown/unexpected type has been
* found in the input stream, this member function allows to access the
* mangled type name.
* It is useful to print an error message with the name of the type in the
* stream
* \code
* InputArchive ia(is);
* Foo f;
* try {
* ia>>f;
* } catch(TscppException& ex) {
* if(ex.type().empty()==false)
* cerr<<"While unserializing Foo, "<<demangle(ex.type())<<" was found\n";
* }
* \endcode
* \return the serialized type name, or "" if eof was found
*/
std
::
string
type
()
const
{
return
t
;
}
private
:
std
::
string
t
;
};
}
// namespace tscpp
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment