将旧的C ++代码集成到PNaCl模块中

时间:2016-08-28 12:07:14

标签: c++ google-nativeclient

我阅读了Google的本机客户端教程,了解如何多次构建我自己的基于C ++的PNaCl模块,不知怎的,我没有变得更聪明,我知道如果我想实现消息传递功能。我在.cc文件中有以下内容作为PNaCl代码的基础,所有这些都来自Googles的Hello World教程:

unsorted1

据我所知,PNaCl模块不使用main()函数,但是假设我有一个旧的C ++代码创建了2个数组unsorted2#include <iostream> #include <stdint.h> #include <unistd.h> #include <array> // a function to create a random number between min and max int32_t rangeRandomAlg (int32_t min, int32_t max) { int32_t num = max - min + 1; int32_t remainder = RAND_MAX % num; int32_t x; do { x = rand(); } while (x >= RAND_MAX - remainder); return min + x % num; } // a function to create arrays with random numbers void unsortedArrays(int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum){ for(int32_t i = 0; i <= arrayElements; i++) { if (i < arrayElements/2) { unsorted1[i] = rangeRandomAlg(minNum, maxNum); } else { unsorted2[i] = rangeRandomAlg(minNum, maxNum); } } } // the main function int32_t main(int32_t argc, char *argv[]) { // declare all the zises int32_t minNum = 0; int32_t maxNum = 100; int32_t arrayElements = maxNum; // the arrays int32_t unsorted1[arrayElements/2]; int32_t unsorted2[arrayElements/2]; // fill the arrays with random numbers unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum); return 0; } ,其中包含我想要的随机数在我的PNaCl模块中使用:

HandleMessage()

我的问题是我不太明白如何将此代码集成到PNaCl模块中并使用unsorted1函数将unsorted2JavaScript数组发送回具有PostMesage()功能的HandleMessage()。我知道我必须使用数组而不是using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; namespace FormsDataGridViewBinding { public class Person : INotifyPropertyChanged { public Person(string n, string name, string address) { _number = n; _name = name; _address = address; } public string Number { get { return _number; } set { if (_number != value) { _number = value; this.NotifyPropertyChanged("Number"); } } } public string Name { get { return _name; } set { if (_name != value) { _name = value; this.NotifyPropertyChanged("Name"); } } } public string Address { get { return _address; } set { if (_address != value) { _address = value; this.NotifyPropertyChanged("Address"); } } } private string _number; private string _name; private string _address; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } } } 函数中的字符串。

我希望能在这里得到一些帮助,因为我对这个完整的原生客户都很陌生。

1 个答案:

答案 0 :(得分:2)

这里的解决方案需要几个小时才能到达:

=INDEX(D3:H3,A3)  

这只是PNaCl模块的预编译C ++代码,其代码集成在其中,它只将// pepper includes #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" #include "ppapi/cpp/var_array.h" #include "ppapi/cpp/var.h" #include "json/json.h" #include <sstream> // cpp includes #include <stdint.h> #include <unistd.h> #include <array> #include <string> //static variables namespace { // The expected string sent by the browser. const char* const kHelloString = "Bereit fuer dein Modul"; // The string sent back to the browser upon receipt of a message // containing "hello". const char* const kReplyString = "PNaCl hat Ergebnisse geschickt"; } // namespace class job1Instance : public pp::Instance { public: explicit job1Instance(PP_Instance instance): pp::Instance(instance) {} virtual ~job1Instance() {} virtual void HandleMessage(const pp::Var& message) { /* if (!message.is_string()) { return; } std::string message_txt = message.AsString(); pp::Var reply; if (message_txt == kHelloString) { reply = pp::Var(kReplyString); PostMessage(kReplyString); } */ /*** my functions and data for the cpp code to integrate start here ***/ // declare all the zises int32_t minNum = 0; int32_t maxNum = 100; int32_t arrayElements = maxNum; // the arrays int32_t unsorted1[arrayElements/2]; int32_t unsorted2[arrayElements/2]; // fill the arrays with random numbers unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum); std::string outRes1, outRes2; arrayToString(unsorted1, arrayElements/2, outRes1); arrayToString(unsorted2, arrayElements/2, outRes2); PostMessage(outRes1); // send the unsorted1 array as a string to the JavaScript back } private: // function to create a random number between min and max int32_t rangeRandomAlg (int32_t min, int32_t max) { int32_t num = max - min + 1; int32_t remainder = RAND_MAX % num; int32_t x; do { x = rand(); } while (x >= RAND_MAX - remainder); return min + x % num; } // function to create arrays with random numbers void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum) { for(int32_t i = 0; i <= arrayElements; i++) { if (i < arrayElements/2) { unsorted1[i] = rangeRandomAlg(minNum, maxNum); } else { unsorted2[i] = rangeRandomAlg(minNum, maxNum); } } } // convert the arrays to string void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) { for (int32_t i = 0; i <= arraySize; ++i){ arrayString+= std::to_string(array[i]); if (i != arraySize) { arrayString+= ','; } } } }; /*** my functions and data for the cpp code to integrate end here ***/ class job1 : public pp::Module { public: job1() : pp::Module() {} virtual ~job1() {} virtual pp::Instance* CreateInstance(PP_Instance instance) { return new job1Instance(instance); } }; namespace pp { Module* CreateModule() { return new job1(); } } // namespace pp 变量= outRes1数组变量作为字符串发送到index.html中的JavaScript代码文件。您必须单独编写.nmf和index.html文件。如果有人想看到我的代码,那些具有此PNaCl模块的基本代码和工作代码的文件应该给我写评论,我会将这些代码发布到。