Download - Cross Platform App Development with C++

Transcript
Page 1: Cross Platform App Development with C++

Joan Puig Sanz

Cross Platform App Development with C++

Page 2: Cross Platform App Development with C++

Apps for all the platformsPossible solutionsWhy C++?C++11LibrariesTips and tricksConclusion

Agenda

2

Page 3: Cross Platform App Development with C++

The problem:apps for all the platformsBecause making good apps is easy…

…or not

3

Page 4: Cross Platform App Development with C++

Apps for all the platformsAndroidiOSMacOSWPWindows 8BB10Ubuntu Touch…

4

Page 5: Cross Platform App Development with C++

Apps for all the platformsA love story

PhonegapTitaniumAdobe AirXamarin…

5

Page 6: Cross Platform App Development with C++

Apps for all the platformsA true love story

Not native UISlow performanceFor complex apps you

need to make custom components.

You depend on a company

Poor user experience

6

Page 7: Cross Platform App Development with C++

Good user experience

UX

UISmooth

7

Page 8: Cross Platform App Development with C++

Using C++

8

Page 9: Cross Platform App Development with C++

Cross platform language.Better Performance.Very mature language.Lots of libraries.Easy communication with native language:

Objective-CJavaC#

Why C++?

9

Page 10: Cross Platform App Development with C++

C++ don’t want to be alone

• Full native UI and UXUI/UX

• Bindings to communicate with C++

• e.g. Objective-C++ and JNI

C++ bindings

• Common functionalitiesC++ Core

10

Page 11: Cross Platform App Development with C++

C++ 11 features

11

Page 12: Cross Platform App Development with C++

The compiler deduce the actual type of a variable that is being declared from its initializer.

C++ 11::Features - Auto

auto i = 42; // i is an intauto l = 42LL; // l is an long longauto p = new Foo(); // p is a foo*

std::map<std::string, std::vector<int>> map;for(auto it = begin(map); it != end(map); ++it) {}

12

Page 13: Cross Platform App Development with C++

Support the "foreach" paradigm of iterating over collections.

C++ 11::Features - Range-based for loops

std::map<std::string, std::vector<int>> map;std::vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);map["one"] = v;

for(const auto& kvp : map) { std::cout << kvp.first << std::endl;

for(auto v : kvp.second) std::cout << v << std::endl;}

13

Page 14: Cross Platform App Development with C++

Before:Implicitly converted to integral typesExport their enumerators in the surrounding

scope, which can lead to name collisionsNo user-specified underlying type

C++ 11::Features - Strongly-typed enums

enum class Options {None, One, All};Options o = Options::All;

14

Page 15: Cross Platform App Development with C++

unique_ptr: Ownership of a memory resource it is not shared, but it can be transferred to another unique_ptr

shared_ptr: Ownership of a memory resource should be shared

weak_ptr: Holds a reference to an object managed by a shared_ptr, but does not contribute to the reference count; it is used to break dependency cycles.

C++ 11::Features - Smart pointers

15

Page 16: Cross Platform App Development with C++

Powerful feature borrowed from functional programming.

You can use lambdas wherever a function object or a functor or a std::function is expected

C++ 11::Features - Lambdas

std::function<int(int)> lfib = [&lfib](int n) { return n < 2 ? 1 : lfib(n-1) + lfib(n-2);};

16

Page 17: Cross Platform App Development with C++

static_assert performs an assertion check at compile-time

C++ 11::Features - static_assert and type traits

template <typename T1, typename T2>auto add(T1 t1, T2 t2) -> decltype(t1 + t2){ return t1 + t2;}

template <typename T1, typename T2>auto add(T1 t1, T2 t2) -> decltype(t1 + t2){ static_assert(std::is_integral<T1>::value, "Type T1 must be integral"); static_assert(std::is_integral<T2>::value, "Type T2 must be integral");

return t1 + t2;}

std::cout << add(1, 3.14) << std::endl;std::cout << add("one", 2) << std::endl;

17

Page 18: Cross Platform App Development with C++

Final and overviewnon-member begin() and end()Initializer listsObject construction improvementUnrestricted unionsUser-defined literals…

C++ 11::Features - More

18

Page 19: Cross Platform App Development with C++

Complementing C++Not reinventing the wheel

19

Page 20: Cross Platform App Development with C++

C++ 11 is missing utilitiesA standard way to make requestsDealing with XML or JSONBig numbersSecurityMath…

Solution: Use it with other libraries

Complementing C++

20

Page 21: Cross Platform App Development with C++

C++ 11::Libs – For making appsBoostJuceQt…

21

Page 22: Cross Platform App Development with C++

C++ 11::Libs - BoostSupports:

License:"AS IS”

Big communityLots of modules

