Skip to content

Commit

Permalink
Fix review:
Browse files Browse the repository at this point in the history
- Move BlobImpl implementation to Blob
- Remove BlobImpl

Signed-off-by: luckychess <[email protected]>
  • Loading branch information
luckychess authored and lebdron committed Dec 19, 2017
1 parent 896152f commit 6fb8868
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 80 deletions.
35 changes: 31 additions & 4 deletions shared_model/cryptography/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,49 @@
#ifndef IROHA_SHARED_MODEL_BLOB_HPP
#define IROHA_SHARED_MODEL_BLOB_HPP

#include <iomanip>
#include <sstream>
#include "interfaces/model_primitive.hpp"
#include "utils/lazy_initializer.hpp"
#include "utils/string_builder.hpp"

namespace shared_model {
namespace crypto {

/**
* Blob interface present user-friendly blob for working with low-level
* Blob class present user-friendly blob for working with low-level
* binary stuff. Its length is not fixed in compile time.
*/
class Blob : public interface::ModelPrimitive<Blob> {
public:
/**
* Create blob from a string
* @param blob - string to create blob from
*/
explicit Blob(const std::string &blob)
: blob_(blob), hex_([this]() {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (const auto &c : blob_) {
ss << std::setw(2) << static_cast<int>(c);
}
return ss.str();
}) {}

/**
* @return provides raw representation of blob
*/
virtual const std::string &blob() const = 0;
const std::string &blob() const { return blob_; }

/**
* @return provides human-readable representation of blob
*/
virtual const std::string &hex() const = 0;
const std::string &hex() const { return hex_.get(); }

/**
* @return size of raw representation of blob
*/
virtual size_t size() const = 0;
size_t size() const { return blob_.size(); }

std::string toString() const override {
return detail::PrettyStringBuilder()
Expand All @@ -56,6 +73,8 @@ namespace shared_model {
return blob() == rhs.blob();
}

Blob *copy() const override { return new Blob(blob()); };

/**
* Method perform transforming object to old-fashion blob_t format
* @tparam BlobType - type of blob
Expand All @@ -67,6 +86,14 @@ namespace shared_model {
[[deprecated]] BlobType makeOldModel() const {
return BlobType::from_string(blob());
}

private:
template <typename Value>
using Lazy = detail::LazyInitializer<Value>;

// TODO: 17/11/2017 luckychess use improved Lazy with references support
std::string blob_;
Lazy<std::string> hex_;
};
} // namespace crypto
} // namespace shared_model
Expand Down
60 changes: 0 additions & 60 deletions shared_model/cryptography/blob_impl.hpp

This file was deleted.

6 changes: 3 additions & 3 deletions shared_model/cryptography/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IROHA_SHARED_MODEL_HASH_HPP
#define IROHA_SHARED_MODEL_HASH_HPP

#include "cryptography/blob_impl.hpp"
#include "cryptography/blob.hpp"
#include "utils/string_builder.hpp"

namespace shared_model {
Expand All @@ -28,9 +28,9 @@ namespace shared_model {
* make difference between Hash which should represent a hashing result and
* a generic Blob which should represent any binary data.
*/
class Hash : public BlobImpl {
class Hash : public Blob {
public:
explicit Hash(const std::string &hash) : BlobImpl(hash) {}
explicit Hash(const std::string &hash) : Blob(hash) {}

std::string toString() const override {
return detail::PrettyStringBuilder()
Expand Down
1 change: 0 additions & 1 deletion shared_model/cryptography/keypair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#ifndef IROHA_SHARED_MODEL_KEYPAIR_HPP
#define IROHA_SHARED_MODEL_KEYPAIR_HPP

#include "cryptography/blob_impl.hpp"
#include "cryptography/private_key.hpp"
#include "cryptography/public_key.hpp"
#include "interfaces/primitive.hpp"
Expand Down
7 changes: 3 additions & 4 deletions shared_model/cryptography/private_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IROHA_SHARED_MODEL_PRIVATE_KEY_HPP
#define IROHA_SHARED_MODEL_PRIVATE_KEY_HPP

#include "cryptography/blob_impl.hpp"
#include "cryptography/blob.hpp"
#include "utils/string_builder.hpp"

#include "common/types.hpp"
Expand All @@ -28,10 +28,9 @@ namespace shared_model {
/**
* A special class for storing private keys.
*/
class PrivateKey : public BlobImpl {
class PrivateKey : public Blob {
public:
explicit PrivateKey(const std::string &privateKey)
: BlobImpl(privateKey) {}
explicit PrivateKey(const std::string &privateKey) : Blob(privateKey) {}
using OldPrivateKeyType = iroha::privkey_t;
std::string toString() const override {
return detail::PrettyStringBuilder()
Expand Down
6 changes: 3 additions & 3 deletions shared_model/cryptography/public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IROHA_SHARED_MODEL_PUBLIC_KEY_HPP
#define IROHA_SHARED_MODEL_PUBLIC_KEY_HPP

#include "cryptography/blob_impl.hpp"
#include "cryptography/blob.hpp"
#include "utils/string_builder.hpp"

#include "common/types.hpp"
Expand All @@ -28,9 +28,9 @@ namespace shared_model {
/**
* A special class for storing public keys.
*/
class PublicKey : public BlobImpl {
class PublicKey : public Blob {
public:
explicit PublicKey(const std::string &publicKey) : BlobImpl(publicKey) {}
explicit PublicKey(const std::string &publicKey) : Blob(publicKey) {}
using OldPublicKeyType = iroha::pubkey_t;
std::string toString() const override {
return detail::PrettyStringBuilder()
Expand Down
6 changes: 3 additions & 3 deletions shared_model/cryptography/seed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IROHA_SEED_HPP
#define IROHA_SEED_HPP

#include "cryptography/blob_impl.hpp"
#include "cryptography/blob.hpp"
#include "utils/string_builder.hpp"

#include "common/types.hpp"
Expand All @@ -28,9 +28,9 @@ namespace shared_model {
/**
* Class for seed representation.
*/
class Seed : public BlobImpl {
class Seed : public Blob {
public:
explicit Seed(const std::string &seed) : BlobImpl(seed) {}
explicit Seed(const std::string &seed) : Blob(seed) {}
/// Old model seed does not have a pretty-looking typedef
using OldSeedType = iroha::blob_t<32>;
std::string toString() const override {
Expand Down
4 changes: 2 additions & 2 deletions shared_model/cryptography/signed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace shared_model {
* Class for storing signed data. It could be used not only for storing
* signed hashes but for other signed objects too.
*/
class Signed : public BlobImpl {
class Signed : public Blob {
public:
using OldSignatureType = iroha::sig_t;
explicit Signed(const std::string &blob) : BlobImpl(blob) {}
explicit Signed(const std::string &blob) : Blob(blob) {}
std::string toString() const override {
return detail::PrettyStringBuilder()
.init("Signed")
Expand Down

0 comments on commit 6fb8868

Please sign in to comment.