Clangformat for preprocessor directives - objective-c

Using Clangformat
I currently have the following method:
- (void)myMethod
{
#if DEBUG
[ClassMethod enableLogging];
#endif
}
I wish for it to be displayed as so
- (void)myMethod
{
#if DEBUG
[ClassMethod enableLogging];
#endif
}
Any ideas?
LVVM Research Link

Related

How to use general C/C++ code in KMP common source set?

I'm building an app for Android and iOS and I want to reuse as much code as possible. I have some generic C code (an algorithm) that doesn't include any system library. Is it possible to expose it to my common Kotlin source set using cinterop or any other tool?
My build.gradle.kts:
plugins {
id("com.android.library")
kotlin("multiplatform")
kotlin("native.cocoapods")
}
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(21)
targetSdkVersion(29)
}
sourceSets.all {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
java.srcDirs("src/androidMain/java")
res.srcDirs("src/androidMain/res")
}
}
version = "1.0"
kotlin {
android()
ios()
cocoapods {
// Configure fields required by CocoaPods.
summary = "..."
homepage = "..."
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "SharedModule"
}
// Workaround for ios platform imports to work on Android Studio
// iosX64("ios")
sourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
}
sourceSets["androidMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
}
sourceSets.all {
languageSettings.progressiveMode = true
}
}
Not automatically. On the JVM side you'll need to use JNI to talk to the C code, and on the native side you'll need to use Kotlin cinterop. JNI and Kotlin cinterop will have an interface to the C code that's very similar to each other, but not the same. To expose that to common code, you'll need to write a common API layer that delegates to JNI code on the JVM and Kotlin cinterop code on native.
Wrapping very similar platform-specific API's is pretty straightforward once you get used to it. Ideally, you could automatically wrap them, but right now you can't. I gave a talk that discusses some techniques for this: https://vimeo.com/371460823
Below follows a simple example of how to call c code within Kotlin:
cCaller.kt
class cCaller {
init {
System.loadLibrary("cCode")
} external fun callCFunction()
}
In your code:
fun main() {
cCaller().callCFunction()
}
cCode.c
#include <stdio.h>
#include "cCaller.h"JNIEXPORT void JNICALL Java_cCaller_callCFunction(JNIEnv *env, jobject obj) {
// YOUR CODE HERE
return;
}
Notice that the function callCFunction is prefixed with Java_ and cCaller_
Also notice the #include "cCaller.h" added to the second line of cCode.c - we need to create this file:
cCaller.h
#include <jni.h>#ifndef _Included_NativeSample
#define _Included_NativeSample
#ifdef __cplusplus
extern "C" {
#endifJNIEXPORT void JNICALL Java_cCaller_callCFunction(JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endif
Compile
gcc cCode.c -o libcCode.so -shared -fPIC -I <jdk_path>/include -I <jdk_path>/include/linux

Android NDK/JNI torch state control

How can I change state of the camera flash throgh JNI function? I am looking to be able to have ON/OFF state control, just like in Java CameraManager.setTorchMode(cameraId, state); method. I've tried to search for it in native camera API ,but no success. Here's what I have done so far:
#include <jni.h>
#include <assert.h>
#include <jni.h>
#include <pthread.h>
#include <android/native_window_jni.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraManager.h>
#include <android/asset_manager.h>
#include "messages-internal.h"
JNIEXPORT void JNICALL
Java_com_android_rxjava_flashlightflicker_MainActivity_flasher(JNIEnv *env, jobject instance) {
ACameraIdList *cameraIdList = NULL;
const char *selectedCameraId = NULL;
ACameraManager *cameraManager = ACameraManager_create();
camera_status_t camera_status = ACAMERA_OK;
camera_status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList);
/// Camera status not ok
if (camera_status != ACAMERA_OK) {
LOGE("Camera is bad id: %d \n", camera_status);
return;
}
// There is no camera
if (cameraIdList->numCameras < 1 ) {
LOGE("Camera is not present on the device.");
return;
}
selectedCameraId = cameraIdList->cameraIds[0];
ACameraMetadata *cameraMetedata = NULL;
ACameraManager_getCameraCharacteristics(cameraManager, selectedCameraId, &cameraMetedata);
// ACaptureSessionOutput_create()
}
I also tried to look in asset manager but no success, can anybody experienced with NDK camera give me a hand with it?
Thanks in advance!
This method is only available in Java API. You could access it through JNI, but IMO it would be easier and safer to write a wrapper static method in Java and have this wrapper called from your C++ code.

