Skip to content

Commit

Permalink
adding C bindings for FilesystemModuleResolver (#44)
Browse files Browse the repository at this point in the history
* 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
Kbz-8 and SirLynix authored Sep 18, 2024
1 parent ac9ac60 commit 4e71c2b
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/CNZSL/CNZSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#ifndef CNZSL_CNZSL_H
#define CNZSL_CNZSL_H

#include <CNZSL/FilesystemModuleResolver.h>
#include <CNZSL/GlslWriter.h>
#include <CNZSL/LangWriter.h>
#include <CNZSL/Module.h>
#include <CNZSL/Parser.h>
#include <CNZSL/Serializer.h>
#include <CNZSL/SpirvWriter.h>

#endif /* CNZSL_CNZSL_H */
37 changes: 37 additions & 0 deletions include/CNZSL/FilesystemModuleResolver.h
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
3 changes: 3 additions & 0 deletions include/CNZSL/WriterStates.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <CNZSL/Config.h>
#include <CNZSL/DebugLevel.h>
#include <CNZSL/FilesystemModuleResolver.h>
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -31,6 +32,8 @@ CNZSL_API void nzslWriterStatesEnableOptimization(nzslWriterStates* statesPtr, n
CNZSL_API void nzslWriterStatesEnableSanitization(nzslWriterStates* statesPtr, nzslBool enable);
CNZSL_API void nzslWriterStatesSetDebugLevel(nzslWriterStates* statesPtr, nzslDebugLevel debugLevel);

CNZSL_API void nzslWriterStatesSetModuleResolver_Filesystem(nzslWriterStates* statesPtr, const nzslFilesystemModuleResolver* resolverPtr);

CNZSL_API void nzslWriterStatesSetOption_bool(nzslWriterStates* statesPtr, nzslOptionHash optionHash, nzslBool value);
CNZSL_API void nzslWriterStatesSetOption_f32(nzslWriterStates* statesPtr, nzslOptionHash optionHash, nzslFloat32 value);
CNZSL_API void nzslWriterStatesSetOption_i32(nzslWriterStates* statesPtr, nzslOptionHash optionHash, int32_t value);
Expand Down
105 changes: 105 additions & 0 deletions src/CNZSL/FilesystemModuleResolver.cpp
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();
}
}
20 changes: 20 additions & 0 deletions src/CNZSL/Structs/FilesystemModuleResolver.hpp
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
7 changes: 7 additions & 0 deletions src/CNZSL/WriterStates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <CNZSL/WriterStates.h>
#include <CNZSL/FilesystemModuleResolver.h>
#include <CNZSL/Structs/WriterStates.hpp>
#include <CNZSL/Structs/FilesystemModuleResolver.hpp>
#include <NZSL/Ast/Option.hpp>
#include <array>

Expand Down Expand Up @@ -46,6 +48,11 @@ extern "C"
statesPtr->debugLevel = s_debugLevels[debugLevel];
}

CNZSL_API void nzslWriterStatesSetModuleResolver_Filesystem(nzslWriterStates* statesPtr, const nzslFilesystemModuleResolver* resolverPtr)
{
statesPtr->shaderModuleResolver = resolverPtr->resolver;
}

CNZSL_API void nzslWriterStatesSetOption_bool(nzslWriterStates* statesPtr, nzslOptionHash optionHash, nzslBool value)
{
statesPtr->optionValues[optionHash] = (value != 0);
Expand Down

0 comments on commit 4e71c2b

Please sign in to comment.