diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1bc0a3e8d..412a63258 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -38,6 +38,7 @@ option(EXAMPLE_SIMPLECOMRPC_TEST "Include Simple COMRPC test client" OFF) option(EXAMPLE_PLUGINSMARTINTERFACETYPE_EXAMPLE "Include PluginSmartInterfaceType examples" OFF) option(EXAMPLE_DYNAMICJSONRPCERRORMESSAGE_EXAMPLE "Include DynamicJSONRPCErrorMessage example" OFF) option(EXAMPLE_GENERATORSHOWCASE_EXAMPLE "Include GeneratorShowCase example" OFF) +option(EXAMPLE_CUSTOMCODE_EXAMPLE "Include Custom Code example" OFF) if(EXAMPLE_COMRPCCLIENT) add_subdirectory(COMRPCClient) @@ -99,3 +100,8 @@ endif() if (EXAMPLE_GENERATORSHOWCASE_EXAMPLE) add_subdirectory(GeneratorShowcase) endif() + +if (EXAMPLE_CUSTOMCODE_EXAMPLE) + add_subdirectory(CustomErrorCodes/CustomErrorCode) + add_subdirectory(CustomErrorCodes/CustomCodeToStringLibrary) +endif() diff --git a/examples/CustomErrorCodes/CustomCodeToStringLibrary/CMakeLists.txt b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CMakeLists.txt new file mode 100644 index 000000000..ffc23cfb6 --- /dev/null +++ b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CMakeLists.txt @@ -0,0 +1,47 @@ +# If not stated otherwise in this file or this component's license file the +# following copyright and licenses apply: +# +# Copyright 2020 Metrological +# +# Licensed under the Apache License, Version 2.0 (the License); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +project(CustomCodeToStringLibrary) + +cmake_minimum_required(VERSION 3.15) + +find_package(Thunder) + +project_version(1.0.0) + +set(MODULE_NAME ${NAMESPACE}${PROJECT_NAME}) + +message("Setup ${MODULE_NAME} v${PROJECT_VERSION}") + +find_package(${NAMESPACE}Core REQUIRED) +find_package(CompileSettingsDebug CONFIG REQUIRED) + +add_library(${MODULE_NAME} SHARED + CustomCodeToString.cpp +) + +set_target_properties(${MODULE_NAME} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + ) + +target_link_libraries(${MODULE_NAME} + PRIVATE + ${NAMESPACE}Core::${NAMESPACE}Core + CompileSettingsDebug::CompileSettingsDebug) + +install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${NAMESPACE}_Runtime) \ No newline at end of file diff --git a/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToString.cpp b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToString.cpp new file mode 100644 index 000000000..e145d5f9e --- /dev/null +++ b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToString.cpp @@ -0,0 +1,34 @@ + +#if defined WIN32 || defined _WINDOWS +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files +#include + +#include +#define VARIABLE_IS_NOT_USED + +#else + +#ifdef _UNICODE +#define _T(x) L##x +#endif + +#ifndef _UNICODE +#define _T(x) x +#endif + +#endif // __LINUX__ + +#include + +const TCHAR* CustomCodeToSting(const int32_t code) { + const TCHAR* text = nullptr; + if (code == 100) { + text = _T("Custom Error code 100"); + } else if (code == -100) { + text = _T("Custom Error code -100"); + } + + return text; +} + diff --git a/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj new file mode 100644 index 000000000..12e59b5f2 --- /dev/null +++ b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {143f6012-63ec-4eef-bba6-cd1c82f3e3ea} + CustomCodeToStringLibrary + 10.0 + + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + DynamicLibrary + true + v143 + NotSet + + + DynamicLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + .so + $(SolutionDir)..\artifacts\$(Configuration)\ + $(OutDir)Plugins\$(TargetName)\ + lib$(ProjectName) + + + + Level3 + true + WIN32;_DEBUG;CUSTOMCODETOSTRINGLIBRARY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;CUSTOMCODETOSTRINGLIBRARY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + _DEBUG;CUSTOMCODETOSTRINGLIBRARY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + $(FrameworkPath);$(ContractsPath);$(ClientsPath);$(WindowsPath);$(WindowsPath)zlib + false + + + Windows + true + false + + + + + Level3 + true + true + true + NDEBUG;CUSTOMCODETOSTRINGLIBRARY_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + + + + + \ No newline at end of file diff --git a/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj.filters b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj.filters new file mode 100644 index 000000000..007b45a9d --- /dev/null +++ b/examples/CustomErrorCodes/CustomCodeToStringLibrary/CustomCodeToStringLibrary.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/examples/CustomErrorCodes/CustomErrorCode/CMakeLists.txt b/examples/CustomErrorCodes/CustomErrorCode/CMakeLists.txt new file mode 100644 index 000000000..8b0050504 --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/CMakeLists.txt @@ -0,0 +1,59 @@ +# If not stated otherwise in this file or this component's LICENSE file the +# following copyright and licenses apply: +# +# Copyright 2025 Metrological +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +project(CustomErrorCodeServer) + +cmake_minimum_required(VERSION 3.15) + +find_package(Thunder) + +project_version(1.0.0) + +set(MODULE_NAME ${NAMESPACE}${PROJECT_NAME}) + +message("Setup ${MODULE_NAME} v${PROJECT_VERSION}") + +find_package(${NAMESPACE}Core REQUIRED) +find_package(${NAMESPACE}Plugins REQUIRED) +find_package(${NAMESPACE}Definitions REQUIRED) +find_package(${NAMESPACE}Messaging REQUIRED) +find_package(CompileSettingsDebug REQUIRED) + +add_library(${MODULE_NAME} SHARED + Module.cpp + CustomErrorCodeServer.cpp +) + +target_include_directories(${MODULE_NAME} SYSTEM + PRIVATE + ".") + +set_target_properties(${MODULE_NAME} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES) + +target_link_libraries(${MODULE_NAME} + PRIVATE + ${NAMESPACE}Core::${NAMESPACE}Core + ${NAMESPACE}Plugins::${NAMESPACE}Plugins + ${NAMESPACE}Definitions::${NAMESPACE}Definitions + ${NAMESPACE}Messaging::${NAMESPACE}Messaging + CompileSettingsDebug::CompileSettingsDebug) + +install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}/${STORAGE_DIRECTORY}/plugins COMPONENT ${NAMESPACE}_Runtime) + +write_config() diff --git a/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.conf.in b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.conf.in new file mode 100644 index 000000000..aa02c8276 --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.conf.in @@ -0,0 +1,2 @@ +startmode = "Activated" + diff --git a/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.cpp b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.cpp new file mode 100644 index 000000000..36f8c30fe --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.cpp @@ -0,0 +1,58 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 Metrological + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CustomErrorCodeServer.h" +#include + +namespace Thunder { + +namespace Plugin { + + namespace { + + static Metadata metadata( + // Version + 1, 0, 0, + // Preconditions + {}, + // Terminations + {}, + // Controls + {} + ); + } + + const string CustomErrorCodeServer::Initialize(PluginHost::IShell*) + { + Example::JCustomErrorCode::Register(*this, this); + return (EMPTY_STRING); + } + + void CustomErrorCodeServer::Deinitialize(PluginHost::IShell*) + { + Example::JCustomErrorCode::Unregister(*this); + } + + string CustomErrorCodeServer::Information() const + { + return (EMPTY_STRING); + } + +} +} // namespace Plugin diff --git a/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.h b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.h new file mode 100644 index 000000000..0651c66dc --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.h @@ -0,0 +1,124 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 Metrological + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "Module.h" +#include + +namespace Thunder { +namespace Plugin { + + class CustomErrorCodeServer : public Example::ICustomErrorCode, public PluginHost::IPlugin, public PluginHost::JSONRPC { + public: + CustomErrorCodeServer(const CustomErrorCodeServer&) = delete; + CustomErrorCodeServer& operator=(const CustomErrorCodeServer&) = delete; + CustomErrorCodeServer(CustomErrorCodeServer&&) = delete; + CustomErrorCodeServer& operator=(CustomErrorCodeServer&&) = delete; + + CustomErrorCodeServer() + : Example::ICustomErrorCode() + , PluginHost::IPlugin() + , PluginHost::JSONRPC() + { + } + + ~CustomErrorCodeServer() override = default; + + public: + //Service Registration + BEGIN_INTERFACE_MAP(CustomErrorCodeServer) + INTERFACE_ENTRY(IPlugin) + INTERFACE_ENTRY(IDispatcher) + INTERFACE_ENTRY(Example::ICustomErrorCode) + END_INTERFACE_MAP + + const string Initialize(PluginHost::IShell* service); + void Deinitialize(PluginHost::IShell* service); + string Information() const; + + // the interface allows the json rpc call to provide the error code used which provides for easy testing + Core::hresult TriggerCustomError(const int32_t errorcode) const override + { + // we simulate calling aanother COMRPC call which might return a custom error code (and in this case it will return a custom code) + Core::hresult result = SimulatedComRPCCall(errorcode); + + // we check if there was an error, comparing to Core::ERROR_NONE is the only thing needed also with custom codes + // only if you want to deal with some specific error you need to handle that situation specifically which we wil do here as en example + if (result != Core::ERROR_NONE) { + ExampleHandlingHResultWhichCouldContainCustomCode(result); + } + + return Core::CustomCode(errorcode == 0 ? Core::ERROR_NONE : errorcode); // of course we know Core::ERROR_NONE is also 0 but semantically it is more correct like this (and will continue to work might ERROR_NONE ever be a different value) + } + + // the interface allows the json rpc call to provide the error code used which provides for easy testing + Core::hresult TriggerNonCustomError(const uint32_t errorcode) const override + { + // we simulate calling aanother COMRPC call which might return a custom error code (and in this case it wil return a non custom code) + Core::hresult result = SimulatedComRPCCall(errorcode); + + // we check if there was an error, comparing to Core::ERROR_NONE is the only thing needed also with custom codes + // only if you want to deal with some specific error you need to handle that situation specifically which we wil do here as en example + if (result != Core::ERROR_NONE) { + ExampleHandlingHResultWhichCouldContainCustomCode(result); + } + + return errorcode; + } + + private: + // simulate a COMRPC call that returns an Thunder defined error + Core::hresult SimulatedComRPCCall(const Core::hresult errorcode) const + { + return errorcode; + } + // simulate a COMRPC call that returns an Custom Code + Core::hresult SimulatedComRPCCall(const int32_t customerrorcode) const + { + // when there is no error, just feed Core::ERROR_NONE into the Core::CustomCode(...) function to indicate there is no error as you would do with a normal hresult + int32_t result = Core::ERROR_NONE; + + // suppose you detect an error situation and you want to set a custom code, just set the value assigned for this error + if (customerrorcode != 0) { + result = customerrorcode; + } + + return Core::CustomCode(result); // calling Core::CustomCode will convert the custom code into the correct hresult value so the custom code value gets stored appropriately in the hresult + } + void ExampleHandlingHResultWhichCouldContainCustomCode(const Core::hresult errorcode) const + { + // here we handle an hresult in a detailed way in case we would like to extract the custom code for some reason (again not required, if you just want to handle an error situation you can just compare the hresult with Core::ERROR_NOE, nothing more to do + // (and if you need a text representation feed the hresult to ErrorToString or ErrorToStringExtended that will internnly handle the custom code correclty) + int32_t customcode = Core::IsCustomCode(errorcode); + + if (customcode == 0) { + TRACE(Trace::Information, (_T("Thunder defined error received: %u"), errorcode)); + } else if (customcode == std::numeric_limits::min()) { + TRACE(Trace::Information, (_T("Invalid custom code was passed"))); + } else { + TRACE(Trace::Information, (_T("Custom Code received: %i"), customcode)); + } + + // You can just feed any hresult to ErrorToString or ErrorToStringExtended, it will deal with the Custom Code internally + TRACE(Trace::Information, (_T("Error received: %s"), Core::ErrorToStringExtended(errorcode).c_str())); + } + + }; +} +} diff --git a/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.vcxproj b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.vcxproj new file mode 100644 index 000000000..f10b80b5f --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/CustomErrorCodeServer.vcxproj @@ -0,0 +1,192 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + 15.0 + {E69C85AC-B2BE-4755-80DB-5EC0306BFAE7} + Win32Proj + CustomErrorCodeServer + 10.0 + CustomErrorCodeServer + + + + DynamicLibrary + true + v143 + MultiByte + + + DynamicLibrary + false + v143 + true + MultiByte + + + DynamicLibrary + true + v143 + MultiByte + + + DynamicLibrary + false + v143 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)..\artifacts\$(Configuration)\ + $(OutDir)Plugins\$(TargetName)\ + lib$(ProjectName) + .so + + + true + $(SolutionDir)..\artifacts\$(Configuration)\ + $(OutDir)Plugins\$(TargetName)\ + lib$(ProjectName) + .so + + + false + $(SolutionDir)..\artifacts\$(Configuration)\ + $(OutDir)Plugins\$(TargetName)\ + lib$(ProjectName) + .so + + + false + $(SolutionDir)..\artifacts\$(Configuration)\ + $(OutDir)Plugins\$(TargetName)\ + lib$(ProjectName) + .so + + + + NotUsing + Level3 + Disabled + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;OUTOFPROCESSTEST_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + $(FrameworkPath);$(ContractsPath);$(ClientsPath);$(WindowsPath);$(WindowsPath)zlib + true + + + Windows + true + $(OutDir) + + + + + NotUsing + Level3 + Disabled + true + _CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;OUTOFPROCESSTEST_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + $(FrameworkPath);$(ContractsPath);$(ClientsPath);$(WindowsPath);$(WindowsPath)zlib + true + + + Windows + true + $(OutDir) + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + _CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;OUTOFPROCESSTEST_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + $(FrameworkPath);$(ContractsPath);$(ClientsPath);$(WindowsPath);$(WindowsPath)zlib + true + + + Windows + true + true + true + $(OutDir) + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;OUTOFPROCESSTEST_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + $(FrameworkPath);$(ContractsPath);$(ClientsPath);$(WindowsPath);$(WindowsPath)zlib + true + + + Windows + true + true + true + $(OutDir) + + + + + + \ No newline at end of file diff --git a/examples/CustomErrorCodes/CustomErrorCode/Module.cpp b/examples/CustomErrorCodes/CustomErrorCode/Module.cpp new file mode 100644 index 000000000..bbf44ccdf --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/Module.cpp @@ -0,0 +1,22 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 Metrological + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Module.h" + +MODULE_NAME_DECLARATION(BUILD_REFERENCE) diff --git a/examples/CustomErrorCodes/CustomErrorCode/Module.h b/examples/CustomErrorCodes/CustomErrorCode/Module.h new file mode 100644 index 000000000..3b2d8b495 --- /dev/null +++ b/examples/CustomErrorCodes/CustomErrorCode/Module.h @@ -0,0 +1,30 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 Metrological + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#ifndef MODULE_NAME +#define MODULE_NAME Plugin_CustomErrorCodeServer +#endif + +#include +#include + +#undef EXTERNAL +#define EXTERNAL