Where can I find libdecodeqr.lib?

I am creating a QR code scanner program in C++ using the library called libdecodeqr.
I downloaded all the files from https://github.com/josephholsten/libdecodeqr and I modified one of the test programs. However, I keep getting the error "unresolved external symbol". It's because I have not linked the libraries.
But I can't find libdecodeqr.lib anywhere! It's not included in the GitHub package and it's nowhere online!
In the instructions on the GitHub page, it says to add decodeqr.h, qrtypes.h, qrerror.h and libdecodeqr.lib to my environment. Other than the .lib file, I have added all those files to my environment.
Where can I find libdecodeqr.lib?
Also, here is the sample program I modified and am testing:
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/decodeqr.h"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/qrerror.h"
#include "C:/Users/Asish/Documents/libdecodeqr-master/libdecodeqr/qrtypes.h"
#include "bitstream.h"
using namespace cv;
int main(int argc, char *argv[])
{
namedWindow("src", 1);
//
// load image
//
Mat src_old = imread("qrTest.png", 1);
IplImage* src = new IplImage(src_old);
imshow("src", src_old);
//
// show version info
//
printf("libdecodeqr version %s\n", qr_decoder_version());
//
// initialize
//
QrDecoderHandle decoder = qr_decoder_open();
//
// do decode using default parameter
//
short stat = qr_decoder_decode_image(decoder, src);
printf("STATUS=%04x\n", stat);
//
// get QR code header
//
QrCodeHeader header;
if (qr_decoder_get_header(decoder, &header)){
//
// get QR code text
// To null terminate, a buffer size is larger than body size.
//
char *buf = new char[header.byte_size + 1];
qr_decoder_get_body(decoder, (unsigned char *)buf, header.byte_size + 1);
printf("%s\n", buf);
}
//
// finalize
//
qr_decoder_close(decoder);
puts("");
puts("Hit any key to end.");
cvWaitKey(0);
destroyAllWindows();
cvReleaseImage(&src);
return(0);
}
You are downloading the source code from GitHub, which means that you also need to create a new VisualStudio C++ project to compile your own libdecodeqr.lib (or dll). This will require you also install OpenCV in your computer.

Turn on autocompletion in macros

I would love to have autocomplete in macros like for example,
#ifdef DEBUG
static void print_debug_info(object obj) {
...
}
#endif
Using a near-default configuration of AppCode, it doesn't appear this is enabled. Is there anyway to enable it?

Does Xamarin have an #if or #ifdef for determining the platform?

For example, #ifdef iOS, #ifdef android, or the like. If there is #if, that would be even better.
iOS:
#if __MOBILE__
Console.WriteLine ("__MOBILE__ is defined");
#endif
#if __IOS__
Console.WriteLine ("__IOS__ is defined");
#endif
Android:
#if __MOBILE__
Console.WriteLine ("__MOBILE__ is defined");
#endif
#if __ANDROID__
Console.WriteLine ("__ANDROID__ is defined");
#endif
https://bugzilla.xamarin.com/show_bug.cgi?id=6459#c12
xamarin documentation
Yes it does, I don't know, if Xamarin.iOS provides its own symbols, as I'm new to Xamarin and I actually do not use Xamarin.iOS, but you can define your own symbols.
Right click on the project an open the project options. In the "Compiler" settings you can lookup existing flags and create add new ones.
For example here are the symbols that are shipped with Xamarin.Android:
DEBUG;__MOBILE__;__ANDROID__;
The flags should be available immediately after you have defined them.