- Notifications
You must be signed in to change notification settings - Fork 961
/
Copy pathclass1.cpp
230 lines (185 loc) · 4.92 KB
/
class1.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Class1.cpp
#include"pch.h"
#include"Class1.h"
#include<algorithm>
#include<ppltasks.h>
usingnamespaceJS_Array;
// 05 at the top because we need the includes and usings
//<snippet05>
#include<vector>
#include<collection.h>
usingnamespacePlatform;
usingnamespacestd;
usingnamespacePlatform::Collections;
voidArrayConversions(const Array<int>^ arr)
{
// Construct an Array from another Array.
Platform::Array<int>^ newArr = ref new Platform::Array<int>(arr);
// Construct a Vector from an Array
auto v = ref new Platform::Collections::Vector<int>(arr);
// Construct a std::vector. Two options.
vector<int> v1(begin(arr), end(arr));
vector<int> v2(arr->begin(), arr->end());
// Initialize a vector one element at a time.
// using a range for loop. Not as efficient as using begin/end.
vector<int> v3;
for(int i : arr)
{
v3.push_back(i);
}
}
//</snippet05>
Class1::Class1()
{
}
voidClass1::ConversionDemo(const Array<int>^ arr)
{
ArrayConversions(arr);
}
//<snippet01>
doubleClass1::PassArrayForReading(const Array<double>^ arr)
{
double sum = 0;
for(unsignedint i = 0 ; i < arr->Length; i++)
{
sum += arr[i];
}
return sum;
}
//</snippet01>
//<snippet02>
// Return array as out parameter...
voidClass1::CalleeAllocatedDemo(Array<int>^* arr)
{
auto temp = ref new Array<int>(10);
for(unsignedint i = 0; i < temp->Length; i++)
{
temp[i] = i;
}
*arr = temp;
}
// ...or return array as return value:
Array<int>^ Class1::CalleeAllocatedDemo2()
{
auto temp = ref new Array<int>(10);
for(unsignedint i = 0; i < temp->Length; i++)
{
temp[i] = i;
}
return temp;
}
//</snippet02>
//not used
Array<int>^ Class1::CalleeAllocatedDemo3()
{
std::vector<int> vec;
for(int i = 0; i < 10; i++)
{
vec.push_back(i);
}
return ref new Array<int>(10/*vec*/); //compiler error
}
//<snippet03>
voidClass1::CallerAllocatedDemo(Platform::WriteOnlyArray<int>^ arr)
{
// You can write to the elements directly.
for(unsignedint i = 0; i < arr->Length; i++)
{
arr[i] = i;
}
}
//</snippet03>
//<snippet09>
voidClass1::InvokeIterators(Platform::WriteOnlyArray<int>^ arr)
{
int k = 0;
// You can write to the elements directly.
for (unsignedint i = 9; i > 0; i--)
{
arr->set(k++, i);
}
std::sort(arr->begin(), arr->end());
}
//</snippet09>
usingnamespaceWindows::Foundation::Collections;
usingnamespacePlatform;
usingnamespacePlatform::Collections;
Person::Person(String^ name): m_name(name) { }
voidPerson::AddPhoneNumber(String^ type, String^ number)
{
m_numbers[type] = number;
}
IMapView<String^, String^>^ Person::PhoneNumbers::get()
{
return ref new MapView<String^, String^>(m_numbers);
}
ref classDateTracker sealed
{
public:
property Windows::Foundation::Uri^ uri;
// ...
};
voidDoSomething()
{
Windows::Foundation::Uri docs("http://docs.microsoft.com");
Windows::Foundation::Uri^ devCenter = docs.CombineUri("/windows/");
// ...
} // both variables cleaned up here.
#include<collection.h>
#include<vector>
#include<utility>
usingnamespacePlatform::Collections;
usingnamespaceWindows::Foundation::Collections;
usingnamespacestd;
IVector<int>^ GetInts()
{
vector<int> vec;
for(int i = 0; i < 10; i++)
{
vec.push_back(i);
}
//Implicit conversion to IVector
return ref new Vector<int>(std::move(vec));
}
//<snippet06>
Array<int>^ GetNums()
{
int nums[] = {0,1,2,3,4};
//Use nums internally....
// Convert to Platform::Array and return to caller.
return ref new Array<int>(nums, 5);
}
//</snippet06>
//Array<int>^ GetNums2()
//{
// vector<int> vec;
// for(int i = 0; i < 10; i++)
// {
// vec.push_back(i);
// }
// //Implicit conversion to IVector
// return ref new Array<int>(begin(vec), end(vec));
//}
usingnamespaceWindows::Storage;
usingnamespaceWindows::Storage::Streams;
usingnamespaceconcurrency;
voidClass1::TestDataReader()
{
//String^ Filename = "test.data";
// Windows::Storage::StorageFile^ sampleFile;
// create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Filename)).then([this](task<StorageFile^> getFileTask)
//{
// try
// {
// sampleFile = getFileTask.get();
// }
// catch (Platform::Exception^)
// {
// // sample file doesn't exist so scenario one must be run first.
// }
//});
// IInputStream^ str =
// DataReader^ dataReader = ref new DataReader(sampleFile);
// uint8 data[1024];
// dataReader->ReadBytes( ArrayReference<uint8>(data, 1024) );
}