NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 "concepts.hpp"
26 #include "../encoding/block.hpp"
27 
28 #include <fstream>
29 
30 namespace ndn {
31 namespace io {
32 
33 class Error : public std::runtime_error
34 {
35 public:
36  explicit
37  Error(const std::string& what)
38  : std::runtime_error(what)
39  {
40  }
41 };
42 
45 enum IoEncoding {
49 
56 
62 };
63 
64 namespace detail {
65 
66 template<typename T>
67 static void
68 checkInnerError(typename T::Error*)
69 {
71  "T::Error, if declared, must inherit from ndn::tlv::Error");
72 }
73 
74 template<typename T>
75 static void
77 {
78  // T::Error is not declared
79 }
80 
81 } // namespace detail
82 
88 loadBlock(std::istream& is, IoEncoding encoding = BASE64);
89 
95 template<typename T>
96 shared_ptr<T>
97 load(std::istream& is, IoEncoding encoding = BASE64)
98 {
99  BOOST_CONCEPT_ASSERT((WireDecodable<T>));
100  detail::checkInnerError<T>(nullptr);
101 
102  optional<Block> block = loadBlock(is, encoding);
103  if (!block) {
104  return nullptr;
105  }
106 
107  try {
108  return make_shared<T>(*block);
109  }
110  catch (const tlv::Error&) {
111  return nullptr;
112  }
113 }
114 
117 template<typename T>
118 shared_ptr<T>
119 load(const std::string& filename, IoEncoding encoding = BASE64)
120 {
121  std::ifstream is(filename);
122  return load<T>(is, encoding);
123 }
124 
128 void
129 saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64);
130 
136 template<typename T>
137 void
138 save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
139 {
140  BOOST_CONCEPT_ASSERT((WireEncodable<T>));
141  detail::checkInnerError<T>(nullptr);
142 
143  Block block;
144  try {
145  block = obj.wireEncode();
146  }
147  catch (const tlv::Error& e) {
148  BOOST_THROW_EXCEPTION(Error(e.what()));
149  }
150 
151  saveBlock(block, os, encoding);
152 }
153 
156 template<typename T>
157 void
158 save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
159 {
160  std::ofstream os(filename);
161  save(obj, os, encoding);
162 }
163 
164 } // namespace io
165 } // namespace ndn
166 
167 #endif // NDN_UTIL_IO_HPP
optional< Block > loadBlock(std::istream &is, IoEncoding encoding)
loads a TLV block from a stream
Definition: io.cpp:30
void saveBlock(const Block &block, std::ostream &os, IoEncoding encoding)
saves a TLV block to a stream
Definition: io.cpp:63
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE64)
loads a TLV element from a stream
Definition: io.hpp:97
void save(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
saves a TLV element to a stream
Definition: io.hpp:138
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
hexadecimal encoding
Definition: io.hpp:61
binary without encoding
Definition: io.hpp:48
Error(const std::string &what)
Definition: io.hpp:37
static void checkInnerError(...)
Definition: io.hpp:76
base64 encoding
Definition: io.hpp:55
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:50
IoEncoding
indicates how a file or stream is encoded
Definition: io.hpp:45