-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding C bindings for FilesystemModuleResolver (#44)
* adding serialization bindings to the CNZSL API * Update Serializer.cpp * Update Serializer.cpp * adding C bindings for FilesystemModuleResolver * fixing naming style --------- Co-authored-by: Jérôme Leclercq <lynix680@gmail.com>
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright (C) 2024 kbz_8 ( contact@kbz8.me ) | ||
This file is part of the "Nazara Shading Language - C Binding" project | ||
For conditions of distribution and use, see copyright notice in Config.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef CNZSL_FILESYSTEM_MODULE_RESOLVER_H | ||
#define CNZSL_FILESYSTEM_MODULE_RESOLVER_H | ||
|
||
#include <CNZSL/Config.h> | ||
#include <CNZSL/Module.h> | ||
|
||
#include <stddef.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
typedef struct nzslFilesystemModuleResolver nzslFilesystemModuleResolver; | ||
|
||
CNZSL_API nzslFilesystemModuleResolver* nzslFilesystemModuleResolverCreate(void); | ||
CNZSL_API void nzslFilesystemModuleResolverDestroy(nzslFilesystemModuleResolver* resolverPtr); | ||
CNZSL_API const char* nzslFilesystemModuleResolverGetLastError(const nzslFilesystemModuleResolver* resolverPtr); | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverRegisterModule(nzslFilesystemModuleResolver* resolverPtr, const nzslModule* module); | ||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen); | ||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromSource(nzslFilesystemModuleResolver* resolverPtr, const char* source, size_t sourceLen); | ||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (C) 2024 kbz_8 ( contact@kbz8.me ) | ||
// This file is part of the "Nazara Shading Language - C Binding" project | ||
// For conditions of distribution and use, see copyright notice in Config.hpp | ||
|
||
#include <CNZSL/FilesystemModuleResolver.h> | ||
#include <CNZSL/Structs/FilesystemModuleResolver.hpp> | ||
#include <CNZSL/Structs/Module.hpp> | ||
#include <fmt/format.h> | ||
#include <string_view> | ||
#include <filesystem> | ||
#include <memory> | ||
#include <cassert> | ||
|
||
extern "C" | ||
{ | ||
CNZSL_API nzslFilesystemModuleResolver* nzslFilesystemModuleResolverCreate(void) | ||
{ | ||
nzslFilesystemModuleResolver* resolver = new nzslFilesystemModuleResolver; | ||
resolver->resolver = std::make_shared<nzsl::FilesystemModuleResolver>(); | ||
return resolver; | ||
} | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverDestroy(nzslFilesystemModuleResolver* resolverPtr) | ||
{ | ||
delete resolverPtr; | ||
} | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverRegisterModule(nzslFilesystemModuleResolver* resolverPtr, const nzslModule* module) | ||
{ | ||
assert(resolverPtr); | ||
|
||
try | ||
{ | ||
resolverPtr->resolver->RegisterModule(module->module); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModule failed: {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModule failed with unknown error"; | ||
} | ||
} | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen) | ||
{ | ||
assert(resolverPtr); | ||
|
||
try | ||
{ | ||
resolverPtr->resolver->RegisterModule(std::filesystem::path{ sourcePath, sourcePath + sourcePathLen }); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleFromFile failed: {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleFromFile failed with unknown error"; | ||
} | ||
} | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromSource(nzslFilesystemModuleResolver* resolverPtr, const char* source, size_t sourceLen) | ||
{ | ||
assert(resolverPtr); | ||
|
||
try | ||
{ | ||
resolverPtr->resolver->RegisterModule(std::string_view{ source, sourceLen }); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleFromSource failed: {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleFromSource failed with unknown error"; | ||
} | ||
} | ||
|
||
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen) | ||
{ | ||
assert(resolverPtr); | ||
|
||
try | ||
{ | ||
resolverPtr->resolver->RegisterModuleDirectory(std::filesystem::path{ sourcePath, sourcePath + sourcePathLen }); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleDirectory failed: {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleDirectory failed with unknown error"; | ||
} | ||
} | ||
|
||
CNZSL_API const char* nzslFilesystemModuleResolverGetLastError(const nzslFilesystemModuleResolver* resolverPtr) | ||
{ | ||
assert(resolverPtr); | ||
return resolverPtr->lastError.c_str(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (C) 2024 kbz_8 ( contact@kbz8.me ) | ||
// This file is part of the "Nazara Shading Language - C Binding" project | ||
// For conditions of distribution and use, see copyright notice in Config.hpp | ||
|
||
#pragma once | ||
|
||
#ifndef CNZSL_STRUCTS_FILESYSTEM_MODULE_RESOLVER_HPP | ||
#define CNZSL_STRUCTS_FILESYSTEM_MODULE_RESOLVER_HPP | ||
|
||
#include <NZSL/FilesystemModuleResolver.hpp> | ||
#include <string> | ||
#include <memory> | ||
|
||
struct nzslFilesystemModuleResolver | ||
{ | ||
std::shared_ptr<nzsl::FilesystemModuleResolver> resolver; | ||
std::string lastError; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters