Skip to content

Feature/callback from cpp to java #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions AS3/.gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
app/src/main/java/com/sureshjoshi/core
app/src/main/java/com/sureshjoshi/core
15 changes: 0 additions & 15 deletions AS3/.idea/checkstyle-idea.xml

This file was deleted.

6 changes: 0 additions & 6 deletions AS3/.idea/encodings.xml

This file was deleted.

18 changes: 0 additions & 18 deletions AS3/.idea/gradle.xml

This file was deleted.

67 changes: 0 additions & 67 deletions AS3/.idea/misc.xml

This file was deleted.

9 changes: 0 additions & 9 deletions AS3/.idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions AS3/.idea/runConfigurations.xml

This file was deleted.

4 changes: 4 additions & 0 deletions AS3/app/src/main/cpp/ISeePlusPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>
#include <vector>
#include "ISeePlusPlusInterface.h"

/**
* @file
Expand Down Expand Up @@ -54,6 +55,9 @@ namespace SJ {
*/
virtual std::vector<std::string> Sort(const std::vector<std::string> &inStrings)=0;

virtual void returnViaCallback(const std::string input, ISeePlusPlusInterface * callbackInterface)=0;

}; // class ISeePlusPlus

} // namespace SJ
#endif // __SJ_ISEEPLUSPLUS_H__
15 changes: 15 additions & 0 deletions AS3/app/src/main/cpp/ISeePlusPlusInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _ISEEPLUSPLUSINTERFACE_H__
#define _ISEEPLUSPLUSINTERFACE_H__

#include <string>

//namespace SJ {

struct ISeePlusPlusInterface {
virtual ~ISeePlusPlusInterface() {}

virtual void print(const std::string &text) = 0;
};
//}

#endif // _ISEEPLUSPLUSINTERFACE_H__
6 changes: 5 additions & 1 deletion AS3/app/src/main/cpp/SeePlusPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ std::vector<std::string> SeePlusPlus::Sort(const std::vector<std::string> &inStr
std::vector<std::string> sortedList(inStringList);
std::sort(std::begin(sortedList), std::end(sortedList));
return sortedList;
}
}

void SeePlusPlus::returnViaCallback(const std::string input, ISeePlusPlusInterface * callbackInterface) {
callbackInterface->print(input);
}
2 changes: 2 additions & 0 deletions AS3/app/src/main/cpp/SeePlusPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace SJ {
*/
virtual std::vector<std::string> Sort(const std::vector<std::string> &inStrings);

virtual void returnViaCallback(const std::string input, ISeePlusPlusInterface * callbackInterface);

}; // class SeePlusPlus
} // namespace SJ
#endif // __SJ_SEEPLUSPLUS_H__
6 changes: 5 additions & 1 deletion AS3/app/src/main/cpp/SeePlusPlus.i
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* File : SeePlusPlus.i */
%module SeePlusPlus_Wrapper
%module(directors="1") SeePlusPlus_Wrapper

/* Anything in the following section is added verbatim to the .cxx wrapper file */
%{
#include "SeePlusPlus.h"
#include "ISeePlusPlusInterface.h"
%}

%feature("director") ISeePlusPlusInterface;

/* This will allow us to iterate through arrays defined by STL containers */
%include "std_string.i"
%include "std_vector.i"
Expand All @@ -20,3 +23,4 @@ namespace std {
/* For Java, it seems we need the file of interest and all files up the inheritance tree */
%include "ISeePlusPlus.h"
%include "SeePlusPlus.h"
%include "ISeePlusPlusInterface.h"
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.widget.TextView;

import com.sureshjoshi.core.ISeePlusPlusInterface;
import com.sureshjoshi.core.IntVector;
import com.sureshjoshi.core.SeePlusPlus;
import com.sureshjoshi.core.StringVector;
Expand Down Expand Up @@ -41,6 +42,9 @@ public class MainActivity extends Activity {
@BindView(R.id.textview_android_studio_ndk_integration)
TextView mTextviewAndroidStudioNdkIntegration;

@BindView(R.id.textview_android_studio_ndk_callback)
TextView mTextviewAndroidStudioNdkCallback;

@OnClick(R.id.button_refresh)
void onClick() {
runNativeFunctions();
Expand All @@ -62,6 +66,17 @@ void runNativeFunctions() {
mTextviewRandomNumbers.setText(runRandomNumbers());
mTextviewSortedStrings.setText(runSortString());
// mTextviewAndroidStudioNdkIntegration.setText(runAndroidStudioNativeString());
getStringFromCppCallback();
}

private void getStringFromCppCallback() {
mCpp.returnViaCallback("test string", new ISeePlusPlusInterface() {

@Override
public void print(String text) {
mTextviewAndroidStudioNdkCallback.setText(text);
}
});
}

String runMultiplyInts() {
Expand Down
7 changes: 7 additions & 0 deletions AS3/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@
android:layout_height="wrap_content"
android:textSize="20sp" />

<TextView
android:id="@+id/textview_android_studio_ndk_callback"
android:text="@string/string_callback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />

</LinearLayout>
1 change: 1 addition & 0 deletions AS3/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="sorted_strings">Becomes this list of sorted strings: </string>
<string name="action_settings">Settings</string>
<string name="refresh">Refresh</string>
<string name="string_callback">String returned from C++ via callback</string>
</resources>