I'm reading this page in my effort to determine if Clojurescript is appropriate for my use case.
I'm interested in using Clojurescript to create the Javascript code that will run in Qt 5 where JS is a native language that can access C++ functions exposed in Qt to the JS layer.
These functions will have names that can be called from Javascript within Qt, but of course they will not exist outside the Qt project, thus creating Javascript code via Clojurescript requires that the Clojurescript can call these functions even though they are outside the scope of the Clojurescript environment.
Does the "extern" method allow for Clojurescript to do this, while maintaining both successful compilation as well as no "munging" of those function names so they can operate in my Qt context fine?
Yes. If you do any optimizations but advanced, externs are not necessary.
If you plan on using the advanced mode, you will need to specify the externs, either manually, using a plugin like lein-externs, or a combination of both.
Another cool thing to do would be generating a full externs from the API docs and publishing them for other people to benefit too. Shouldn't be too hard.
Related
We have a lot of business logic written in cross-platform C++. We want to write cross-platform UI for our application and use this business logic to create whole cross-platfrom application.
Is it possible to expose native module purely written in C++ to react-native?
We don't want to create wrappers around C++ code in native language (Java or Objective-C). Such wrappers will add more complexity it will make debugging and investigation much harder.
I am also looking for a way to do this directly in C++ without writing JNI for Android and Obj-C for iOS. I have found the CxxNativeModule class in the react native source. See the sample implementation SampleCxxModule.
The last thing to figure out is how to register that module in C++. In the JNI for React Native Android, implementation of NativeModule says
NativeModules whose implementation is written in C++ must not provide
any Java code (so they can be reused on other platforms), and instead
should register themselves using CxxModuleWrapper.
Then implementation of CxxModuleWrapper says
This does nothing interesting, except avoid breaking existing code.
So, this may help you get started. The last thing to figure out is how to register a CxxNativeModule so that it can be used in JS.
It seems as though you would need to find the code which is os dependent and write different versions of this code for different operating systems.
Here is a link which suggests how this might be done:
How do I check OS with a preprocessor directive?
This may be a totally noob question, but I've been experimenting with openGL in clojure via LWJGL, and while there are plenty of resources for learning both graphics programming and LWJGL, I've found it frustrating to port them to clojure. All of the different static methods representing openGL calls are associated with different java classes (ie. GL11, GL15, etc.), which presents no real problem in java as they can all be imported via something like "import static org.lwjgl.opengl.GL11.*". So I was wondering if there's a way to do the same in clojure (like the way "use" does for clojure namespaces)? And if not, is there an easier way to figure out which class a given method is associated with than stopping to search through the api for each one?
The functionality you describe is not included in clojure.core. It was included in the, now deprecated, clojure-contrib project, but including that as a dependency to your project may cause unintended dependency conflicts.
The code for the import-static macro is located in the old clojure-contrib github repository.
https://github.com/richhickey/clojure-contrib/blob/master/src/main/clojure/clojure/contrib/import_static.clj
You should be able to include the single macro in your project without issue.
I intend to use Squirrel as a scripting language for my C++ application. Naturally, there should be an API for interfacing with the C++ code (for things such as accessing and modifying attributes in my C++ program). This API would consist of a bunch of classes, enums and functions.
While there are utilities like Sqrat that make binding single C++ functions to a Squirrel VM the matter of a single line of code, that is still not satifactory: It would require me to create both the C++ classes with their functions to actually do all the interfacing work, and then I'd have to maintain all the bindings to make those C++ functions known in my scripts as well. My intention is to remove this double maintenance overhead.
So what I want is a tool that would simply take the already existing header file containing all my C++ classes and functions and generate API registration calls from this file. And while we are at it, of course it would be nice to automatically generate a documentation for every function as well (doesn't matter if it's HTML or just a Squirrel script that contains of function definitions + comments or whatever).
I know there's SWIG, but it doesn't have a binding Squirrel, and that's not exactly what I would be looking for anyway - after all, I need to create C++ wraper code, not Squirrel code. I've seen Flex, but I'm not sure whether that's what I'm looking for, either. So is there any tool that would do what I want (automate the creation of wrapper code and API documentation from a C/C++ header)? Otherwise, I guess I might have to write my own little C++ parser that can parse simple function and class definitions.
I want to write a native application that can be extended with plugins, perferabily in the form of dynamic libraries. I have an idea of what to do, but I would like some ideas, especially best practice tips on what to do and not to do. I worked with similar things on java and php, so I hope I don't bring any bad habits in to my C++.
I'm thinking of allowing developers to implement certain functions like "on_recieve_data(App* app, void* data)" and my application will load all the plugins and call their on_recieve_data function with a pointer to itself (dlsym?).
There are a few things that I consider very important for plugins:
Language support
If you want to reach the most number of platforms/languages/compilers then you should write the plugin interface in C and not in C++. The plugin developers can still write their functions in C++, of course, it is just the interface that is C. The problem is that each C++ compiler mangles symbol names in its own way, so if you use C++ you will be forcing plugin developers to use the same compilers and tools that you use. On the other side, there is only one way to export C symbols, so using a C interface for the plugin will allow developers to pick whatever tools they like, and as long as they can produce standard .so/.dll libraries they'll be fine.
Memory allocation
In some platforms there are problems when memory allocated by the application is released by a DLL or viceversa. If the plugin has functions that are supposed to allocate memory, then make sure you also require the plugin to provide a corresponding function to release that memory. Likewise, if the plugin can call a function in the application to allocate memory, you should also expose a release function for that memory.
Versioning
It is likely that you will have to revise the plugin API after plugins have been written. So your application needs to be prepared to load plugins developed for an older version. You should require an 'init' function in the plugin that the application calls to determine what version of the API the plugin implements and any other information the app might need to know, like the plugin type (if there are different types), what is implemented and what isn't, etc.
Also you have to be very careful when you have to revise the plugin API. You can't change existing functions, since that would break older plugins. Instead you will need to add alternative versions of those functions that have the improvements. Then the problem comes of how to name the new version of an existing function. Typically they'll get the same name plus some suffix ('Ex', a number, etc.). I haven't seen this problem solved in a way that I like.
Likewise, you have to take precautions for structures that are passed between the application and plugins. A common approach is to make the first member of all structures the size of the structure. This works as sort of a versioning mechanism, so that the application can determine what the structure looks like from its size.
Here are a few links that might be of interest:
C-Pluff, a general purpose plug-in framework in C (MIT license)
lighttpd's plugin.h header file
This page has a discussion on how to implement a plugin architecture under Mac OS X, including a short overview of a how to create a C interface for plugins.
Blender is an interesting one. The application is written in C++, but plugins are written in Python. Not a bad idea really, it makes it a lot easier for developer to write plugins.
There are plenty of applications written in scripting languages that support plugins (Wordpress, Drupal, Django, and many more). You can look at any of those that are closer to the kind of application you are writing for ideas.
I believe that this post -> Design Pattern for implementing plugins in your application? does answer your question I guess. It has a lot of refernce for plugin model.
Perhapse, the Eclipse architecture can serve as example:
http://www.eclipse.org/articles/Article-Plug-in-architecture/plugin_architecture.html
I'm pretty there is a book from Eclipse's creator, but I can't remember neither the author nor the name of the book.
I'm working on a project which needs an embedded DSL to fullfill its expected requirements.
The DSL would be user defined event based. Here goes a mockup of the desired syntax:
user-defined-event-1 {
// event body
}
user-defined-event-2 {
// event body
}
Probably, most similar language I know based on events is LSL (from Second Life).
So, after reading other similar questions on SO, I would like to ask for the best embeddable scripting engine (Ruby, Lua, Python, etc) on C++ (I work in Qt) which allows me to create this DSL.
In my project, I would test that the script properly uses the DSL syntax (at least one event defined) and give the user all the power of the underlying scripting engine and, if possible, Qt.
It is not a requirement for the embedded language to work with Qt. It can be isolated, but it would be nice to have some integration too.
There's at least a few Qt-Lua bindings out there. Lua can somewhat do the syntax you've shown above; specifically, {} indicates a table (associative array) in Lua, and if you are only passing an anonymous table to a function, you don't need parentheses:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> function LengthOfTable(t) print(#t) end
> LengthOfTable ({"a","b","c"})
3
> LengthOfTable {"a","b","c"}
3
Whether Lua is actually the best for your application, depends on your application, of course. Either way, Lua is very easy (IMO) to embed in C or C++.
You could look at embeddable javascript, through Google's V8 project, which is written in C++.
http://code.google.com/apis/v8/intro.html
Qt comes with the QtScript scripting module. It uses an ECMAScript based langauge (like javascript).
Tcl comes fairly close to your proposed syntax:
proc user-defined-event-1 {} {
# event body
puts "Hello World"
}
proc defines a procedure, and the extra {} braces are used for arguments. In a tcl shell, procedures can be dynamically typed in line-by-line, copied and pasted, or loaded from a file. They can also be redefined by simply reloading them.
I've never tried it but there is PyQt.
I believe boost::python is pretty easy to implement. I hear there are some python-Qt solutions too.
You seem to have very specific requirements for picking a generic DSL. You may want to try a generic DSL library (e.g. Boost.Proto) rather than a prexisting-embedded language.
For embedding a DSL within your app, I recommend ANTLR. I have used ANTLR over the years, the latest being within a JDBC driver for Cassandra. You might want to try version 4 which has a C++ runtime. Version 3 was problematic with Qt over a collision with the keyword emit.