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
f3530e05
Commit
f3530e05
authored
7 years ago
by
Federico
Browse files
Options
Downloads
Patches
Plain Diff
Incomplete implementation of serialization to std streams
parent
f0715e67
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/1_stream_known.cpp
+26
-0
26 additions, 0 deletions
examples/1_stream_known.cpp
examples/Makefile
+3
-1
3 additions, 1 deletion
examples/Makefile
tscpp.cpp
+37
-0
37 additions, 0 deletions
tscpp.cpp
tscpp.h
+86
-0
86 additions, 0 deletions
tscpp.h
with
152 additions
and
1 deletion
examples/1_stream_known.cpp
0 → 100644
+
26
−
0
View file @
f3530e05
#include
<iostream>
#include
<sstream>
#include
<cassert>
#include
<tscpp.h>
#include
"types.h"
using
namespace
std
;
using
namespace
tscpp
;
int
main
()
{
//Serialize to buffer
Point3d
p1
(
1
,
2
,
3
);
stringstream
ss
;
OutputArchive
oa
(
ss
);
oa
<<
p1
;
//Unserialize from buffer
Point3d
p2
;
InputArchive
ia
(
ss
);
ia
>>
p2
;
assert
(
p1
==
p2
);
cout
<<
"Test passed"
<<
endl
;
}
This diff is collapsed.
Click to expand it.
examples/Makefile
+
3
−
1
View file @
f3530e05
...
...
@@ -3,10 +3,12 @@ CXX = g++
CXXFLAGS
=
-std
=
c++11
-O2
-Wall
-I
..
all
:
$(
CXX
)
$(
CXXFLAGS
)
1_stream_known.cpp ../tscpp.cpp
-o
1_stream_known
$(
CXX
)
$(
CXXFLAGS
)
3_buffer_known.cpp ../tscpp.cpp
-o
3_buffer_known
$(
CXX
)
$(
CXXFLAGS
)
4_buffer_unknown.cpp ../tscpp.cpp
-o
4_buffer_unknown
./1_stream_known
./3_buffer_known
./4_buffer_unknown
clean
:
rm
-f
3_buffer_known 4_buffer_unknown
rm
-f
1_stream_known
3_buffer_known 4_buffer_unknown
This diff is collapsed.
Click to expand it.
tscpp.cpp
+
37
−
0
View file @
f3530e05
...
...
@@ -26,6 +26,7 @@
***************************************************************************/
#include
"tscpp.h"
#include
<memory>
#if defined(__GNUC__) && !defined(_MIOSIX)
#include
<cxxabi.h>
#endif
...
...
@@ -107,4 +108,40 @@ string demangle(const string& name)
#endif
}
void
OutputArchive
::
serializeImpl
(
const
char
*
name
,
const
void
*
data
,
int
size
)
{
int
nameSize
=
strlen
(
name
);
os
.
write
(
name
,
nameSize
+
1
);
os
.
write
(
reinterpret_cast
<
const
char
*>
(
data
),
size
);
}
void
InputArchive
::
unserializeImpl
(
const
char
*
name
,
void
*
data
,
int
size
)
{
auto
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
(
memcmp
(
unserializedName
.
get
(),
name
,
nameSize
+
1
))
{
is
.
seekg
(
pos
);
throw
2
;
//FIXME
}
//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
())
{
is
.
seekg
(
pos
);
throw
3
;
//FIXME
}
}
}
//namespace tscpp
This diff is collapsed.
Click to expand it.
tscpp.h
+
86
−
0
View file @
f3530e05
...
...
@@ -29,6 +29,8 @@
#include
<type_traits>
#include
<functional>
#include
<ostream>
#include
<istream>
#include
<cstring>
#include
<string>
#include
<map>
...
...
@@ -186,4 +188,88 @@ std::string peekTypeName(const void *buffer, int bufSize);
*/
std
::
string
demangle
(
const
std
::
string
&
name
);
/**
* The output archive.
* This class allows to serialize objects to any ostream using the familiar
* << syntax
*/
class
OutputArchive
{
public:
/**
* Constructor
* \param os ostream where srialized types will be written
*/
OutputArchive
(
std
::
ostream
&
os
)
:
os
(
os
)
{}
/**
* \internal
*/
void
serializeImpl
(
const
char
*
name
,
const
void
*
data
,
int
size
);
private
:
OutputArchive
(
const
OutputArchive
&
)
=
delete
;
OutputArchive
&
operator
=
(
const
OutputArchive
&
)
=
delete
;
std
::
ostream
&
os
;
};
/**
* Serialize a type
* \param oa archive where the type will be serialized
* \param t type to serialize
*/
template
<
typename
T
>
OutputArchive
&
operator
<<
(
OutputArchive
&
oa
,
const
T
&
t
)
{
#ifndef _MIOSIX
static_assert
(
std
::
is_trivially_copyable
<
T
>::
value
,
"Type is not trivially copyable"
);
#endif
oa
.
serializeImpl
(
typeid
(
t
).
name
(),
&
t
,
sizeof
(
t
));
return
oa
;
}
/**
* The input archive.
* This class allows to unserialize types from a stream, as long as you know
* what types have been serialized in which order. Otherwise have a look at
* UnknownInputArchive.
* To unserialize, use the familiar >> syntax.
*/
class
InputArchive
{
public:
/**
* Constructor
* \param os ostream where srialized types will be written
*/
InputArchive
(
std
::
istream
&
is
)
:
is
(
is
)
{}
/**
* \internal
*/
void
unserializeImpl
(
const
char
*
name
,
void
*
data
,
int
size
);
private
:
InputArchive
(
const
InputArchive
&
)
=
delete
;
InputArchive
&
operator
=
(
const
InputArchive
&
)
=
delete
;
std
::
istream
&
is
;
};
/**
* Unserialize a type
* \param ia archive where the type has been serialized
* \param t type to unserialize
*/
template
<
typename
T
>
InputArchive
&
operator
>>
(
InputArchive
&
ia
,
T
&
t
)
{
#ifndef _MIOSIX
static_assert
(
std
::
is_trivially_copyable
<
T
>::
value
,
"Type is not trivially copyable"
);
#endif
ia
.
unserializeImpl
(
typeid
(
t
).
name
(),
&
t
,
sizeof
(
t
));
return
ia
;
}
}
// 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