1
0
Fork 0
mirror of https://git.suyu.dev/suyu/sirit.git synced 2024-12-23 04:32:04 +00:00
sirit/src/literal_number.h

43 lines
1 KiB
C
Raw Normal View History

2018-08-28 08:16:52 +01:00
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
2018-11-16 07:10:10 +00:00
* Lesser General Public License version 3 or any later version.
2018-08-28 08:16:52 +01:00
*/
#pragma once
#include <cstring>
2018-10-03 04:32:45 +01:00
#include <typeindex>
2019-03-11 06:26:21 +00:00
#include "operand.h"
#include "stream.h"
2018-10-03 04:32:45 +01:00
2018-08-28 08:16:52 +01:00
namespace Sirit {
class LiteralNumber : public Operand {
2019-03-11 06:26:21 +00:00
public:
2018-10-03 04:32:45 +01:00
LiteralNumber(std::type_index type);
2019-03-14 06:25:57 +00:00
~LiteralNumber() override;
2018-08-28 08:16:52 +01:00
2019-03-14 06:25:57 +00:00
void Fetch(Stream& stream) const override;
u16 GetWordCount() const override;
2018-08-28 08:16:52 +01:00
2019-03-14 06:25:57 +00:00
bool operator==(const Operand& other) const override;
2018-08-28 08:16:52 +01:00
2019-03-11 06:26:21 +00:00
template <typename T>
static LiteralNumber* Create(T value) {
2018-10-03 04:32:45 +01:00
static_assert(sizeof(T) == 4 || sizeof(T) == 8);
2018-10-03 04:32:45 +01:00
LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T)));
number->is_32 = sizeof(T) == 4;
std::memcpy(&number->raw, &value, sizeof(T));
2018-10-03 04:32:45 +01:00
return number;
}
2019-03-11 06:26:21 +00:00
private:
std::type_index type;
bool is_32{};
u64 raw{};
2018-08-28 08:16:52 +01:00
};
} // namespace Sirit