Core: Add MovableValue
This commit is contained in:
parent
68760209c1
commit
fca3d855bd
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Copyright (C) 2020 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_MOVABLE_VALUE_HPP
|
||||||
|
#define NAZARA_MOVABLE_VALUE_HPP
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
class MovableValue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MovableValue(T value = T{});
|
||||||
|
MovableValue(const MovableValue&) = default;
|
||||||
|
MovableValue(MovableValue&& ptr) noexcept;
|
||||||
|
~MovableValue() = default;
|
||||||
|
|
||||||
|
T& Get();
|
||||||
|
const T& Get() const;
|
||||||
|
|
||||||
|
operator T&();
|
||||||
|
operator const T&() const;
|
||||||
|
|
||||||
|
MovableValue& operator=(T value);
|
||||||
|
MovableValue& operator=(const MovableValue&) = default;
|
||||||
|
MovableValue& operator=(MovableValue&& ptr) noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
T m_value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/MovableValue.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_MOVABLE_VALUE_HPP
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
// Copyright (C) 2020 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/MovableValue.hpp>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>::MovableValue(T value) :
|
||||||
|
m_value(std::move(value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>::MovableValue(MovableValue&& val) noexcept :
|
||||||
|
m_value()
|
||||||
|
{
|
||||||
|
std::swap(m_value, val.m_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T& MovableValue<T>::Get()
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
const T& MovableValue<T>::Get() const
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>::operator T&()
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>::operator const T&() const
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>& MovableValue<T>::operator=(T value)
|
||||||
|
{
|
||||||
|
m_value = std::move(value);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
MovableValue<T>& MovableValue<T>::operator=(MovableValue&& ptr) noexcept
|
||||||
|
{
|
||||||
|
std::swap(m_value, ptr.m_value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue