Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/maidsafe/common/data_types/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Data {

Data();
Data(const Data&);
Data(Data&& other);
Data(Data&& other) MAIDSAFE_NOEXCEPT;
Data& operator=(const Data&);
Data& operator=(Data&& other);
Data& operator=(Data&& other) MAIDSAFE_NOEXCEPT;
virtual ~Data();

// Returns false for a default-constructed instance of this class, otherwise true.
Expand Down
4 changes: 3 additions & 1 deletion include/maidsafe/common/data_types/immutable_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace maidsafe {

class ImmutableData : public Data {
public:
static NameAndTypeId MakeNameAndTypeId(Identity name);

explicit ImmutableData(NonEmptyString value);

ImmutableData();
Expand Down Expand Up @@ -65,7 +67,7 @@ class ImmutableData : public Data {
}

private:
virtual std::uint32_t ThisTypeId() const final { return 0; }
virtual std::uint32_t ThisTypeId() const final;

NonEmptyString value_;
};
Expand Down
9 changes: 6 additions & 3 deletions include/maidsafe/common/data_types/mutable_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "cereal/types/base_class.hpp"
#include "cereal/types/polymorphic.hpp"

#include "maidsafe/common/config.h"
#include "maidsafe/common/identity.h"
#include "maidsafe/common/log.h"
#include "maidsafe/common/types.h"
Expand All @@ -34,13 +35,15 @@ namespace maidsafe {

class MutableData : public Data {
public:
static NameAndTypeId MakeNameAndTypeId(Identity name);

MutableData(Identity name, NonEmptyString value);

MutableData();
MutableData(const MutableData&);
MutableData(MutableData&& other);
MutableData(MutableData&& other) MAIDSAFE_NOEXCEPT;
MutableData& operator=(const MutableData&);
MutableData& operator=(MutableData&& other);
MutableData& operator=(MutableData&& other) MAIDSAFE_NOEXCEPT;
virtual ~MutableData() final;

const NonEmptyString& Value() const;
Expand All @@ -64,7 +67,7 @@ class MutableData : public Data {
}

private:
virtual std::uint32_t ThisTypeId() const final { return 1; }
virtual std::uint32_t ThisTypeId() const final;

NonEmptyString value_;
};
Expand Down
35 changes: 35 additions & 0 deletions include/maidsafe/common/data_units.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2015 MaidSafe.net limited

This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").

By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses

Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied.

See the Licences for the specific language governing permissions and limitations relating to
use of the MaidSafe Software. */

#ifndef MAIDSAFE_COMMON_DATA_UNITS_H_
#define MAIDSAFE_COMMON_DATA_UNITS_H_

#include <chrono>

namespace maidsafe {
// SI units.
using Bytes = std::chrono::duration<uint64_t>;
using KiloBytes = std::chrono::duration<uint64_t, std::kilo>;
using MegaBytes = std::chrono::duration<uint64_t, std::mega>;
using GigaBytes = std::chrono::duration<uint64_t, std::giga>;
using TeraBytes = std::chrono::duration<uint64_t, std::tera>;
using PetaBytes = std::chrono::duration<uint64_t, std::peta>;
using ExaBytes = std::chrono::duration<uint64_t, std::exa>;
}

#endif // MAIDSAFE_COMMON_DATA_UNITS_H_
4 changes: 2 additions & 2 deletions src/maidsafe/common/data_types/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ Data::Data() = default;

Data::Data(const Data&) = default;

Data::Data(Data&& other) : name_(std::move(other.name_)) {}
Data::Data(Data&& other) MAIDSAFE_NOEXCEPT : name_(std::move(other.name_)) {}

Data& Data::operator=(const Data&) = default;

Data& Data::operator=(Data&& other) {
Data& Data::operator=(Data&& other) MAIDSAFE_NOEXCEPT {
name_ = std::move(other.name_);
return *this;
}
Expand Down
9 changes: 9 additions & 0 deletions src/maidsafe/common/data_types/immutable_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
#include "maidsafe/common/error.h"

namespace maidsafe {
namespace {
const std::uint32_t kTypeId = 0;
}

Data::NameAndTypeId ImmutableData::MakeNameAndTypeId(Identity name) {
return NameAndTypeId{std::move(name), DataTypeId{kTypeId}};
}

ImmutableData::ImmutableData(NonEmptyString value)
: Data(crypto::Hash<crypto::SHA512>(value)), value_(std::move(value)) {}
Expand Down Expand Up @@ -50,4 +57,6 @@ const NonEmptyString& ImmutableData::Value() const {
return value_;
}

std::uint32_t ImmutableData::ThisTypeId() const { return kTypeId; }

} // namespace maidsafe
13 changes: 11 additions & 2 deletions src/maidsafe/common/data_types/mutable_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#include "maidsafe/common/serialisation/serialisation.h"

namespace maidsafe {
namespace {
const std::uint32_t kTypeId = 1;
}

Data::NameAndTypeId MutableData::MakeNameAndTypeId(Identity name) {
return NameAndTypeId{std::move(name), DataTypeId{kTypeId}};
}

MutableData::MutableData(Identity name, NonEmptyString value)
: Data(std::move(name)), value_(std::move(value)) {
Expand All @@ -43,12 +50,12 @@ MutableData::MutableData() = default;

MutableData::MutableData(const MutableData&) = default;

MutableData::MutableData(MutableData&& other)
MutableData::MutableData(MutableData&& other) MAIDSAFE_NOEXCEPT
: Data(std::move(other)), value_(std::move(other.value_)) {}

MutableData& MutableData::operator=(const MutableData&) = default;

MutableData& MutableData::operator=(MutableData&& other) {
MutableData& MutableData::operator=(MutableData&& other) MAIDSAFE_NOEXCEPT {
Data::operator=(std::move(other));
value_ = std::move(other.value_);
return *this;
Expand All @@ -62,4 +69,6 @@ const NonEmptyString& MutableData::Value() const {
return value_;
}

std::uint32_t MutableData::ThisTypeId() const { return kTypeId; }

} // namespace maidsafe
10 changes: 8 additions & 2 deletions src/maidsafe/common/data_types/tests/derived_data_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ class DerivedDataTest : public testing::Test {
}
}

T GetData();

const NonEmptyString value_;
Identity name_;
T data_;
const DataTypeId type_id_;

private:
Identity GetName();
T GetData();
};

template <>
Expand Down Expand Up @@ -137,6 +138,11 @@ TYPED_TEST(DerivedDataTest, BEH_SerialiseParse) {
EXPECT_TRUE(Equal(&this->data_, dynamic_cast<TypeParam*>(parsed_ptr.get())));
}

TYPED_TEST(DerivedDataTest, MakeNameAndTypeId) {
const auto data = this->GetData();
const auto name_and_type = TypeParam::MakeNameAndTypeId(this->name_);
EXPECT_EQ(data.TypeId(), name_and_type.type_id);
EXPECT_EQ(data.Name(), name_and_type.name);
}
} // namespace test

} // namespace maidsafe