This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVTable.cpp
131 lines (106 loc) · 3.37 KB
/
VTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "VTable.h"
#include "Includes.h"
#include "Preferences.h"
VTable::VTable(System::Object ^tableType) {
TableType = tableType->GetType();
accessors = gcnew cli::array<System::Object^>(0);
objects = gcnew cli::array<System::Object^>(0);
delete tableType;
}
VTable::VTable(System::Type^ tableType) {
TableType = tableType;
accessors = gcnew cli::array<System::Object^>(0);
objects = gcnew cli::array<System::Object^>(0);
delete tableType;
}
VTable::~VTable() {
accessors->Clear;
objects->Clear;
}
System::Object^ VTable::operator()(int location, System::Object^ newobj)
{
this->objects[location] = newobj;
return nullptr;
}
VTable::VTable(System::Type^ keyType, System::Type^ valueType)
{
throw gcnew System::NotImplementedException();
}
int VTable::Length() {
if (accessors->Length != objects->Length)
std::exception("Integrity check failed on VTable length check");
return accessors->Length;
}
// unsigned int does not work with this.
System::Object^ VTable::operator()(int index, bool key) {
if (key) {
std::cout << "E1: " << msclr::interop::marshal_as<std::string>(objects[0]->ToString()) << std::endl;
int as = System::Array::IndexOf(accessors, index);
return objects[index];
}
else
return accessors[System::Array::IndexOf(objects, index)];
}
System::Object^ VTable::operator()(System::Object^ item, bool key) {
if (key) {
return objects[System::Array::IndexOf(accessors, item)];
}
else
return accessors[System::Array::IndexOf(objects, item)];
}
// Default functionality is to return value paired with key.
System::Object^ VTable::operator()(int item) {
return objects[item];
}
// If there are not any objects populating the array, create the arrays and populate them.
void VTable::CreateKeyValuePairs(System::Object^ key, System::Object^ item) {
accessors = gcnew cli::array<System::Object^>(1);
accessors[0] = key;
objects = gcnew cli::array<System::Object^>(1);
objects[0] = item;
}
// Add objects to the array.
void VTable::AddKeyValuePairs(System::Object^ key, System::Object^ item) {
cli::array<System::Object^>^ tempArray = gcnew cli::array<System::Object^>(objects->Length + 1);
int tLength = objects->Length;
for (int idx = 0; idx < tLength; idx++) {
tempArray[idx] = objects[idx];
}
tempArray[tempArray->Length - 1] = item;
objects = tempArray;
tempArray = gcnew cli::array<System::Object^>(accessors->Length + 1);
for (int idx = 0; idx < tLength; idx++) {
tempArray[idx] = accessors[idx];
}
tempArray[tempArray->Length - 1] = key;
accessors = tempArray;
delete key, item, tempArray;
}
// Add object to array.
void VTable::Add(System::Object^ key, System::Object^ item) {
if (TableType != item->GetType()) {
std::exception("Types not the same, and multi-type support is not enabled.");
}
if (accessors != nullptr) {
System::Collections::Generic::List<System::Object^> acs = gcnew System::Collections::Generic::List<System::Object^>(accessors);
if (acs.Contains(item)) {
std::exception("Key already exists in VTable!");
}
acs.Clear();
AddKeyValuePairs(key, item);
}
else
CreateKeyValuePairs(key, item);
delete key, item;
}
inline bool VTable::ContainsValue(System::Object^ value)
{
return System::Array::IndexOf(objects, value) != -1;
}
inline bool VTable::ContainsAccessor(System::Object^ value)
{
return System::Array::IndexOf(accessors, value) != -1;
}
cli::array<System::Object^>^ VTable::GetArray() {
return objects;
}