22

Page 23: Cross Platform App Development with C++

C++ 11::Libs - JuceSupports:

No WP (for now)License:

Core: "AS IS”Other modules: GPL and commercial license

Very well written codeEasy to use

23

Page 24: Cross Platform App Development with C++

C++ 11::Libs - QtSupports:

License: GPL v3, LGPL v2 and a commercial license

Big communityLots of modules

24

Page 25: Cross Platform App Development with C++

Tips and tricks To make easier our life

25

Page 26: Cross Platform App Development with C++

Tips and tricks – JNI calling C from Java

Implement the C methodJNIEXPORT void Java_com_example_MyClass_doSomethingNative (JNIEnv *env, jobject obj, jstring s){ const char* const utf8 = env->GetStringUTFChars (s, nullptr); CharPointer_UTF8 utf8CP (utf8); String cppString (utf8CP); env->ReleaseStringUTFChars (s, utf8); doSomethingWithCppString (cppString);}

private native void doSomethingNative(String str);

Declare the native method on java

Do not forget to release the java objects! env->Release: The maxim number of java references is 512

Some libraries offers you utilities to parse common java objects to Cpp objects

26

Page 27: Cross Platform App Development with C++

Tips and tricks – JNI calling java from C++

JNIEnv* env = getEnv();

jclass myJavaClass = env->GetObjectClass (myJavaObject);jmethodID myJavaMethod = env->GetMethodID(myJavaClass , ”methodName", "([I;Z)Ljava/lang/String");

jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean);

// Do something

env->DeleteLocalRef (jresult);env->DeleteLocalRef (myJavaClass);

Declare the native method on java

Do not forget to delete the local java referencesenv->DeleteLocalRef: The maxim number of

java references is 512 27

Page 28: Cross Platform App Development with C++

Tips and tricks – JNI binding C++ objects

public class Foo { private static native void destroyCppInstanceNative(long ref); private native long newCppInstanceNative(); private native String getStringNative(long ref); private long _ref = 0; public Foo() { _ref = newCppInstanceNative(); } public String getString() { return getStringNative(_ref); } public void destroy() { destroyCppInstanceNative(_ref); }}

Java

28

Page 29: Cross Platform App Development with C++

Tips and tricks – JNI binding C++ objects

JNIEXPORT long Java_com_example_Foo_newCppInstanceNative(JNIEnv *env, jobject obj) { Foo* newFoo = new Foo(); int64 ref = reinterpret_cast<int64>(newFoo); return ref;}

JNIEXPORT jstring Java_com_example_Foo_getStringNative(JNIEnv *env, jobject obj, long ref) { Foo* foo = reinterpret_cast<Foo*>(ref); jstring jStringResult = env->NewStringUTF(foo->getString().toUTF8()); return jStringResult;}

JNIEXPORT void Java_com_example_Foo_destroyCppInstanceNative(JNIEnv *env, jobject obj, int64 ref) { Foo* foo = reinterpret_cast<Foo*>(ref); delete foo;}

C

29

Page 30: Cross Platform App Development with C++

Tips and tricks – C++ and Objective-C++ On Objective-C++ is very easy to use C++ The extension of the Objective-C++ file is .mm Try to do not add imports of C++ code on the Objective-C

headers If you add a C++ import in your Objective-C header you

will force other classes to be Objective-C++ (.mm) instead of Objective-C (.m)

@implementation JSPFooFoo fooObject;-(id) init { self = [super init]; if (self) { } return self;}

- (NSString*) stringFromCpp{ NSString* result = [NSString stringWithUTF8String:fooObject.getString().toRawUTF8()]; return result;}@end 30

Page 31: Cross Platform App Development with C++

Establish some code conventions with your coworkers

Review each others codeIf you do not find the C++ code that you need

just create some interfaces and implement them in the native platform (java, Objective-c, C# … )

Code as if you where creating a library, if you have more apps to develop this code will help you

C++ is not a read-only code, so do not put the blame on it

Tips and tricks

31

Page 32: Cross Platform App Development with C++

Remember to initialize all the values in the constructor, even numbers…

To speed up the compilation time use unity buildsCheck out djinni: tool for generating cross-

language type declarations and interface bindings.

Tips and tricks

dropbox/djinni

32

Page 33: Cross Platform App Development with C++

ResultsWhat do we get at the end?

33

Page 34: Cross Platform App Development with C++

ProsDifferent apps sharing the same core and with

native UI/UXBetter performanceFaster developmentEasier maintenance

ConsNeed to learn a new languageAndroid apps will be bigger (code compiled for

different architectures)Can be fixed distributing specific apk per each

architecture

Results

34

Page 35: Cross Platform App Development with C++

?@joanpuigsanz