NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
io.hpp
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013-2019 Regents of the University of California.
4
*
5
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6
*
7
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
8
* terms of the GNU Lesser General Public License as published by the Free Software
9
* Foundation, either version 3 of the License, or (at your option) any later version.
10
*
11
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14
*
15
* You should have received copies of the GNU General Public License and GNU Lesser
16
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17
* <http://www.gnu.org/licenses/>.
18
*
19
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20
*/
21
22
#ifndef NDN_UTIL_IO_HPP
23
#define NDN_UTIL_IO_HPP
24
25
#include "
ndn-cxx/encoding/block.hpp
"
26
#include "
ndn-cxx/util/concepts.hpp
"
27
28
#include <fstream>
29
30
namespace
ndn
{
31
namespace
io {
32
33
class
Error
:
public
std::runtime_error
34
{
35
public
:
36
using
std::runtime_error::runtime_error;
37
};
38
41
enum
IoEncoding
{
44
NO_ENCODING
,
45
51
BASE64
,
52
57
HEX
,
58
};
59
60
namespace
detail {
61
62
template
<
typename
T>
63
static
void
64
checkInnerError
(
typename
T::Error*)
65
{
66
static_assert(std::is_convertible<typename T::Error*, tlv::Error*>::value,
67
"T::Error, if defined, must be a subclass of ndn::tlv::Error"
);
68
}
69
70
template
<
typename
T>
71
static
void
72
checkInnerError
(...)
73
{
74
// T::Error is not defined
75
}
76
77
}
// namespace detail
78
84
shared_ptr<Buffer>
85
loadBuffer
(std::istream& is,
IoEncoding
encoding =
BASE64
);
86
90
optional<Block>
91
loadBlock
(std::istream& is,
IoEncoding
encoding =
BASE64
);
92
98
template
<
typename
T>
99
shared_ptr<T>
100
load
(std::istream& is,
IoEncoding
encoding =
BASE64
)
101
{
102
BOOST_CONCEPT_ASSERT((
WireDecodable<T>
));
103
detail::checkInnerError<T>(
nullptr
);
104
105
auto
block =
loadBlock
(is, encoding);
106
if
(!block) {
107
return
nullptr
;
108
}
109
110
try
{
111
return
make_shared<T>(*block);
112
}
113
catch
(
const
tlv::Error
&) {
114
return
nullptr
;
115
}
116
}
117
123
template
<
typename
T>
124
shared_ptr<T>
125
load
(
const
std::string& filename,
IoEncoding
encoding =
BASE64
)
126
{
127
std::ifstream is(filename);
128
return
load<T>(is, encoding);
129
}
130
135
void
136
saveBuffer
(
const
uint8_t*
buf
,
size_t
size, std::ostream& os,
IoEncoding
encoding =
BASE64
);
137
142
void
143
saveBlock
(
const
Block
& block, std::ostream& os,
IoEncoding
encoding =
BASE64
);
144
151
template
<
typename
T>
152
void
153
save
(
const
T& obj, std::ostream& os,
IoEncoding
encoding =
BASE64
)
154
{
155
BOOST_CONCEPT_ASSERT((
WireEncodable<T>
));
156
detail::checkInnerError<T>(
nullptr
);
157
158
Block
block;
159
try
{
160
block = obj.wireEncode();
161
}
162
catch
(
const
tlv::Error
&) {
163
NDN_THROW_NESTED
(
Error
(
"Encode error during save"
));
164
}
165
166
saveBlock
(block, os, encoding);
167
}
168
175
template
<
typename
T>
176
void
177
save
(
const
T& obj,
const
std::string& filename,
IoEncoding
encoding =
BASE64
)
178
{
179
std::ofstream os(filename);
180
save
(obj, os, encoding);
181
}
182
183
}
// namespace io
184
}
// namespace ndn
185
186
#endif // NDN_UTIL_IO_HPP
buf
const uint8_t * buf
Definition:
verification-helpers.cpp:47
ndn::io::NO_ENCODING
@ NO_ENCODING
Raw binary, without encoding.
Definition:
io.hpp:44
ndn::io::detail::checkInnerError
static void checkInnerError(typename T::Error *)
Definition:
io.hpp:64
block.hpp
ndn::io::BASE64
@ BASE64
Base64 encoding.
Definition:
io.hpp:51
concepts.hpp
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition:
exception.hpp:71
ndn::io::loadBlock
optional< Block > loadBlock(std::istream &is, IoEncoding encoding)
Reads a TLV block from a stream.
Definition:
io.cpp:63
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition:
concepts.hpp:81
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition:
concepts.hpp:45
ndn::io::loadBuffer
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition:
io.cpp:37
ndn::io::IoEncoding
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition:
io.hpp:41
ndn::io::saveBuffer
void saveBuffer(const uint8_t *buf, size_t size, std::ostream &os, IoEncoding encoding)
Writes a byte buffer to a stream.
Definition:
io.cpp:77
ndn::io::load
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE64)
Reads a TLV element from a stream.
Definition:
io.hpp:100
ndn::io::Error
Definition:
io.hpp:34
ndn::Block
Represents a TLV element of NDN packet format.
Definition:
block.hpp:43
ndn::io::HEX
@ HEX
Hexadecimal encoding.
Definition:
io.hpp:57
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition:
tlv.hpp:53
ndn::io::saveBlock
void saveBlock(const Block &block, std::ostream &os, IoEncoding encoding)
Writes a TLV block to a stream.
Definition:
io.cpp:102
ndn::io::save
void save(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
Writes a TLV element to a stream.
Definition:
io.hpp:153
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
ndnSIM
ndn-cxx
ndn-cxx
util
io.hpp
Generated on Mon Jun 1 2020 22:32:15 for ndnSIM by
1.8.18