Skip to content

Native Library - Brings Java productivity into C++ program

License

Notifications You must be signed in to change notification settings

tiny-express/native

Repository files navigation

Native Library

Join Gitter Chat Channel - Build status Build Status Coverage status Support Platform

Native Library provides a set of Java packages are re-written in C++

  • Blazing fast - small footprint - no dependency required
  • Cross platform - outstanding performance powered by C++ STL
  • Integrate directly with any C/C++ project with productivity of Java
  • Prevents nearly all memory leak and segmentation fault by destructors
  • Packages are strictly tested with unit tests, clean with Valgrind and follow Oracle documentation
  • Feel free to use in your commercial products and welcome for contributions

Getting started

Docker

$ docker pull foodtiny/native:latest

Installation

$ git clone https://github.com/tiny-express/native.git
$ cd native
$ cmake . -DCMAKE_BUILD_TYPE=Release && make native -j4 && sudo make install

Post installation

Ubuntu

$ sudo ldconfig

Examples

HelloWorld.cpp

#include <native/library.hpp>

class MainApplication {
public:
    static void main(Array<String> arguments) {
        HashMap<String, String> hashMap;
        var index = 0;
        for (var argument : arguments) {
            hashMap.put(String::valueOf(index++), argument);
        }
        hashMap.forEach([](String key, String value) {
            System::out::println(String::format("Key is %s - value is %s", key, value));
            return true;
        });
    }
};

int main(int argc, char **argv) {
    return Application(MainApplication::main, argc, argv);
}

Compile your source and link with native library

$ g++ -c -o main.o HelloWorld.cpp
$ g++ -o main main.o -lnative -lstdc++
$ ./main one two three

Output:

Key is 3 - Value is three
Key is 2 - Value is two
Key is 0 - Value is ./main
Key is 1 - Value is one

Test memory clean up with Valgrind

$ valgrind ./main one two three

Output:

==14685== Memcheck, a memory error detector
==14685== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14685== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==14685== Command: ./main one two three
==14685==
Key is 3 - Value is three
Key is 2 - Value is two
Key is 0 - Value is ./main
Key is 1 - Value is one
==14685==
==14685== HEAP SUMMARY:
==14685==     in use at exit: 0 bytes in 0 blocks
==14685==   total heap usage: 2,971 allocs, 2,971 frees, 380,013 bytes allocated
==14685==
==14685== All heap blocks were freed -- no leaks are possible
==14685==
==14685== For counts of detected and suppressed errors, rerun with: -v
==14685== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

More examples can be found here Support unit test by default via ApplicationTest here - Powered by CUnit

Documentation

Documentation

Differences

This library provides Java classes in C++ so its syntax are friendly for both programming languages:

  • Namespace - Package
// Java
System.out.println("Java");
// C++
System::out::println("C++");
  • Array
// Java
byte[] byes = {};
// C++
Array<byte> bytes = {};

Primitive Data Types

All data types are implemented and ready to use in C++ Application

  • char - Java.Lang.Character
  • byte - Java.Lang.Byte
  • string - Java.Lang.String
  • short - Java.Lang.Short
  • int - Java.Lang.Integer
  • long - Java.Lang.Long
  • float - Java.Lang.Float
  • double - Java.Lang.Double
  • boolean - Java.Lang.Boolean
  • enum - Java.Lang.Enum