diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2018-12-21 11:00:28 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-21 11:00:28 -0500 |
| commit | efa2c8f41aa5cd2c27990fd9b57ea0eff06976e7 (patch) | |
| tree | 4c1a4d8e6e71e81c42cfbf315610e380d75527bf /source | |
| parent | b5bda9b3d155234be079debe6997cbc900773cf2 (diff) | |
Feature/remove app context (#765)
* Remove AppContext. Use StdChannels to hold writers, and TestToolUtil to hold test tool specific functionality.
* StdChannels -> StdWriters
* getStdOut -> getOut, getStdError -> getError
Diffstat (limited to 'source')
| -rw-r--r-- | source/core/core.vcxproj | 6 | ||||
| -rw-r--r-- | source/core/core.vcxproj.filters | 18 | ||||
| -rw-r--r-- | source/core/slang-app-context.cpp | 67 | ||||
| -rw-r--r-- | source/core/slang-app-context.h | 60 | ||||
| -rw-r--r-- | source/core/slang-std-writers.cpp | 43 | ||||
| -rw-r--r-- | source/core/slang-std-writers.h | 44 | ||||
| -rw-r--r-- | source/core/slang-test-tool-util.cpp | 21 | ||||
| -rw-r--r-- | source/core/slang-test-tool-util.h | 25 | ||||
| -rw-r--r-- | source/core/slang-writer.h | 2 | ||||
| -rw-r--r-- | source/slang/slang.vcxproj | 2 | ||||
| -rw-r--r-- | source/slangc/main.cpp | 17 |
11 files changed, 159 insertions, 146 deletions
diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj index 5dad716fb..bebf7350c 100644 --- a/source/core/core.vcxproj +++ b/source/core/core.vcxproj @@ -182,7 +182,6 @@ <ClInclude Include="list.h" /> <ClInclude Include="platform.h" /> <ClInclude Include="secure-crt.h" /> - <ClInclude Include="slang-app-context.h" /> <ClInclude Include="slang-byte-encode-util.h" /> <ClInclude Include="slang-cpu-defines.h" /> <ClInclude Include="slang-free-list.h" /> @@ -192,9 +191,11 @@ <ClInclude Include="slang-object-scope-manager.h" /> <ClInclude Include="slang-random-generator.h" /> <ClInclude Include="slang-shared-library.h" /> + <ClInclude Include="slang-std-writers.h" /> <ClInclude Include="slang-string-slice-pool.h" /> <ClInclude Include="slang-string-util.h" /> <ClInclude Include="slang-string.h" /> + <ClInclude Include="slang-test-tool-util.h" /> <ClInclude Include="slang-writer.h" /> <ClInclude Include="smart-pointer.h" /> <ClInclude Include="stream.h" /> @@ -204,7 +205,6 @@ </ItemGroup> <ItemGroup> <ClCompile Include="platform.cpp" /> - <ClCompile Include="slang-app-context.cpp" /> <ClCompile Include="slang-byte-encode-util.cpp" /> <ClCompile Include="slang-free-list.cpp" /> <ClCompile Include="slang-io.cpp" /> @@ -212,9 +212,11 @@ <ClCompile Include="slang-object-scope-manager.cpp" /> <ClCompile Include="slang-random-generator.cpp" /> <ClCompile Include="slang-shared-library.cpp" /> + <ClCompile Include="slang-std-writers.cpp" /> <ClCompile Include="slang-string-slice-pool.cpp" /> <ClCompile Include="slang-string-util.cpp" /> <ClCompile Include="slang-string.cpp" /> + <ClCompile Include="slang-test-tool-util.cpp" /> <ClCompile Include="slang-writer.cpp" /> <ClCompile Include="stream.cpp" /> <ClCompile Include="text-io.cpp" /> diff --git a/source/core/core.vcxproj.filters b/source/core/core.vcxproj.filters index c5c1fc733..dd33b61f9 100644 --- a/source/core/core.vcxproj.filters +++ b/source/core/core.vcxproj.filters @@ -45,9 +45,6 @@ <ClInclude Include="secure-crt.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="slang-app-context.h"> - <Filter>Header Files</Filter> - </ClInclude> <ClInclude Include="slang-byte-encode-util.h"> <Filter>Header Files</Filter> </ClInclude> @@ -75,6 +72,9 @@ <ClInclude Include="slang-shared-library.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="slang-std-writers.h"> + <Filter>Header Files</Filter> + </ClInclude> <ClInclude Include="slang-string-slice-pool.h"> <Filter>Header Files</Filter> </ClInclude> @@ -84,6 +84,9 @@ <ClInclude Include="slang-string.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="slang-test-tool-util.h"> + <Filter>Header Files</Filter> + </ClInclude> <ClInclude Include="slang-writer.h"> <Filter>Header Files</Filter> </ClInclude> @@ -107,9 +110,6 @@ <ClCompile Include="platform.cpp"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="slang-app-context.cpp"> - <Filter>Source Files</Filter> - </ClCompile> <ClCompile Include="slang-byte-encode-util.cpp"> <Filter>Source Files</Filter> </ClCompile> @@ -131,6 +131,9 @@ <ClCompile Include="slang-shared-library.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="slang-std-writers.cpp"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="slang-string-slice-pool.cpp"> <Filter>Source Files</Filter> </ClCompile> @@ -140,6 +143,9 @@ <ClCompile Include="slang-string.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="slang-test-tool-util.cpp"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="slang-writer.cpp"> <Filter>Source Files</Filter> </ClCompile> diff --git a/source/core/slang-app-context.cpp b/source/core/slang-app-context.cpp deleted file mode 100644 index 2b3f32cbc..000000000 --- a/source/core/slang-app-context.cpp +++ /dev/null @@ -1,67 +0,0 @@ - -#include "slang-app-context.h" - -#include "slang-writer.h" - -namespace Slang -{ - -/* static */AppContext* AppContext::s_singleton = nullptr; - - -/* static */AppContext* AppContext::getDefault() -{ - static AppContext* s_context = nullptr; - - if (!s_context) - { - static FileWriter s_stdError(stderr, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); - static FileWriter s_stdOut(stdout, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); - - static AppContext s_contextVar; - s_context = &s_contextVar; - - s_context->setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, &s_stdError); - s_context->setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, &s_stdOut); - } - return s_context; -} - -/* static */AppContext* AppContext::initDefault() -{ - AppContext* context = getDefault(); - setSingleton(context); - return context; -} - -/* static */int AppContext::getReturnCode(SlangResult res) -{ - if (SLANG_SUCCEEDED(res)) - { - return 0; - } - else if (res == SLANG_E_INTERNAL_FAIL) - { - return -1; - } - return 1; -} - -void AppContext::setRequestWriters(SlangCompileRequest* request) -{ - for (int i = 0; i < SLANG_WRITER_CHANNEL_COUNT_OF; ++i) - { - if (m_replaceWriterFlags & (1 << i)) - { - spSetWriter(request, SlangWriterChannel(i), m_writers[i]); - } - } -} - -void AppContext::configureRequest(SlangCompileRequest* request) -{ - setRequestWriters(request); -} - -} - diff --git a/source/core/slang-app-context.h b/source/core/slang-app-context.h deleted file mode 100644 index 9f1eb306c..000000000 --- a/source/core/slang-app-context.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef SLANG_APP_CONTEXT_H -#define SLANG_APP_CONTEXT_H - -#include "slang-writer.h" -#include "../../slang-com-ptr.h" - -namespace Slang -{ - -#ifdef SLANG_SHARED_LIBRARY_TOOL -# define SLANG_SHARED_LIBRARY_TOOL_API SLANG_EXTERN_C SLANG_DLL_EXPORT -#else -# define SLANG_SHARED_LIBRARY_TOOL_API -#endif - -/* A structure to hold general state shared across an application */ -class AppContext -{ -public: - - ISlangWriter * getWriter(SlangWriterChannel chan) const { return m_writers[chan]; } - void setWriter(SlangWriterChannel chan, ISlangWriter* writer) { m_writers[chan] = writer; } - - /// Make modifications to the request - void configureRequest(SlangCompileRequest* request); - - void setRequestWriters(SlangCompileRequest* request); - - void setReplaceWriterFlagsAll() { setReplaceWriterFlags((1 << SLANG_WRITER_CHANNEL_COUNT_OF) - 1); } - void setReplaceWriterFlags(int flags) { m_replaceWriterFlags = flags; } - int getReplaceWriterFlags() const { return m_replaceWriterFlags; } - - /// Ctor - AppContext() : m_replaceWriterFlags(0) {} - - /// Initialize a default context - static AppContext* initDefault(); - - static AppContext* getDefault(); - - static AppContext* getSingleton() { return s_singleton; } - static void setSingleton(AppContext* context) { s_singleton = context; } - - static WriterHelper getStdError() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_ERROR); } - static WriterHelper getStdOut() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT); } - static WriterHelper getDiagnostic() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_DIAGNOSTIC); } - - static int getReturnCode(SlangResult res); - -protected: - - ComPtr<ISlangWriter> m_writers[SLANG_WRITER_CHANNEL_COUNT_OF]; - int m_replaceWriterFlags; ///< Bit for each writer - - static AppContext* s_singleton; -}; - -} - -#endif diff --git a/source/core/slang-std-writers.cpp b/source/core/slang-std-writers.cpp new file mode 100644 index 000000000..963af9ef5 --- /dev/null +++ b/source/core/slang-std-writers.cpp @@ -0,0 +1,43 @@ + +#include "slang-std-writers.h" + +namespace Slang +{ + +/* static */StdWriters* StdWriters::s_singleton = nullptr; + +/* static */StdWriters* StdWriters::getDefault() +{ + static StdWriters* s_context = nullptr; + + if (!s_context) + { + static FileWriter s_stdError(stderr, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); + static FileWriter s_stdOut(stdout, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); + + static StdWriters s_contextVar; + s_context = &s_contextVar; + + s_context->setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, &s_stdError); + s_context->setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, &s_stdOut); + } + return s_context; +} + +/* static */StdWriters* StdWriters::initDefault() +{ + StdWriters* context = getDefault(); + setSingleton(context); + return context; +} + +void StdWriters::setRequestWriters(SlangCompileRequest* request) +{ + for (int i = 0; i < SLANG_WRITER_CHANNEL_COUNT_OF; ++i) + { + spSetWriter(request, SlangWriterChannel(i), m_writers[i]); + } +} + +} + diff --git a/source/core/slang-std-writers.h b/source/core/slang-std-writers.h new file mode 100644 index 000000000..d580842eb --- /dev/null +++ b/source/core/slang-std-writers.h @@ -0,0 +1,44 @@ +#ifndef SLANG_STD_WRITERS_H +#define SLANG_STD_WRITERS_H + +#include "slang-writer.h" +#include "../../slang-com-ptr.h" + +namespace Slang +{ + +/* Holds standard writers for the channels */ +class StdWriters +{ +public: + ISlangWriter * getWriter(SlangWriterChannel chan) const { return m_writers[chan]; } + void setWriter(SlangWriterChannel chan, ISlangWriter* writer) { m_writers[chan] = writer; } + + /// Set the writers on the SlangCompileRequest + void setRequestWriters(SlangCompileRequest* request); + + /// Ctor + StdWriters() {} + + /// Initialize a default context + static StdWriters* initDefault(); + + static StdWriters* getDefault(); + + static StdWriters* getSingleton() { return s_singleton; } + static void setSingleton(StdWriters* context) { s_singleton = context; } + + static WriterHelper getError() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_ERROR); } + static WriterHelper getOut() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT); } + static WriterHelper getDiagnostic() { return getSingleton()->getWriter(SLANG_WRITER_CHANNEL_DIAGNOSTIC); } + +protected: + + ComPtr<ISlangWriter> m_writers[SLANG_WRITER_CHANNEL_COUNT_OF]; + + static StdWriters* s_singleton; +}; + +} + +#endif diff --git a/source/core/slang-test-tool-util.cpp b/source/core/slang-test-tool-util.cpp new file mode 100644 index 000000000..7ecfacefa --- /dev/null +++ b/source/core/slang-test-tool-util.cpp @@ -0,0 +1,21 @@ + +#include "slang-test-tool-util.h" + +namespace Slang +{ + +/* static */int TestToolUtil::getReturnCode(SlangResult res) +{ + if (SLANG_SUCCEEDED(res)) + { + return 0; + } + else if (res == SLANG_E_INTERNAL_FAIL) + { + return -1; + } + return 1; +} + +} + diff --git a/source/core/slang-test-tool-util.h b/source/core/slang-test-tool-util.h new file mode 100644 index 000000000..615bbe10d --- /dev/null +++ b/source/core/slang-test-tool-util.h @@ -0,0 +1,25 @@ +#ifndef SLANG_TEST_TOOL_UTIL_H +#define SLANG_TEST_TOOL_UTIL_H + +#include "slang-std-writers.h" + +namespace Slang { + +#ifdef SLANG_SHARED_LIBRARY_TOOL +# define SLANG_TEST_TOOL_API SLANG_EXTERN_C SLANG_DLL_EXPORT +#else +# define SLANG_TEST_TOOL_API +#endif + +/* Utility functions for 'test tools' */ +struct TestToolUtil +{ + typedef SlangResult(*InnerMainFunc)(Slang::StdWriters* stdWriters, SlangSession* session, int argc, const char*const* argv); + + /// Given a slang result, returns a return code that can be returned from an executable + static int getReturnCode(SlangResult res); +}; + +} // namespace Slang + +#endif // SLANG_TEST_TOOL_H diff --git a/source/core/slang-writer.h b/source/core/slang-writer.h index b18af373f..463450ac9 100644 --- a/source/core/slang-writer.h +++ b/source/core/slang-writer.h @@ -45,7 +45,7 @@ public: // ISlangUnknown SLANG_REF_OBJECT_IUNKNOWN_QUERY_INTERFACE SLANG_REF_OBJECT_IUNKNOWN_ADD_REF - SLANG_NO_THROW uint32_t SLANG_MCALL release() { return (m_flags & WriterFlag::IsStatic) ? 1 : (uint32_t)releaseReference(); } + SLANG_NO_THROW uint32_t SLANG_MCALL release() { return (m_flags & WriterFlag::IsStatic) ? (uint32_t)decreaseReference() : (uint32_t)releaseReference(); } // ISlangWriter - default impl SLANG_NO_THROW virtual void SLANG_MCALL flush() SLANG_OVERRIDE {} diff --git a/source/slang/slang.vcxproj b/source/slang/slang.vcxproj index a36d9bfbf..132a56ea5 100644 --- a/source/slang/slang.vcxproj +++ b/source/slang/slang.vcxproj @@ -314,4 +314,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project>
\ No newline at end of file +</Project>
\ No newline at end of file diff --git a/source/slangc/main.cpp b/source/slangc/main.cpp index c2d64730a..ae15b7d49 100644 --- a/source/slangc/main.cpp +++ b/source/slangc/main.cpp @@ -5,8 +5,7 @@ SLANG_API void spSetCommandLineCompilerMode(SlangCompileRequest* request); #include "../core/slang-io.h" -#include "../core/slang-app-context.h" -#include "../core/slang-writer.h" +#include "../core/slang-test-tool-util.h" using namespace Slang; @@ -16,7 +15,7 @@ static void diagnosticCallback( char const* message, void* /*userData*/) { - auto stdError = AppContext::getStdError(); + auto stdError = StdWriters::getError(); stdError.put(message); stdError.flush(); } @@ -27,9 +26,9 @@ static void diagnosticCallback( #define MAIN main #endif -SLANG_SHARED_LIBRARY_TOOL_API SlangResult innerMain(AppContext* appContext, SlangSession* session, int argc, const char*const* argv) +SLANG_TEST_TOOL_API SlangResult innerMain(StdWriters* stdWriters, SlangSession* session, int argc, const char*const* argv) { - AppContext::setSingleton(appContext); + StdWriters::setSingleton(stdWriters); SlangCompileRequest* compileRequest = spCreateCompileRequest(session); @@ -41,7 +40,7 @@ SLANG_SHARED_LIBRARY_TOOL_API SlangResult innerMain(AppContext* appContext, Slan spSetCommandLineCompilerMode(compileRequest); // Do any app specific configuration - appContext->configureRequest(compileRequest); + stdWriters->setRequestWriters(compileRequest); char const* appName = "slangc"; if (argc > 0) appName = argv[0]; @@ -70,7 +69,7 @@ SLANG_SHARED_LIBRARY_TOOL_API SlangResult innerMain(AppContext* appContext, Slan #ifndef _DEBUG catch (Exception & e) { - AppContext::getStdOut().print("internal compiler error: %S\n", e.Message.ToWString().begin()); + StdWriters::getOut().print("internal compiler error: %S\n", e.Message.ToWString().begin()); res = SLANG_FAIL; } #endif @@ -85,10 +84,10 @@ int MAIN(int argc, char** argv) SlangResult res; { SlangSession* session = spCreateSession(nullptr); - res = innerMain(AppContext::initDefault(), session, argc, argv); + res = innerMain(StdWriters::initDefault(), session, argc, argv); spDestroySession(session); } - return AppContext::getReturnCode(res); + return TestToolUtil::getReturnCode(res); } #ifdef _WIN32 |
