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 
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 {
45 
52 
58 };
59 
60 namespace detail {
61 
62 template<typename T>
63 static void
64 checkInnerError(typename T::Error*)
65 {
66  static_assert(std::is_base_of<tlv::Error, typename T::Error>::value,
67  "T::Error, if declared, must inherit from ndn::tlv::Error");
68 }
69 
70 template<typename T>
71 static void
73 {
74  // T::Error is not declared
75 }
76 
77 } // namespace detail
78 
83 optional<Block>
84 loadBlock(std::istream& is, IoEncoding encoding = BASE64);
85 
91 template<typename T>
92 shared_ptr<T>
93 load(std::istream& is, IoEncoding encoding = BASE64)
94 {
95  BOOST_CONCEPT_ASSERT((WireDecodable<T>));
96  detail::checkInnerError<T>(nullptr);
97 
98  optional<Block> block = loadBlock(is, encoding);
99  if (!block) {
100  return nullptr;
101  }
102 
103  try {
104  return make_shared<T>(*block);
105  }
106  catch (const tlv::Error&) {
107  return nullptr;
108  }
109 }
110 
113 template<typename T>
114 shared_ptr<T>
115 load(const std::string& filename, IoEncoding encoding = BASE64)
116 {
117  std::ifstream is(filename);
118  return load<T>(is, encoding);
119 }
120 
124 void
125 saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64);
126 
132 template<typename T>
133 void
134 save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
135 {
136  BOOST_CONCEPT_ASSERT((WireEncodable<T>));
137  detail::checkInnerError<T>(nullptr);
138 
139  Block block;
140  try {
141  block = obj.wireEncode();
142  }
143  catch (const tlv::Error&) {
144  NDN_THROW_NESTED(Error("Encode error during save"));
145  }
146 
147  saveBlock(block, os, encoding);
148 }
149 
152 template<typename T>
153 void
154 save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
155 {
156  std::ofstream os(filename);
157  save(obj, os, encoding);
158 }
159 
160 } // namespace io
161 } // namespace ndn
162 
163 #endif // NDN_UTIL_IO_HPP
optional< Block > loadBlock(std::istream &is, IoEncoding encoding)
loads a TLV block from a stream
Definition: io.cpp:30
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
void saveBlock(const Block &block, std::ostream &os, IoEncoding encoding)
saves a TLV block to a stream
Definition: io.cpp:66
Definition: data.cpp:26
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE64)
loads a TLV element from a stream
Definition: io.hpp:93
void save(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
saves a TLV element to a stream
Definition: io.hpp:134
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
hexadecimal encoding
Definition: io.hpp:57
binary without encoding
Definition: io.hpp:44
static void checkInnerError(...)
Definition: io.hpp:72
base64 encoding
Definition: io.hpp:51
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
IoEncoding
indicates how a file or stream is encoded
Definition: io.hpp:41