diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2018-06-22 13:09:01 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-22 13:09:01 -0400 |
| commit | d0c9571be3a2167a9f019310aca8f7cd326972c0 (patch) | |
| tree | 52aa2f14ec2c9d8d42bc6fcbd381ed9799c19533 /source/core | |
| parent | e66d66b88e1c6ef8499708952fcbe3ba873f6e4c (diff) | |
Expose macros/functionality for defining interfaces (#604)
* Added Result definitions to the slang.h
* Removed slang-result.h and added slang-com-helper.h
* Move slang-com-ptr.h to be publically available.
* Add SLANG_IUNKNOWN macros to simplify implementing interfaces.
Use the SLANG_IUNKNOWN macros to in slang.c
* Removed slang-defines.h added outstanding defines to slang.h
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/list.h | 5 | ||||
| -rw-r--r-- | source/core/slang-com-ptr.h | 206 | ||||
| -rw-r--r-- | source/core/slang-defines.h | 314 | ||||
| -rw-r--r-- | source/core/slang-free-list.h | 8 | ||||
| -rw-r--r-- | source/core/slang-result.h | 135 |
5 files changed, 8 insertions, 660 deletions
diff --git a/source/core/list.h b/source/core/list.h index 3a325955a..6df60b74b 100644 --- a/source/core/list.h +++ b/source/core/list.h @@ -1,10 +1,11 @@ -#ifndef FUNDAMENTAL_LIB_LIST_H +#ifndef FUNDAMENTAL_LIB_LIST_H #define FUNDAMENTAL_LIB_LIST_H +#include "../../slang.h" + #include "allocator.h" #include "slang-math.h" #include "array-view.h" -#include "slang-defines.h" #include <algorithm> #include <new> diff --git a/source/core/slang-com-ptr.h b/source/core/slang-com-ptr.h deleted file mode 100644 index 765edbee5..000000000 --- a/source/core/slang-com-ptr.h +++ /dev/null @@ -1,206 +0,0 @@ -#ifndef SLANG_COM_PTR_H -#define SLANG_COM_PTR_H - -#include "slang-defines.h" -#include "slang-result.h" - -#include <assert.h> - -namespace Slang { - -/*! \brief ComPtr is a simple smart pointer that manages types which implement COM based interfaces. -\details A class that implements a COM, must derive from the IUnknown interface or a type that matches -it's layout exactly (such as IForwardUnknown). Trying to use this template with a class that doesn't follow -these rules, will lead to undefined behavior. -This is a 'strong' pointer type, and will AddRef when a non null pointer is set and Release when the pointer -leaves scope. -Using 'detach' allows a pointer to be removed from the management of the ComPtr. -To set the smart pointer to null, there is the method setNull, or alternatively just assign SLANG_NULL/nullptr. - -One edge case using the template is that sometimes you want access as a pointer to a pointer. Sometimes this -is to write into the smart pointer, other times to pass as an array. To handle these different behaviors -there are the methods readRef and writeRef, which are used instead of the & (ref) operator. For example - -\code - -Void doSomething(ID3D12Resource** resources, IndexT numResources); - -// ... -ComPtr<ID3D12Resource> resources[3]; - -doSomething(resources[0].readRef(), SLANG_COUNT_OF(resource)); -\endcode - -A more common scenario writing to the pointer - -\code -IUnknown* unk = ...; - -ComPtr<ID3D12Resource> resource; -Result res = unk->QueryInterface(resource.writeRef()); -\endcode -*/ - -typedef SlangUUID Guid; - -SLANG_FORCE_INLINE bool operator==(const Guid& aIn, const Guid& bIn) -{ - // Use the largest type the honors the alignment of Guid - typedef uint32_t CmpType; - union GuidCompare - { - Guid guid; - CmpType data[sizeof(Guid) / sizeof(CmpType)]; - }; - // Type pun - so compiler can 'see' the pun and not break aliasing rules - const CmpType* a = reinterpret_cast<const GuidCompare&>(aIn).data; - const CmpType* b = reinterpret_cast<const GuidCompare&>(bIn).data; - // Make the guid comparison a single branch, by not using short circuit - return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]) | (a[3] ^ b[3])) == 0; -} - -SLANG_FORCE_INLINE bool operator!=(const Guid& a, const Guid& b) -{ - return !(a == b); -} - -// Allows for defining of a GUID that works in C++ and C which defines in a format similar to microsofts INTERFACE style -// MIDL_INTERFACE("00000000-0000-0000-C000-00 00 00 00 00 46") - -#define SLANG_GUID_BYTE(x, index) ((uint8_t)(SLANG_UINT64(0x##x) >> (8 * index))) - -#define SLANG_MAKE_GUID(data0, data1, data2, shortTail, tail) \ - { (uint32_t)(0x##data0), (uint16_t)(0x##data1), (uint16_t)(0x##data2), \ - { (uint8_t)(0x##shortTail >> 8), (uint8_t)(0x##shortTail & 0xff), \ - SLANG_GUID_BYTE(tail,5), SLANG_GUID_BYTE(tail,4), SLANG_GUID_BYTE(tail,3), SLANG_GUID_BYTE(tail,2), SLANG_GUID_BYTE(tail,1), SLANG_GUID_BYTE(tail,0) \ - }} - -// Compatible with Microsoft IUnknown -static const Guid IID_IComUnknown = SLANG_MAKE_GUID(00000000, 0000, 0000, C000, 000000000046); - -typedef ISlangUnknown IComUnknown; - -// Enum to force initializing as an attach (without adding a reference) -enum InitAttach -{ - INIT_ATTACH -}; - -template <class T> -class ComPtr -{ -public: - typedef T Type; - typedef ComPtr ThisType; - typedef IComUnknown* Ptr; - - /// Constructors - /// Default Ctor. Sets to nullptr - SLANG_FORCE_INLINE ComPtr() :m_ptr(nullptr) {} - /// Sets, and ref counts. - SLANG_FORCE_INLINE explicit ComPtr(T* ptr) :m_ptr(ptr) { if (ptr) ((Ptr)ptr)->addRef(); } - /// The copy ctor - SLANG_FORCE_INLINE ComPtr(const ThisType& rhs) : m_ptr(rhs.m_ptr) { if (m_ptr) ((Ptr)m_ptr)->addRef(); } - - /// Ctor without adding to ref count. - SLANG_FORCE_INLINE explicit ComPtr(InitAttach, T* ptr) :m_ptr(ptr) { } - /// Ctor without adding to ref count - SLANG_FORCE_INLINE ComPtr(InitAttach, const ThisType& rhs) : m_ptr(rhs.m_ptr) { } - -#ifdef SLANG_HAS_MOVE_SEMANTICS - /// Move Ctor - SLANG_FORCE_INLINE ComPtr(ThisType&& rhs) : m_ptr(rhs.m_ptr) { rhs.m_ptr = nullptr; } - /// Move assign - SLANG_FORCE_INLINE ComPtr& operator=(ThisType&& rhs) { T* swap = m_ptr; m_ptr = rhs.m_ptr; rhs.m_ptr = swap; return *this; } -#endif - - /// Destructor releases the pointer, assuming it is set - SLANG_FORCE_INLINE ~ComPtr() { if (m_ptr) ((Ptr)m_ptr)->release(); } - - // !!! Operators !!! - - /// Returns the dumb pointer - SLANG_FORCE_INLINE operator T *() const { return m_ptr; } - - SLANG_FORCE_INLINE T& operator*() { return *m_ptr; } - /// For making method invocations through the smart pointer work through the dumb pointer - SLANG_FORCE_INLINE T* operator->() const { return m_ptr; } - - /// Assign - SLANG_FORCE_INLINE const ThisType &operator=(const ThisType& rhs); - /// Assign from dumb ptr - SLANG_FORCE_INLINE T* operator=(T* in); - - /// Get the pointer and don't ref - SLANG_FORCE_INLINE T* get() const { return m_ptr; } - /// Release a contained nullptr pointer if set - SLANG_FORCE_INLINE void setNull(); - - /// Detach - SLANG_FORCE_INLINE T* detach() { T* ptr = m_ptr; m_ptr = nullptr; return ptr; } - /// Set to a pointer without changing the ref count - SLANG_FORCE_INLINE void attach(T* in) { m_ptr = in; } - - /// Get ready for writing (nulls contents) - SLANG_FORCE_INLINE T** writeRef() { setNull(); return &m_ptr; } - /// Get for read access - SLANG_FORCE_INLINE T*const* readRef() const { return &m_ptr; } - - /// Swap - void swap(ThisType& rhs); - -protected: - /// Gets the address of the dumb pointer. - // Disabled: use writeRef and readRef to get a reference based on usage. - SLANG_FORCE_INLINE T** operator&(); - - T* m_ptr; -}; - -//---------------------------------------------------------------------------- -template <typename T> -void ComPtr<T>::setNull() -{ - if (m_ptr) - { - ((Ptr)m_ptr)->release(); - m_ptr = nullptr; - } -} -//---------------------------------------------------------------------------- -/* template <typename T> -T** ComPtr<T>::operator&() -{ - assert(m_ptr == nullptr); - return &m_ptr; -} */ -//---------------------------------------------------------------------------- -template <typename T> -const ComPtr<T>& ComPtr<T>::operator=(const ThisType& rhs) -{ - if (rhs.m_ptr) ((Ptr)rhs.m_ptr)->addRef(); - if (m_ptr) ((Ptr)m_ptr)->release(); - m_ptr = rhs.m_ptr; - return *this; -} -//---------------------------------------------------------------------------- -template <typename T> -T* ComPtr<T>::operator=(T* ptr) -{ - if (ptr) ((Ptr)ptr)->addRef(); - if (m_ptr) ((Ptr)m_ptr)->release(); - m_ptr = ptr; - return m_ptr; -} -//---------------------------------------------------------------------------- -template <typename T> -void ComPtr<T>::swap(ThisType& rhs) -{ - T* tmp = m_ptr; - m_ptr = rhs.m_ptr; - rhs.m_ptr = tmp; -} - -} // namespace Slang - -#endif // SLANG_COM_PTR_H diff --git a/source/core/slang-defines.h b/source/core/slang-defines.h deleted file mode 100644 index 580dd801c..000000000 --- a/source/core/slang-defines.h +++ /dev/null @@ -1,314 +0,0 @@ -#ifndef SLANG_DEFINES_H -#define SLANG_DEFINES_H - -/* -Some of our `#define`s are needed in the public API header as well, so -we will go ahead and include that here so that we can share the definitions. -*/ -#include "../../slang.h" - -/* -The following preprocessor identifiers specify compiler, OS, and architecture. -All definitions have a value of 1 or 0, use '#if' instead of '#ifdef'. -*/ - -#ifndef SLANG_PROCESSOR -# define SLANG_PROCESSOR - -/* Architecture defines, see http://sourceforge.net/p/predef/wiki/Architectures/ */ -# if defined(__x86_64__) || defined(_M_X64) // ps4 compiler defines _M_X64 without value -# define SLANG_X64 1 -# elif defined(__i386__) || defined(_M_IX86) -# define SLANG_X86 1 -# elif defined(__arm64__) || defined(__aarch64__) -# define SLANG_A64 1 -# elif defined(__arm__) || defined(_M_ARM) -# define SLANG_ARM 1 -# elif defined(__SPU__) -# define SLANG_SPU 1 -# elif defined(__ppc__) || defined(_M_PPC) || defined(__CELLOS_LV2__) -# define SLANG_PPC 1 -# else -# error "Unknown architecture" -# endif - -/** -SIMD defines -*/ -# if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) -# define SLANG_SSE2 1 -# endif -# if defined(_M_ARM) || defined(__ARM_NEON__) -# define SLANG_NEON 1 -# endif -# if defined(_M_PPC) || defined(__CELLOS_LV2__) -# define SLANG_VMX 1 -# endif - -// Zero unset -# ifndef SLANG_X64 -# define SLANG_X64 0 -# endif -# ifndef SLANG_X86 -# define SLANG_X86 0 -# endif -# ifndef SLANG_A64 -# define SLANG_A64 0 -# endif -# ifndef SLANG_ARM -# define SLANG_ARM 0 -# endif -# ifndef SLANG_SPU -# define SLANG_SPU 0 -# endif -# ifndef SLANG_PPC -# define SLANG_PPC 0 -# endif -# ifndef SLANG_SSE2 -# define SLANG_SSE2 0 -# endif -# ifndef SLANG_NEON -# define SLANG_NEON 0 -# endif -# ifndef SLANG_VMX -# define SLANG_VMX 0 -# endif - -#endif // SLANG_PROCESSOR - -/* -define anything not defined through the command line to 0 -*/ -#ifndef SLANG_DEBUG -# define SLANG_DEBUG 0 -#endif -#ifndef SLANG_CHECKED -# define SLANG_CHECKED 0 -#endif -#ifndef SLANG_PROFILE -# define SLANG_PROFILE 0 -#endif - -/** -family shortcuts -*/ -// architecture -#define SLANG_INTEL_FAMILY (SLANG_X64 || SLANG_X86) // Intel x86 family -#define SLANG_ARM_FAMILY (SLANG_ARM || SLANG_A64) -#define SLANG_PPC_FAMILY (SLANG_PPC) - -#define SLANG_P64_FAMILY (SLANG_X64 || SLANG_A64) // shortcut for 64-bit architectures - -// Use for getting the amount of members of a standard C array. -#define SLANG_COUNT_OF(x) (sizeof(x)/sizeof(x[0])) -/// SLANG_INLINE exists to have a way to inline consistent with SLANG_ALWAYS_INLINE -#define SLANG_INLINE inline - -// Other defines -#define SLANG_STRINGIZE_HELPER(X) #X -#define SLANG_STRINGIZE(X) SLANG_STRINGIZE_HELPER(X) - -#define SLANG_CONCAT_HELPER(X, Y) X##Y -#define SLANG_CONCAT(X, Y) SLANG_CONCAT_HELPER(X, Y) - -#ifndef SLANG_UNUSED -# define SLANG_UNUSED(v) (void)v; -#endif - -/** -General defines -*/ - -// GCC Specific -#if SLANG_GCC_FAMILY -# define SLANG_NO_INLINE __attribute__((noinline)) - -// This doesn't work on clang - because the typedef is seen as multiply defined, use the line numbered version defined later -# if !defined(__clang__) && (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__ORBIS__)) -# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SlangCompileTimeAssert_Dummy[(exp) ? 1 : -1] __attribute__((unused)) -# endif - -# if !SLANG_SNC && !SLANG_GHS -# define SLANG_OFFSET_OF(X, Y) __builtin_offsetof(X, Y) -# endif - -//# if !SLANG_LINUX // Workaround; Fedora Core 3 do not agree with force inline -# define SLANG_FORCE_INLINE inline __attribute__((always_inline)) -//# endif - -# define SLANG_BREAKPOINT(id) __builtin_trap(); - -# if (__cplusplus >= 201103L) && ((__GNUC__ > 4) || (__GNUC__ ==4 && __GNUC_MINOR__ >= 6)) -# define SLANG_NULL nullptr -# else -# define SLANG_NULL __null -# endif - -# define SLANG_ALIGN_OF(T) __alignof__(T) - -# define SLANG_FUNCTION_SIG __PRETTY_FUNCTION__ - -#endif // SLANG_GCC_FAMILY - -// Microsoft VC specific -#if SLANG_MICROSOFT_FAMILY - -# pragma inline_depth(255) - -# pragma warning(disable : 4324 ) // C4324: structure was padded due to alignment specifier -# pragma warning(disable : 4514 ) // 'function' : unreferenced inline function has been removed -# pragma warning(disable : 4710 ) // 'function' : function not inlined -# pragma warning(disable : 4711 ) // function 'function' selected for inline expansion -# pragma warning(disable : 4127 ) // C4127: conditional expression is constant - -# define SLANG_NO_ALIAS __declspec(noalias) -# define SLANG_NO_INLINE __declspec(noinline) -# define SLANG_FORCE_INLINE __forceinline -# define SLANG_PUSH_PACK_DEFAULT __pragma(pack(push, 8)) -# define SLANG_POP_PACK __pragma(pack(pop)) - -# define SLANG_BREAKPOINT(id) __debugbreak(); - -# ifdef __cplusplus -# define SLANG_NULL nullptr -# endif - -# define SLANG_ALIGN_OF(T) __alignof(T) - -# define SLANG_FUNCTION_SIG __FUNCSIG__ - -# define SLANG_INT64(x) (x##i64) -# define SLANG_UINT64(x) (x##ui64) - -# define SLANG_CALL_CONV __cdecl - -#endif // SLANG_MICROSOFT_FAMILY - -// Set defaults if not set - -// Used for doing constant literals -#ifndef SLANG_INT64 -# define SLANG_INT64(x) (x##ll) -#endif -#ifndef SLANG_UINT64 -# define SLANG_UINT64(x) (x##ull) -#endif - -#ifndef SLANG_NULL -# if defined(NULL) -# define SLANG_NULL NULL -# else -# define SLANG_NULL 0 -# endif -#endif - -#ifndef SLANG_FORCE_INLINE -# define SLANG_FORCE_INLINE inline -#endif -#ifndef SLANG_NO_INLINE -# define SLANG_NO_INLINE -#endif -#ifndef SLANG_NO_ALIAS -# define SLANG_NO_ALIAS -#endif -#ifndef SLANG_COMPILE_TIME_ASSERT -# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SLANG_CONCAT(SlangCompileTimeAssert,__LINE__)[(exp) ? 1 : -1] -#endif -#ifndef SLANG_OFFSET_OF -# define SLANG_OFFSET_OF(X, Y) offsetof(X, Y) -#endif -#ifndef SLANG_BREAKPOINT -// Make it crash with a write to 0! -# define SLANG_BREAKPOINT(id) (*((int*)0) = int(id)); -#endif - -#ifndef SLANG_FUNCTION_NAME -# define SLANG_FUNCTION_NAME __FUNCTION__ -#endif -#ifndef SLANG_FUNCTION_SIG -# define SLANG_FUNCTION_SIG SLANG_FUNCTION_NAME -#endif - -#ifndef SLANG_CALL_CONV -# define SLANG_CALL_CONV -#endif - -//! casting the null ptr takes a special-case code path, which we don't want -#define SLANG_OFFSETOF_BASE 0x100 -#define SLANG_OFFSET_OF_RT(Class, Member) \ - (reinterpret_cast<size_t>(&reinterpret_cast<Class*>(SLANG_OFFSETOF_BASE)->Member) - size_t(SLANG_OFFSETOF_BASE)) - -#ifdef __CUDACC__ -# define SLANG_CUDA_CALLABLE __host__ __device__ -#else -# define SLANG_CUDA_CALLABLE -#endif - -#if SLANG_GCC_FAMILY && !SLANG_GHS -# define SLANG_WEAK_SYMBOL __attribute__((weak)) // this is to support SIMD constant merging in template specialization -#else -# define SLANG_WEAK_SYMBOL -#endif - -// C++ Symbols -#ifdef __cplusplus - -// C++ specific macros - -// Gcc -# if SLANG_GCC_FAMILY -// Check for C++11 -# if (__cplusplus >= 201103L) -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 -# define SLANG_HAS_MOVE_SEMANTICS 1 -# endif -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 -# define SLANG_HAS_ENUM_CLASS 1 -# endif -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 407 -# define SLANG_OVERRIDE override -# endif -# endif -# endif // SLANG_GCC_FAMILY - -// Visual Studio - -# if SLANG_VC -// C4481: nonstandard extension used: override specifier 'override' -# if _MSC_VER < 1700 -# pragma warning(disable : 4481) -# endif -# define SLANG_OVERRIDE override -# if _MSC_VER >= 1600 -# define SLANG_HAS_MOVE_SEMANTICS 1 -# endif -# if _MSC_VER >= 1700 -# define SLANG_HAS_ENUM_CLASS 1 -# endif -# endif // SLANG_VC - -// Clang specific -# if SLANG_CLANG -# endif // SLANG_CLANG - -// Set non set - -#ifndef SLANG_NO_THROW -# define SLANG_NO_THROW -#endif -#ifndef SLANG_OVERRIDE -# define SLANG_OVERRIDE -#endif -#ifndef SLANG_HAS_ENUM_CLASS -# define SLANG_HAS_ENUM_CLASS 0 -#endif -#ifndef SLANG_HAS_MOVE_SEMANTICS -# define SLANG_HAS_MOVE_SEMANTICS 0 -#endif - -#include <new> // For placement new - -#endif // __cplusplus - - -#endif // SLANG_DEFINES_H diff --git a/source/core/slang-free-list.h b/source/core/slang-free-list.h index 10f0a39ba..dfa924797 100644 --- a/source/core/slang-free-list.h +++ b/source/core/slang-free-list.h @@ -1,8 +1,10 @@ -#ifndef SLANG_FREE_LIST_H +#ifndef SLANG_FREE_LIST_H #define SLANG_FREE_LIST_H -#include "slang-defines.h" -#include "slang-result.h" +#include "../../slang.h" + +#include <assert.h> + #include <stdlib.h> #include <string.h> diff --git a/source/core/slang-result.h b/source/core/slang-result.h deleted file mode 100644 index 348029af4..000000000 --- a/source/core/slang-result.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef SLANG_RESULT_H -#define SLANG_RESULT_H - -#include <cstdint> -#include <assert.h> - -/*! SlangResult is a type that represents the status result after a call to a function/method. It can represent if the call was successful or -not. It can also specify in an extensible manner what facility produced the result (as the integral 'facility') as well as what caused it (as an integral 'code'). -Under the covers SlangResult is represented as a int32_t. A negative value indicates an error, a positive (or 0) value indicates success. - -SlangResult is designed to be compatible with COM HRESULT. - -It's layout in bits is as follows - -Severity | Facility | Code ----------|----------|----- -31 | 30-16 | 15-0 - -Severity - 1 fail, 0 is success. -Facility is where the error originated from -Code is the code specific to the facility. - -The layout is designed such that failure is a negative number, and success is positive due to Result -being represented by an Int32. - -Result codes have the following style - -1. SLANG_name -2. SLANG_s_f_name -3. SLANG_s_name - -where s is severity as a single letter S - success, and E for error -Style 1 is reserved for SLANG_OK and SLANG_FAIL as they are so common and not tied to a facility - -s is S for success, E for error -f is the short version of the facility name - -For the common used SLANG_OK and SLANG_FAIL, the name prefix is dropped. -It is acceptable to expand 'f' to a longer name to differentiate a name -ie for a facility 'DRIVER' it might make sense to have an error of the form SLANG_E_DRIVER_OUT_OF_MEMORY -*/ - -typedef int32_t SlangResult; - -// Make just the identifier for the code -#define SLANG_MAKE_RESULT_ID(fac, code) ((((int32_t)(fac))<<16) | ((int32_t)(code))) - -//! Make a result -#define SLANG_MAKE_RESULT(sev, fac, code) ((((int32_t)(sev))<<31) | SLANG_MAKE_RESULT_ID(fac, code)) - -//! Will be 0 - for ok, 1 for failure -#define SLANG_GET_RESULT_SEVERITY(r) ((int32_t)(((uint32_t(r)) >> 31)) -//! Get the facility the result is associated with -#define SLANG_GET_RESULT_FACILITY(r) ((int32_t)(((r) >> 16) & 0x7fff)) -//! Get the result code for the facility -#define SLANG_GET_RESULT_CODE(r) ((int32_t)((r) & 0xffff)) - -#define SLANG_MAKE_ERROR(fac, code) (SLANG_MAKE_RESULT_ID(fac, code) | 0x80000000) -#define SLANG_MAKE_SUCCESS(fac, code) SLANG_MAKE_RESULT_ID(fac, code) - -/*************************** Facilities ************************************/ - -//! General - careful to make compatible with HRESULT -#define SLANG_FACILITY_GENERAL 0 - -//! Base facility -> so as to not clash with HRESULT values -#define SLANG_FACILITY_BASE 0x100 - -/*! Facilities numbers must be unique across a project to make the resulting result a unique number! -It can be useful to have a consistent short name for a facility, as used in the name prefix */ -#define SLANG_FACILITY_DISK (SLANG_FACILITY_BASE + 1) -#define SLANG_FACILITY_INTERFACE (SLANG_FACILITY_BASE + 2) -#define SLANG_FACILITY_UNKNOWN (SLANG_FACILITY_BASE + 3) -#define SLANG_FACILITY_MEMORY (SLANG_FACILITY_BASE + 4) -#define SLANG_FACILITY_MISC (SLANG_FACILITY_BASE + 5) - -/// Base for external facilities. Facilities should be unique across modules. -#define SLANG_FACILITY_EXTERNAL_BASE 0x210 -#define SLANG_FACILITY_CORE (SLANG_FACILITY_EXTERNAL_BASE + 1) - -/* *************************** Codes **************************************/ - -// Memory -#define SLANG_E_MEM_OUT_OF_MEMORY SLANG_MAKE_ERROR(SLANG_FACILITY_MEMORY, 1) -#define SLANG_E_MEM_BUFFER_TOO_SMALL SLANG_MAKE_ERROR(SLANG_FACILITY_MEMORY, 2) - -//! SLANG_OK indicates success, and is equivalent to SLANG_MAKE_RESULT(0, GENERAL, 0) -#define SLANG_OK 0 -//! SLANG_FAIL is the generic failure code - meaning a serious error occurred and the call couldn't complete -#define SLANG_FAIL SLANG_MAKE_ERROR(SLANG_FACILITY_GENERAL, 1) - -//! Used to identify a Result that has yet to be initialized. -//! It defaults to failure such that if used incorrectly will fail, as similar in concept to using an uninitialized variable. -#define SLANG_E_MISC_UNINITIALIZED SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 2) -//! Returned from an async method meaning the output is invalid (thus an error), but a result for the request is pending, and will be returned on a subsequent call with the async handle. -#define SLANG_E_MISC_PENDING SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 3) -//! Indicates that a handle passed in as parameter to a method is invalid. -#define SLANG_E_MISC_INVALID_HANDLE SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 4) - -//! Indicates that tn interface could not be found -#define SLANG_E_INTF_NO_INTERFACE SLANG_MAKE_ERROR(SLANG_FACILITY_INTERFACE, 2) - -/*! Set SLANG_HANDLE_RESULT_FAIL(x) to code to be executed whenever an error occurs, and is detected by one of the macros */ -#ifndef SLANG_HANDLE_RESULT_FAIL -# define SLANG_HANDLE_RESULT_FAIL(x) -#endif - -/* !!!!!!!!!!!!!!!!!!!!!!!!! Checking codes !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ - -//! Use to test if a result was failure. Never use result != SLANG_OK to test for failure, as there may be successful codes != SLANG_OK. -#define SLANG_FAILED(status) ((status) < 0) -//! Use to test if a result succeeded. Never use result == SLANG_OK to test for success, as will detect other successful codes as a failure. -#define SLANG_SUCCEEDED(status) ((status) >= 0) - -//! Helper macro, that makes it easy to add result checking to calls in functions/methods that themselves return Result. -#define SLANG_RETURN_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return _res; } } -//! Helper macro that can be used to test the return value from a call, and will return in a void method/function -#define SLANG_RETURN_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return; } } -//! Helper macro that will return false on failure. -#define SLANG_RETURN_FALSE_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return false; } } -//! Helper macro that will return nullptr on failure. -#define SLANG_RETURN_NULL_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return nullptr; } } - -//! Helper macro that will assert if the return code from a call is failure, also returns the failure. -#define SLANG_ASSERT_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return _res; } } -//! Helper macro that will assert if the result from a call is a failure, also returns. -#define SLANG_ASSERT_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return; } } - -#if defined(__cplusplus) -namespace Slang { -typedef SlangResult Result; -} // namespace Slang -#endif // defined(__cplusplus) - -#endif // SLANG_RESULT_H
\ No newline at end of file |
