// Class1.cpp #include "pch.h" #include "Class1.h" #include #include using namespace JS_Array; // 05 at the top because we need the includes and usings // #include #include using namespace Platform; using namespace std; using namespace Platform::Collections; void ArrayConversions(const Array^ arr) { // Construct an Array from another Array. Platform::Array^ newArr = ref new Platform::Array(arr); // Construct a Vector from an Array auto v = ref new Platform::Collections::Vector(arr); // Construct a std::vector. Two options. vector v1(begin(arr), end(arr)); vector 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 v3; for(int i : arr) { v3.push_back(i); } } // Class1::Class1() { } void Class1::ConversionDemo(const Array^ arr) { ArrayConversions(arr); } // double Class1::PassArrayForReading(const Array^ arr) { double sum = 0; for(unsigned int i = 0 ; i < arr->Length; i++) { sum += arr[i]; } return sum; } // // // Return array as out parameter... void Class1::CalleeAllocatedDemo(Array^* arr) { auto temp = ref new Array(10); for(unsigned int i = 0; i < temp->Length; i++) { temp[i] = i; } *arr = temp; } // ...or return array as return value: Array^ Class1::CalleeAllocatedDemo2() { auto temp = ref new Array(10); for(unsigned int i = 0; i < temp->Length; i++) { temp[i] = i; } return temp; } // //not used Array^ Class1::CalleeAllocatedDemo3() { std::vector vec; for(int i = 0; i < 10; i++) { vec.push_back(i); } return ref new Array(10/*vec*/); //compiler error } // void Class1::CallerAllocatedDemo(Platform::WriteOnlyArray^ arr) { // You can write to the elements directly. for(unsigned int i = 0; i < arr->Length; i++) { arr[i] = i; } } // // void Class1::InvokeIterators(Platform::WriteOnlyArray^ arr) { int k = 0; // You can write to the elements directly. for (unsigned int i = 9; i > 0; i--) { arr->set(k++, i); } std::sort(arr->begin(), arr->end()); } // using namespace Windows::Foundation::Collections; using namespace Platform; using namespace Platform::Collections; Person::Person(String^ name): m_name(name) { } void Person::AddPhoneNumber(String^ type, String^ number) { m_numbers[type] = number; } IMapView^ Person::PhoneNumbers::get() { return ref new MapView(m_numbers); } ref class DateTracker sealed { public: property Windows::Foundation::Uri^ uri; // ... }; void DoSomething() { Windows::Foundation::Uri docs("http://docs.microsoft.com"); Windows::Foundation::Uri^ devCenter = docs.CombineUri("/windows/"); // ... } // both variables cleaned up here. #include #include #include using namespace Platform::Collections; using namespace Windows::Foundation::Collections; using namespace std; IVector^ GetInts() { vector vec; for(int i = 0; i < 10; i++) { vec.push_back(i); } //Implicit conversion to IVector return ref new Vector(std::move(vec)); } // Array^ GetNums() { int nums[] = {0,1,2,3,4}; //Use nums internally.... // Convert to Platform::Array and return to caller. return ref new Array(nums, 5); } // //Array^ GetNums2() //{ // vector vec; // for(int i = 0; i < 10; i++) // { // vec.push_back(i); // } // //Implicit conversion to IVector // return ref new Array(begin(vec), end(vec)); //} using namespace Windows::Storage; using namespace Windows::Storage::Streams; using namespace concurrency; void Class1::TestDataReader() { //String^ Filename = "test.data"; // Windows::Storage::StorageFile^ sampleFile; // create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Filename)).then([this](task 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(data, 1024) ); }