Commit 0f4166db authored by Felix Morgner's avatar Felix Morgner
Browse files

week12: add lecture examples

parent 683119a7
package ch.hsr.cpla;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
public interface CplaLib extends Library {
CplaLib INSTANCE = (CplaLib) Native.load("cpla", CplaLib.class);
public static class Point extends Structure implements Structure.ByValue {
public int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
protected List<String> getFieldOrder() {
return List.of("x", "y");
}
}
public static class Unicorn extends Pointer {
Unicorn(String name) {
super(Pointer.nativeValue(INSTANCE.createUnicorn(name)));
}
void dispose() {
INSTANCE.disposeUnicorn(this);
}
}
Pointer createUnicorn(String name);
void disposeUnicorn(Unicorn instance);
void printUnicorn(Unicorn unicorn);
void printPoint(Point point);
void printInt(int prefix);
Pointer getData(IntByReference size);
void freeData(Pointer data);
}
......@@ -44,7 +44,7 @@ public interface Julia extends Library {
IntByReference size = new IntByReference();
Pointer dumped = INSTANCE.dump(this, size);
byte[] data = dumped.getByteArray(0, size.getValue());
Native.free(Pointer.nativeValue(dumped));
INSTANCE.disposeBitmapData(dumped);
return data;
}
}
......@@ -56,6 +56,8 @@ public interface Julia extends Library {
int getSize(Image image);
Pointer dump(Image image, IntByReference size);
void disposeBitmapData(Pointer data);
void render(Image image, Window window, double exponent, double kReal, double kImaginary, int iterations);
}
......@@ -37,6 +37,10 @@ char* dump(image image, uint32_t *size) {
return byteData;
}
void disposeBitmapData(char * data) {
delete[] data;
}
void render(image image, window window, double exponent, double k_real, double k_imaginary, uint32_t iterations) {
auto realWindow = julia::Window { { window.x, window.y }, window.size };
auto realK = std::complex<double> { k_real, k_imaginary };
......
......@@ -11,6 +11,7 @@ image createImage(uint32_t size);
void disposeImage(image image);
uint32_t getSize(image image);
char * dump(image image, uint32_t * size);
void disposeBitmapData(char * data);
struct window {
double x;
......
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.so.debug.379419582">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.so.debug.379419582" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings>
<externalSetting>
<entry flags="VALUE_WORKSPACE_PATH" kind="includePath" name="/w12_01_wizard_c_lib"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="libraryPath" name="/w12_01_wizard_c_lib/Debug"/>
<entry flags="RESOLVED" kind="libraryFile" name="w12_01_wizard_c_lib" srcPrefixMapping="" srcRootPath=""/>
</externalSetting>
</externalSettings>
<extensions>
<extension id="org.eclipse.cdt.core.PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactExtension="so" artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.sharedLib" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.sharedLib,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.so.debug.379419582" name="Debug" parent="cdt.managedbuild.config.gnu.mingw.so.debug">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.so.debug.379419582." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.so.debug.2031050289" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.so.debug">
<targetPlatform binaryParser="org.eclipse.cdt.core.PE;org.eclipse.cdt.core.MachO64;org.eclipse.cdt.core.Cygwin_PE;org.eclipse.cdt.core.ELF;org.eclipse.cdt.core.GNU_ELF" id="cdt.managedbuild.target.gnu.platform.mingw.so.debug.893633671" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.mingw.so.debug"/>
<builder buildPath="${workspace_loc:/w12_01_wizard_c_lib}/Debug" id="cdt.managedbuild.tool.gnu.builder.mingw.base.1105131569" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.so.debug.1678099773" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.so.debug">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1132412813" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.288540665" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug.1164427830" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug">
<option id="gnu.cpp.compiler.mingw.so.debug.option.optimization.level.2136521166" name="Optimization Level" superClass="gnu.cpp.compiler.mingw.so.debug.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
<option id="gnu.cpp.compiler.mingw.so.debug.option.debugging.level.1980546391" name="Debug Level" superClass="gnu.cpp.compiler.mingw.so.debug.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.dialect.flags.1898826707" name="Other dialect flags" superClass="gnu.cpp.compiler.option.dialect.flags" useByScannerDiscovery="true" value="-std=c++1y" valueType="string"/>
<option id="gnu.cpp.compiler.option.other.other.665980922" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -Wno-attributes -fvisibility=hidden" valueType="string"/>
<option id="gnu.cpp.compiler.option.dialect.std.751968866" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.other.pic.2142613752" name="Position Independent Code (-fPIC)" superClass="gnu.cpp.compiler.option.other.pic" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.compiler.option.include.paths.560241823" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/w12_01_wizard_c_lib}&quot;"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1288945770" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.debug.1964462416" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.debug">
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.mingw.so.debug.option.optimization.level.219492737" name="Optimization Level" superClass="gnu.c.compiler.mingw.so.debug.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option id="gnu.c.compiler.mingw.so.debug.option.debugging.level.1135472543" name="Debug Level" superClass="gnu.c.compiler.mingw.so.debug.option.debugging.level" useByScannerDiscovery="false" value="gnu.c.debugging.level.max" valueType="enumerated"/>
<option id="gnu.c.compiler.option.dialect.std.373815453" name="Language standard" superClass="gnu.c.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.c.compiler.dialect.default" valueType="enumerated"/>
<option id="gnu.c.compiler.option.misc.other.1957835130" name="Other flags" superClass="gnu.c.compiler.option.misc.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -fvisibility=hidden" valueType="string"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.399929755" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug.1666772647" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug">
<option defaultValue="true" id="gnu.c.link.mingw.so.debug.option.shared.1212215486" name="Shared (-shared)" superClass="gnu.c.link.mingw.so.debug.option.shared" valueType="boolean"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.debug.169874354" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.debug">
<option defaultValue="true" id="gnu.cpp.link.mingw.so.debug.option.shared.190551720" name="Shared (-shared)" superClass="gnu.cpp.link.mingw.so.debug.option.shared" useByScannerDiscovery="false" valueType="boolean"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1378148703" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.so.release.989961675">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.so.release.989961675" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings>
<externalSetting>
<entry flags="VALUE_WORKSPACE_PATH" kind="includePath" name="/w12_01_wizard_c_lib"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="libraryPath" name="/w12_01_wizard_c_lib/Release"/>
<entry flags="RESOLVED" kind="libraryFile" name="w12_01_wizard_c_lib" srcPrefixMapping="" srcRootPath=""/>
</externalSetting>
</externalSettings>
<extensions>
<extension id="org.eclipse.cdt.core.PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactExtension="dll" artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.sharedLib" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.sharedLib,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.so.release.989961675" name="Release" parent="cdt.managedbuild.config.gnu.mingw.so.release">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.so.release.989961675." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.so.release.502905323" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.so.release">
<targetPlatform binaryParser="org.eclipse.cdt.core.PE;org.eclipse.cdt.core.MachO64;org.eclipse.cdt.core.Cygwin_PE;org.eclipse.cdt.core.ELF;org.eclipse.cdt.core.GNU_ELF" id="cdt.managedbuild.target.gnu.platform.mingw.so.release.1565389793" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.mingw.so.release"/>
<builder buildPath="${workspace_loc:/w12_01_wizard_c_lib}/Release" id="cdt.managedbuild.tool.gnu.builder.mingw.base.1013909175" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.so.release.594831329" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.so.release">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.204856120" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.1332717815" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release.2013812445" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release">
<option id="gnu.cpp.compiler.mingw.so.release.option.optimization.level.2086294100" name="Optimization Level" superClass="gnu.cpp.compiler.mingw.so.release.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
<option id="gnu.cpp.compiler.mingw.so.release.option.debugging.level.1872981465" name="Debug Level" superClass="gnu.cpp.compiler.mingw.so.release.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.dialect.flags.38980438" name="Other dialect flags" superClass="gnu.cpp.compiler.option.dialect.flags" useByScannerDiscovery="true" value="-std=c++1y" valueType="string"/>
<option id="gnu.cpp.compiler.option.other.other.1465502851" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -Wno-attributes" valueType="string"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1929130382" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.1323788528" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release">
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.mingw.so.release.option.optimization.level.293479776" name="Optimization Level" superClass="gnu.c.compiler.mingw.so.release.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option id="gnu.c.compiler.mingw.so.release.option.debugging.level.120590819" name="Debug Level" superClass="gnu.c.compiler.mingw.so.release.option.debugging.level" useByScannerDiscovery="false" value="gnu.c.debugging.level.none" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1196523650" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.so.release.1164071544" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.so.release">
<option defaultValue="true" id="gnu.c.link.mingw.so.release.option.shared.446840131" name="Shared (-shared)" superClass="gnu.c.link.mingw.so.release.option.shared" valueType="boolean"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.release.78859625" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.release">
<option defaultValue="true" id="gnu.cpp.link.mingw.so.release.option.shared.1744268561" name="Shared (-shared)" superClass="gnu.cpp.link.mingw.so.release.option.shared" valueType="boolean"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1363515339" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="w12_01_wizard_c_lib.cdt.managedbuild.target.gnu.mingw.so.1324306490" name="Shared Library" projectType="cdt.managedbuild.target.gnu.mingw.so"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.379419582;cdt.managedbuild.config.gnu.mingw.so.debug.379419582.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug.1164427830;cdt.managedbuild.tool.gnu.cpp.compiler.input.1288945770">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.989961675;cdt.managedbuild.config.gnu.mingw.so.release.989961675.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.1323788528;cdt.managedbuild.tool.gnu.c.compiler.input.1196523650">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.379419582;cdt.managedbuild.config.gnu.mingw.so.debug.379419582.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.debug.1964462416;cdt.managedbuild.tool.gnu.c.compiler.input.399929755">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.989961675;cdt.managedbuild.config.gnu.mingw.so.release.989961675.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release.2013812445;cdt.managedbuild.tool.gnu.cpp.compiler.input.1929130382">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope"/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
</cproject>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>w12_01_wizard_c_lib</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
#ifndef WIZARD_H_
#define WIZARD_H_
#if defined(_WIN32)
#define WIZARD_EXPORT_DLL __declspec(dllexport)
#else
//assumes -fvisibility=hidden
#define WIZARD_EXPORT_DLL __attribute__ ((visibility ("default")))
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Wizard * wizard;
typedef struct Wizard const * cwizard;
typedef struct Error * error;
WIZARD_EXPORT_DLL
char const *error_message(error error);
WIZARD_EXPORT_DLL
void error_dispose(error error);
WIZARD_EXPORT_DLL
wizard createWizard(char const *name, error *out_error);
WIZARD_EXPORT_DLL
void disposeWizard(wizard toDispose);
WIZARD_EXPORT_DLL
char const *doMagic(wizard w, char const *wish, error *out_error);
WIZARD_EXPORT_DLL
void learnSpell(wizard w, char const *spell);
WIZARD_EXPORT_DLL
void mixAndStorePotion(wizard w, char const *potion);
WIZARD_EXPORT_DLL
char const *wizardName(cwizard w);
#ifdef __cplusplus
}
#endif
#endif /* WIZARD_H_ */
#include "Wizard.h"
#include "WizardHidden.h"
#include "WizardIngredients.h"
#include <cassert>
#include <stdexcept>
#include <string>
extern "C"{
struct Error {
std::string message;
};
WIZARD_EXPORT_DLL
const char* error_message(error error)
{
return error->message.c_str();
}
WIZARD_EXPORT_DLL
void error_dispose(error error)
{
delete error;
}
}
template<typename Fn>
bool translateExceptions(error* out_error, Fn&& fn)
try {
fn();
return true;
}
catch (const std::exception& e) {
*out_error = new Error { e.what() };
return false;
}
catch (...) {
*out_error = new Error { "Unknown internal error" };
return false;
}
extern "C" {
struct Wizard { // C linkage trampolin
Wizard(char const *name)
:wiz{name}{}
unseen::Wizard wiz;
};
WIZARD_EXPORT_DLL
wizard createWizard(const char* name, error *out_error) {
wizard result=nullptr;
translateExceptions(out_error,[&]{
result = new Wizard{name};
});
return result;
}
WIZARD_EXPORT_DLL
void disposeWizard(wizard const toDispose) {
delete toDispose;
}
WIZARD_EXPORT_DLL
char const* doMagic(wizard w, char const * const wish, error *out_error) {
// will return static string!
char const *result="";
translateExceptions(out_error,[&]{
if (!w) throw std::logic_error{"null wizard"};
result = w->wiz.doMagic(wish);
});
return result;
}
WIZARD_EXPORT_DLL
void learnSpell(wizard w, char const * const spell) {
assert(w);
w->wiz.learnSpell(spell);
}
WIZARD_EXPORT_DLL
void mixAndStorePotion(wizard w, char const* const potion) {
assert(w);
w->wiz.mixAndStorePotion(potion);
}
WIZARD_EXPORT_DLL
char const* wizardName(cwizard const w) {
assert(w);
return w->wiz.getName();
}
}
#include "WizardHidden.h"
#include "WizardIngredients.h"
#include <algorithm>
#include <stdexcept>
namespace unseen {
std::string Wizard::searchForSpell(std::string const &wish) {
auto const spell = find(begin(books), end(books), wish);
return spell!=end(books)?*spell:"";
}
Potion Wizard::mixPotion(std::string const &recipe) {
auto const potion = find(begin(potions), end(potions), recipe);
return potion!=end(potions)?*potion:"";
}
void Wizard::castSpell(std::string spell) {
}
void Wizard::applyPotion(Potion phial) {
}
char const * Wizard::doMagic(std::string const &wish) {
auto spell=searchForSpell(wish);
if (! spell.empty()){
castSpell(spell);
return "wootsh";
}
auto potion=mixPotion(wish);
if (!potion.empty()){
applyPotion(potion);
return "zapp";
}
throw std::logic_error{"magic failed"};
}
void Wizard::learnSpell(const std::string& newspell) {
this->books.push_back(newspell);
}
void Wizard::mixAndStorePotion(const std::string& potion) {
this->potions.push_back(potion);
}
}
#ifndef WIZARDHIDDEN_H_
#define WIZARDHIDDEN_H_
#include <string>
#include <vector>
#include "WizardIngredients.h"
namespace unseen{
struct Wizard {
private:
std::string const name;
MagicWand const wand;
std::vector<Spell> books;
std::vector<Potion> potions;
std::string searchForSpell(std::string const &wish);
Potion mixPotion(std::string const &recipe);
void castSpell(Spell spell);
void applyPotion(Potion phial);
public:
Wizard(std::string name="Rincewind"):name{name}, wand{}{}
char const * doMagic(std::string const &wish);
void learnSpell(std::string const &newspell);
void mixAndStorePotion(std::string const &potion);
char const * getName() const { return name.c_str() ; }
};
}
#endif /* WIZARDHIDDEN_H_ */
#ifndef WIZARDINGREDIENTS_H_
#define WIZARDINGREDIENTS_H_
using Potion=std::string;
struct MagicWand{};
using Spell=std::string;
#endif /* WIZARDINGREDIENTS_H_ */
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>w12_01_wizard_c_lib_test</name>
<comment></comment>
<projects>
<project>w12_05_wizard_c_lib</project>
<project>w12_01_wizard_c_lib</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>ch.hsr.ifs.cute.ui.cutenature</nature>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
/*********************************************************************************
* This file is part of CUTE.
*
* CUTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CUTE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CUTE. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2006-2009 Peter Sommerlad
*
*********************************************************************************/
#ifndef CUTE_H_
#define CUTE_H_
// all CUTE includes for writing tests and suites
#include "cute_base.h"
#include "cute_equals.h"
#include "cute_relops.h"
#include "cute_data_driven.h"
#include "cute_throws.h"
#include "cute_suite.h"
#include "cute_repeated_test.h"
#include "cute_suite_test.h"
#include "cute_test_incarnate.h"
#include "cute_test.h"
#include "cute_testmember.h"
#include "cute_version.h"
#endif /*CUTE_H_*/
/*********************************************************************************
* This file is part of CUTE.
*