src: Improving consistency and correcting code style
As of this commit, all data structures can be directly constructed from
wire format.
This commit excludes full correction of code style in security/ and
tools/ndnsec*, which will be part of a different commit.
Change-Id: I121ac1f81948bc7468990df52cdefeb2988d91a1
Refs: #1403
diff --git a/src/util/io.hpp b/src/util/io.hpp
index 6026f06..ff6852b 100644
--- a/src/util/io.hpp
+++ b/src/util/io.hpp
@@ -19,7 +19,15 @@
namespace ndn {
namespace io {
-struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+class Error : public std::runtime_error
+{
+public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+};
enum IoEncoding {
NO_ENCODING,
@@ -27,8 +35,8 @@
HEX
};
-template<typename T>
-shared_ptr<T>
+template<typename T>
+shared_ptr<T>
load(std::istream& is, IoEncoding encoding = BASE_64)
{
typedef typename T::Error TypeError;
@@ -39,54 +47,54 @@
shared_ptr<T> object = make_shared<T>();
OBufferStream os;
-
- switch(encoding)
- {
- case NO_ENCODING:
- {
- FileSource ss(is, true, new FileSink(os));
- break;
- }
- case BASE_64:
- {
- FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
- break;
- }
- case HEX:
- {
- FileSource ss(is, true, new HexDecoder(new FileSink(os)));
- break;
- }
- default:
- return shared_ptr<T>();
- }
+
+ switch (encoding)
+ {
+ case NO_ENCODING:
+ {
+ FileSource ss(is, true, new FileSink(os));
+ break;
+ }
+ case BASE_64:
+ {
+ FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
+ break;
+ }
+ case HEX:
+ {
+ FileSource ss(is, true, new HexDecoder(new FileSink(os)));
+ break;
+ }
+ default:
+ return shared_ptr<T>();
+ }
object->wireDecode(Block(os.buf()));
return object;
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
return shared_ptr<T>();
}
- catch(Block::Error& e)
+ catch (Block::Error& e)
{
return shared_ptr<T>();
}
- catch(TypeError& e)
+ catch (TypeError& e)
{
return shared_ptr<T>();
}
}
-template<typename T>
-shared_ptr<T>
+template<typename T>
+shared_ptr<T>
load(const std::string& file, IoEncoding encoding = BASE_64)
{
std::ifstream is(file.c_str());
return load<T>(is, encoding);
}
-template<typename T>
+template<typename T>
void
save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64)
{
@@ -96,38 +104,41 @@
using namespace CryptoPP;
Block block = object.wireEncode();
-
- switch(encoding)
- {
- case NO_ENCODING:
- {
- StringSource ss(block.wire(), block.size(), true, new FileSink(os));
- break;
- }
- case BASE_64:
- {
- StringSource ss(block.wire(), block.size(), true, new Base64Encoder(new FileSink(os), true, 64));
- break;
- }
- case HEX:
- {
- StringSource ss(block.wire(), block.size(), true, new HexEncoder(new FileSink(os)));
- break;
- }
- default:
- return;
- }
+
+ switch (encoding)
+ {
+ case NO_ENCODING:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new FileSink(os));
+ break;
+ }
+ case BASE_64:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new Base64Encoder(new FileSink(os), true, 64));
+ break;
+ }
+ case HEX:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new HexEncoder(new FileSink(os)));
+ break;
+ }
+ default:
+ return;
+ }
return;
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
throw Error(e.what());
}
- catch(Block::Error& e)
+ catch (Block::Error& e)
{
throw Error(e.what());
}
- catch(TypeError& e)
+ catch (TypeError& e)
{
throw Error(e.what());
}
@@ -145,4 +156,3 @@
} // namespace ndn
#endif // NDN_UTIL_IO_HPP
-