Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit 1a23e1b

Browse files
committed
event callback
1 parent df048d9 commit 1a23e1b

File tree

8 files changed

+139
-7
lines changed

8 files changed

+139
-7
lines changed

cpp/include/AppCenter.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <utils.hpp>
3+
4+
namespace Interop {
5+
namespace AppCenter {
6+
7+
void setTrackEventCallback(void (*callback)(const char *str));
8+
void setTrackEventCallback(void (*callback)(const char *str, const char *data));
9+
10+
void trackEvent(std::string event);
11+
void trackEvent(std::string event, Properties properties);
12+
13+
} // namespace AppCenter
14+
} // namespace Interop

cpp/include/utils.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <string>
3+
#include <unordered_map>
4+
5+
using Properties = std::unordered_map<std::string, std::string>;
6+
7+
inline std::string toJson(Properties &properties) {
8+
std::string json = "{";
9+
for (auto &p : properties) {
10+
json += "\"" + p.first + "\":\"" + p.second + "\",";
11+
}
12+
json.pop_back();
13+
json += "}";
14+
return json;
15+
}

cpp/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ myApp_inc = include_directories('include')
33
myApp_sources = [
44
'src/main.cpp',
55
'src/dialogBox.cpp',
6+
'src/AppCenter.cpp',
67
]
78
myApp_args = []
89
myApp_link = []

cpp/src/AppCenter.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <AppCenter.hpp>
2+
#include <iostream>
3+
4+
namespace Interop {
5+
namespace AppCenter {
6+
void (*trackEventCallback)(const char *str) = nullptr;
7+
void (*trackEventExtraCallback)(const char *str, const char *data) = nullptr;
8+
9+
void setTrackEventCallback(void (*callback)(const char *str)) {
10+
trackEventCallback = callback;
11+
}
12+
void setTrackEventCallback(void (*callback)(const char *str,
13+
const char *data)) {
14+
trackEventExtraCallback = callback;
15+
}
16+
17+
void trackEvent(std::string event) {
18+
if (trackEventCallback) {
19+
trackEventCallback(event.c_str());
20+
} else {
21+
std::cerr << "Interop::AppCenter::trackEventCallback is nullptr\n";
22+
}
23+
}
24+
25+
void trackEvent(std::string event, Properties properties) {
26+
if (trackEventExtraCallback) {
27+
trackEventExtraCallback(event.c_str(), toJson(properties).c_str());
28+
} else {
29+
std::cerr << "Interop::AppCenter::trackEventExtraCallback is nullptr\n";
30+
}
31+
}
32+
33+
} // namespace AppCenter
34+
} // namespace Interop

cpp/src/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <AppCenter.hpp>
12
#include <dialogBox.hpp>
23
#include <iostream>
34

@@ -7,11 +8,26 @@
78
// your main code
89
int main() {
910
std::cout << "C++ Powered\n";
11+
Interop::AppCenter::trackEvent("[Native][Init]", {{"Hello", "from C++"}});
1012
showDialogBox("C++ Powered", "Hello from C++!");
1113
}
1214
// the entry point of your program
1315
// you need to export this function
1416

1517
extern "C" {
1618
myAppAPI void dllEntry() { main(); }
19+
// this will setup the callbacks needed by the C++ code
20+
// in order to call the AppCenter.trackEvent() function
21+
myAppAPI void setupAppCenterCallbacks(
22+
void (*trackEventCallback)(const char *str),
23+
void (*trackEventExtraCallback)(const char *str, const char *data)) {
24+
Interop::AppCenter::setTrackEventCallback(trackEventCallback);
25+
Interop::AppCenter::setTrackEventCallback(trackEventExtraCallback);
26+
}
27+
28+
// on this we call the funtion provided by the parameter
29+
myAppAPI void dllCallback(void (*callback)()) {
30+
std::cout << "Callback from C++\n";
31+
callback();
32+
}
1733
}

csharp/csharp-cross-platform.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<Compile Remove="source\Program.cs" />
12+
<Compile Remove="source\interop\AppCenter.cs" />
1213
</ItemGroup>
1314

1415
<ItemGroup>

csharp/source/Program.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace csharp;
22

3-
// DllImport
4-
using System.Runtime.InteropServices;
3+
using interop;
54

65
// Microsoft AppCenter SDK
76
using Microsoft.AppCenter;
@@ -22,10 +21,8 @@ static void Main()
2221
// this is only for demo purposes
2322
System.Console.WriteLine("App Center Powered.");
2423
AppCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Crashes));
25-
dllEntry();
24+
AppCenterCpp.setup();
25+
AppCenterCpp.initApp();
2626
}
27-
28-
// define the DLL entry point from c++ dll (void dllEntry() from myApp.dll)
29-
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
30-
public static extern void dllEntry();
27+
3128
}

csharp/source/interop/AppCenter.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace csharp.interop;
2+
3+
// DllImport
4+
using System.Runtime.InteropServices;
5+
6+
// Microsoft AppCenter SDK
7+
using Microsoft.AppCenter.Analytics;
8+
9+
// JSON conversion
10+
using Newtonsoft.Json;
11+
12+
static class AppCenterCpp
13+
{
14+
15+
// setup the required data and callbacks for C++
16+
// it is require that the AppCenter SDK is initialized before calling this function
17+
public static void setup()
18+
{
19+
setupAppCenterCallbacks(trackEvent, trackEventFunc);
20+
}
21+
// initialize the C++ dll entry point
22+
// it is required that the setup is called before calling this function
23+
public static void initApp()
24+
{
25+
dllEntry();
26+
}
27+
28+
// the C++ dll entry point
29+
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
30+
private static extern void dllEntry();
31+
[DllImport("myApp.dll")]
32+
private static extern void setupAppCenterCallbacks(TrackEventDelegate normal, TrackEventExtraDelegate extra);
33+
34+
private static void trackEventFunc(string eventName)
35+
{
36+
Console.WriteLine("trackEventFunc: " + eventName);
37+
Analytics.TrackEvent(eventName);
38+
}
39+
private static void trackEventFunc(string eventName, string properties)
40+
{
41+
Console.WriteLine("trackEventFunc: " + eventName + " " + properties);
42+
// convert the properties string(json) to a dictionary
43+
var props = JsonConvert.DeserializeObject<Dictionary<string, string>>(properties);
44+
Analytics.TrackEvent(eventName, props);
45+
}
46+
47+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
48+
private delegate void TrackEventDelegate(string eventName);
49+
50+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
51+
private delegate void TrackEventExtraDelegate(string eventName, string eventProperties);
52+
private static TrackEventDelegate trackEvent = trackEventFunc;
53+
private static TrackEventExtraDelegate trackEventExtra = trackEventFunc;
54+
}

0 commit comments

Comments
 (0)