forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmemlink.cc
59 lines (51 loc) · 1.55 KB
/
cmemlink.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#include "cmemlink.h"
#include "ofstream.h"
#include "strmsize.h"
#include "ualgo.h"
namespace ustl {
/// \brief Attaches the object to pointer \p p of size \p n.
///
/// If \p p is nullptr and \p n is non-zero, bad_alloc is thrown and current
/// state remains unchanged.
///
void cmemlink::link (const void* p, size_type n)
{
if (!p && n)
throw bad_alloc (n);
unlink();
relink (p, n);
}
/// Writes the object to stream \p os
void cmemlink::write (ostream& os) const
{
const written_size_type sz (size());
assert (sz == size() && "No support for writing memblocks larger than 4G");
os << sz;
os.write (cdata(), sz);
os.align (stream_align_of (sz));
}
/// Writes the object to stream \p os
void cmemlink::text_write (ostringstream& os) const
{
os.write (begin(), readable_size());
}
/// Returns the number of bytes required to write this object to a stream.
cmemlink::size_type cmemlink::stream_size (void) const noexcept
{
const written_size_type sz (size());
return Align (stream_size_of (sz) + sz, stream_align_of(sz));
}
/// Writes the data to file \p "filename".
void cmemlink::write_file (const char* filename, int mode) const
{
fstream f;
f.exceptions (fstream::allbadbits);
f.open (filename, fstream::out | fstream::trunc, mode);
f.write (cdata(), readable_size());
f.close();
}
} // namespace ustl