CommonLibSSE NG
Loading...
Searching...
No Matches
PackUnpackImpl.h
Go to the documentation of this file.
1#pragma once
2
3#include "RE/A/Array.h"
5#include "RE/P/PackUnpack.h"
6#include "RE/T/TypeInfo.h"
7#include "RE/V/Variable.h"
9
10// DO NOT INCLUDE EXCEPT IN MEGA HEADER!!!
11
12namespace RE
13{
14 namespace BSScript
15 {
16 // T requires:
17 // * begin()
18 // * end()
19 // * size()
20 // * value_type
21 // * input iterator:
22 // * weakly incrementable
23 // * indirectly readable
24 template <
25 class T,
26 class U,
27 std::enable_if_t<
28 is_array_v<U>,
29 int>>
30 void PackValue(Variable* a_dst, T&& a_src)
31 {
32 assert(a_dst);
33 a_dst->SetNone();
34
36 if (!vm) {
37 assert(false);
38 return;
39 }
40
41 BSTSmartPointer<Array> array;
42 TypeInfo typeInfo(GetRawType<typename U::value_type>{}());
43 if (!vm->CreateArray(typeInfo, static_cast<std::uint32_t>(a_src.size()), array) || !array) {
44 assert(false);
45 return;
46 }
47
48 auto it = a_src.begin();
49 auto end = a_src.end();
50 std::uint32_t i = 0;
51 while (it != end) {
52 if constexpr (std::is_same_v<U, std::vector<bool>>) {
53 (*array)[i++].Pack(static_cast<bool>(*it));
54 } else {
55 (*array)[i++].Pack(*it);
56 }
57 ++it;
58 }
59
60 a_dst->SetArray(std::move(array));
61 }
62
63 // T requires:
64 // * default constructible
65 // * destructible
66 // * value_type
67 // * push_back(value_type)
68 template <
69 class T,
70 std::enable_if_t<
71 is_array_v<T>,
72 int>>
73 [[nodiscard]] T UnpackValue(const Variable* a_src)
74 {
75 assert(a_src);
76
77 std::remove_const_t<T> container;
78 if (a_src->IsNoneObject() || a_src->IsNoneArray()) {
79 return container;
80 }
81
82 auto array = a_src->GetArray();
83 if (!array) {
84 return container;
85 }
86
87 for (auto& elem : *array) {
88 container.push_back(elem.Unpack<typename T::value_type>());
89 }
90
91 return container;
92 }
93 }
94}
static VirtualMachine * GetSingleton()
T UnpackValue(const Variable *a_src)
Definition PackUnpack.h:188
void PackValue(Variable *a_dst, T &&a_src)
Definition PackUnpack.h:90
Definition AbsorbEffect.h:6