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; -*- */
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 <iostream>
29 #include <fstream>
30 
31 namespace ndn {
32 namespace io {
33 
34 class Error : public std::runtime_error
35 {
36 public:
37  explicit
38  Error(const std::string& what)
39  : std::runtime_error(what)
40  {
41  }
42 };
43 
46 enum IoEncoding {
50 
57 
63 };
64 
65 constexpr IoEncoding DEPRECATED(BASE_64) = BASE64;
66 
67 namespace detail {
68 
69 template<typename T>
70 static void
71 checkInnerError(typename T::Error*)
72 {
73  static_assert(std::is_base_of<tlv::Error, typename T::Error>::value,
74  "T::Error, if declared, must inherit from ndn::tlv::Error");
75 }
76 
77 template<typename T>
78 static void
80 {
81  // T::Error is not declared
82 }
83 
84 } // namespace detail
85 
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  optional<Block> 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 
120 template<typename T>
121 shared_ptr<T>
122 load(const std::string& filename, IoEncoding encoding = BASE64)
123 {
124  std::ifstream is(filename);
125  return load<T>(is, encoding);
126 }
127 
131 void
132 saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE64);
133 
139 template<typename T>
140 void
141 save(const T& obj, std::ostream& os, IoEncoding encoding = BASE64)
142 {
143  BOOST_CONCEPT_ASSERT((WireEncodable<T>));
144  detail::checkInnerError<T>(nullptr);
145 
146  Block block;
147  try {
148  block = obj.wireEncode();
149  }
150  catch (const tlv::Error& e) {
151  BOOST_THROW_EXCEPTION(Error(e.what()));
152  }
153 
154  saveBlock(block, os, encoding);
155 }
156 
159 template<typename T>
160 void
161 save(const T& obj, const std::string& filename, IoEncoding encoding = BASE64)
162 {
163  std::ofstream os(filename);
164  save(obj, os, encoding);
165 }
166 
167 } // namespace io
168 } // namespace ndn
169 
170 #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:100
void save(const T &obj, std::ostream &os, IoEncoding encoding=BASE64)
saves a TLV element to a stream
Definition: io.hpp:141
STL namespace.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
hexadecimal encoding
Definition: io.hpp:62
binary without encoding
Definition: io.hpp:49
Error(const std::string &what)
Definition: io.hpp:38
static void checkInnerError(...)
Definition: io.hpp:79
base64 encoding
Definition: io.hpp:56
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
constexpr IoEncoding DEPRECATED(BASE_64)
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
IoEncoding
indicates how a file or stream is encoded
Definition: io.hpp:46