ndn-cxx 0.9.0-42-g8cff8f06
Loading...
Searching...
No Matches
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-2026 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_CXX_UTIL_IO_HPP
23#define NDN_CXX_UTIL_IO_HPP
24
27
28#include <fstream>
29
30namespace ndn::io {
31
32class Error : public std::runtime_error
33{
34public:
35 using std::runtime_error::runtime_error;
36};
37
62
63namespace detail {
64
65template<typename T>
66constexpr void
67checkNestedError(typename T::Error*)
68{
69 static_assert(std::is_convertible_v<typename T::Error*, tlv::Error*>,
70 "T::Error, if defined, must be a subclass of ndn::tlv::Error");
71}
72
73template<typename T>
74constexpr void
76{
77 // T::Error is not defined
78}
79
80} // namespace detail
81
88shared_ptr<Buffer>
89loadBuffer(std::istream& is, IoEncoding encoding = BASE64);
90
98template<typename T>
99T
100loadTlv(std::istream& is, IoEncoding encoding = BASE64)
101{
102 BOOST_CONCEPT_ASSERT((WireDecodable<T>));
103
104 auto buf = loadBuffer(is, encoding);
105 try {
106 return T(Block(buf));
107 }
108 catch (const std::exception& e) {
109 NDN_THROW_NESTED(Error("Decode error during load: "s + e.what()));
110 }
111}
112
120template<typename T>
121[[deprecated("use loadTlv")]]
122shared_ptr<T>
123load(std::istream& is, IoEncoding encoding = BASE64)
124{
125 BOOST_CONCEPT_ASSERT((WireDecodable<T>));
126 detail::checkNestedError<T>(nullptr);
127
128 Block block;
129 try {
130 block = Block(loadBuffer(is, encoding));
131 }
132 catch (const std::invalid_argument&) {
133 return nullptr;
134 }
135 catch (const std::runtime_error&) {
136 return nullptr;
137 }
138
139 try {
140 return make_shared<T>(block);
141 }
142 catch (const tlv::Error&) {
143 return nullptr;
144 }
145}
146
154template<typename T>
155[[deprecated("use loadTlv")]]
156shared_ptr<T>
157load(const std::string& filename, IoEncoding encoding = BASE64)
158{
159 std::ifstream is(filename);
160 return load<T>(is, encoding);
161}
162
168void
169saveBuffer(span<const uint8_t> buf, std::ostream& os, IoEncoding encoding = BASE64);
170
177template<typename T>
178void
179saveTlv(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
180{
181 BOOST_CONCEPT_ASSERT((WireEncodable<T>));
182
183 Block block;
184 try {
185 block = obj.wireEncode();
186 }
187 catch (const std::exception& e) {
188 NDN_THROW_NESTED(Error("Encode error during save: "s + e.what()));
189 }
190
191 saveBuffer(block, os, encoding);
192}
193
201template<typename T>
202[[deprecated("use saveTlv")]]
203void
204save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
205{
206 BOOST_CONCEPT_ASSERT((WireEncodable<T>));
207 detail::checkNestedError<T>(nullptr);
208
209 Block block;
210 try {
211 block = obj.wireEncode();
212 }
213 catch (const tlv::Error& e) {
214 NDN_THROW_NESTED(Error("Encode error during save: "s + e.what()));
215 }
216
217 saveBuffer(block, os, encoding);
218}
219
227template<typename T>
228[[deprecated("use saveTlv")]]
229void
230save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
231{
232 std::ofstream os(filename);
233 save(obj, os, encoding);
234}
235
236} // namespace ndn::io
237
238#endif // NDN_CXX_UTIL_IO_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
A concept check for TLV abstraction with a wireDecode(Block) method and constructible from Block.
Definition concepts.hpp:82
A concept check for TLV abstraction with a wireEncode() method.
Definition concepts.hpp:46
Represents an error in TLV encoding or decoding.
Definition tlv.hpp:54
#define NDN_THROW_NESTED(e)
Definition exception.hpp:65
constexpr void checkNestedError(typename T::Error *)
Definition io.hpp:67
Definition io.cpp:33
T loadTlv(std::istream &is, IoEncoding encoding=BASE64)
Reads a TLV element of type T from a stream.
Definition io.hpp:100
void save(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
Writes a TLV element to a stream.
Definition io.hpp:204
void saveBuffer(span< const uint8_t > buf, std::ostream &os, IoEncoding encoding)
Writes a sequence of bytes to a stream.
Definition io.cpp:67
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE64)
Reads a TLV element from a stream.
Definition io.hpp:123
void saveTlv(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
Writes a TLV element of type T to a stream.
Definition io.hpp:179
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition io.hpp:41
@ NO_ENCODING
Raw binary, without encoding.
Definition io.hpp:45
@ HEX
Hexadecimal encoding.
Definition io.hpp:60
@ BASE64
Base64 encoding.
Definition io.hpp:53
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition io.cpp:36