From d31dc00f82d2121dc0378db77264d9263540a192 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 25 Jan 2020 11:19:23 -0800 Subject: Reworked main headers to be compatible with c --- Primitives/interface/BasicTypes.h | 51 +++++++++++--------- Primitives/interface/CommonDefinitions.h | 80 ++++++++++++++++++++++++++++++++ Primitives/interface/DataBlob.h | 14 ++++-- Primitives/interface/DebugOutput.h | 25 +++++----- Primitives/interface/Errors.h | 26 +++++------ Primitives/interface/FileStream.h | 15 ++++-- Primitives/interface/FlagEnum.h | 30 +++++++----- Primitives/interface/InterfaceID.h | 14 ++++-- Primitives/interface/MemoryAllocator.h | 12 +++-- Primitives/interface/Object.h | 11 +++-- Primitives/interface/ReferenceCounters.h | 11 +++-- 11 files changed, 209 insertions(+), 80 deletions(-) create mode 100644 Primitives/interface/CommonDefinitions.h (limited to 'Primitives/interface') diff --git a/Primitives/interface/BasicTypes.h b/Primitives/interface/BasicTypes.h index 07da992a..9be3c03a 100644 --- a/Primitives/interface/BasicTypes.h +++ b/Primitives/interface/BasicTypes.h @@ -27,33 +27,42 @@ #pragma once -#include -#include +#include "CommonDefinitions.h" -namespace Diligent -{ +#if DILIGENT_C_INTERFACE +# include +# include +#else +# include +# include +#endif -using Float32 = float; ///< 32-bit float +DILIGENT_BEGIN_NAMESPACE(Diligent) -using Int64 = int64_t; ///< 64-bit signed integer -using Int32 = int32_t; ///< 32-bit signed integer -using Int16 = int16_t; ///< 16-bit signed integer -using Int8 = int8_t; ///< 8-bit signed integer +typedef float Float32; ///< 32-bit float -using Uint64 = uint64_t; ///< 64-bit unsigned integer -using Uint32 = uint32_t; ///< 32-bit unsigned integer -using Uint16 = uint16_t; ///< 16-bit unsigned integer -using Uint8 = uint8_t; ///< 8-bit unsigned integer +typedef int64_t Int64; ///< 64-bit signed integer +typedef int32_t Int32; ///< 32-bit signed integer +typedef int16_t Int16; ///< 16-bit signed integer +typedef int8_t Int8; ///< 8-bit signed integer -using SizeType = size_t; -using PVoid = void*; -using CPVoid = const void*; +typedef uint64_t Uint64; ///< 64-bit unsigned integer +typedef uint32_t Uint32; ///< 32-bit unsigned integer +typedef uint16_t Uint16; ///< 16-bit unsigned integer +typedef uint8_t Uint8; ///< 8-bit unsigned integer -using Bool = bool; ///< Boolean -static constexpr Bool False = false; -static constexpr Bool True = true; +typedef size_t SizeType; +typedef void* PVoid; +typedef const void* CPVoid; -using Char = char; +typedef bool Bool; ///< Boolean + +static const Bool False = false; +static const Bool True = true; + +typedef char Char; +#if !DILIGENT_C_INTERFACE using String = std::basic_string; ///< String variable +#endif -} // namespace Diligent +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/CommonDefinitions.h b/Primitives/interface/CommonDefinitions.h new file mode 100644 index 00000000..74fed6ca --- /dev/null +++ b/Primitives/interface/CommonDefinitions.h @@ -0,0 +1,80 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov + * + * 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. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +/// \file +/// Common definitions + +#ifndef DILIGENT_C_INTERFACE +# ifdef __cplusplus +# define DILIGENT_C_INTERFACE 0 +# else +# define DILIGENT_C_INTERFACE 1 +# endif +#endif + +#if DILIGENT_C_INTERFACE + +# define DILIGENT_BEGIN_NAMESPACE(Name) +# define DILIGENT_END_NAMESPACE + +# define DILIGENT_TYPED_ENUM(EnumName, EnumType) \ + typedef EnumType EnumName; \ + enum _##EnumName + +# define DILIGENT_DERIVE(TypeName) \ + { \ + struct TypeName _##TypeName; + +# define DEFAULT_INITIALIZER(x) + +#else + +# define DILIGENT_BEGIN_NAMESPACE(Name) \ + namespace Name \ + { +# define DILIGENT_END_NAMESPACE } + +# define DILIGENT_TYPED_ENUM(EnumName, EnumType) enum EnumName : EnumType + +# define DILIGENT_DERIVE(TypeName) : public TypeName \ + { + +# define DEFAULT_INITIALIZER(x) = x + +#endif + +#ifndef __cplusplus +# define class struct +#endif + +#if DILIGENT_C_INTERFACE +# define DILIGENT_CPP_INTERFACE 0 +#else +# define DILIGENT_CPP_INTERFACE 1 +#endif diff --git a/Primitives/interface/DataBlob.h b/Primitives/interface/DataBlob.h index abe5a7e8..aa284a6f 100644 --- a/Primitives/interface/DataBlob.h +++ b/Primitives/interface/DataBlob.h @@ -32,13 +32,15 @@ #include "Object.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) + // {F578FF0D-ABD2-4514-9D32-7CB454D4A73B} -static constexpr INTERFACE_ID IID_DataBlob = +static const struct INTERFACE_ID IID_DataBlob = {0xf578ff0d, 0xabd2, 0x4514, {0x9d, 0x32, 0x7c, 0xb4, 0x54, 0xd4, 0xa7, 0x3b}}; +#if DILIGENT_CPP_INTERFACE + /// Base interface for a file stream class IDataBlob : public IObject { @@ -53,4 +55,8 @@ public: virtual void* GetDataPtr() = 0; }; -} // namespace Diligent +#else + +#endif + +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/DebugOutput.h b/Primitives/interface/DebugOutput.h index afa708f0..ca65c132 100644 --- a/Primitives/interface/DebugOutput.h +++ b/Primitives/interface/DebugOutput.h @@ -29,23 +29,22 @@ #include "BasicTypes.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) /// Describes debug message severity -enum class DebugMessageSeverity : Int32 +enum DEBUG_MESSAGE_SEVERITY { /// Information message - Info = 0, + DEBUG_MESSAGE_SEVERITY_INFO = 0, /// Warning message - Warning, + DEBUG_MESSAGE_SEVERITY_WARNING, /// Error, with potential recovery - Error, + DEBUG_MESSAGE_SEVERITY_ERROR, /// Fatal error - recovery is not possible - FatalError + DEBUG_MESSAGE_SEVERITY_FATAL_ERROR }; @@ -56,11 +55,11 @@ enum class DebugMessageSeverity : Int32 /// \param [in] Function - Name of the function or nullptr /// \param [in] Function - File name or nullptr /// \param [in] Line - Line number -using DebugMessageCallbackType = void (*)(DebugMessageSeverity Severity, - const Char* Message, - const char* Function, - const char* File, - int Line); +typedef void (*DebugMessageCallbackType)(enum DEBUG_MESSAGE_SEVERITY Severity, + const Char* Message, + const Char* Function, + const Char* File, + int Line); extern DebugMessageCallbackType DebugMessageCallback; @@ -70,4 +69,4 @@ extern DebugMessageCallbackType DebugMessageCallback; /// wants to use the callback. void SetDebugMessageCallback(DebugMessageCallbackType DbgMessageCallback); -} // namespace Diligent +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/Errors.h b/Primitives/interface/Errors.h index 417fe87e..4280c1bf 100644 --- a/Primitives/interface/Errors.h +++ b/Primitives/interface/Errors.h @@ -59,7 +59,7 @@ void LogError(bool IsFatal, const char* Function, const char* FullFilePath, int auto Msg = FormatString(Args...); if (DebugMessageCallback != nullptr) { - DebugMessageCallback(IsFatal ? DebugMessageSeverity::FatalError : DebugMessageSeverity::Error, Msg.c_str(), Function, FileName.c_str(), Line); + DebugMessageCallback(IsFatal ? DEBUG_MESSAGE_SEVERITY_FATAL_ERROR : DEBUG_MESSAGE_SEVERITY_ERROR, Msg.c_str(), Function, FileName.c_str(), Line); } else { @@ -118,10 +118,10 @@ void LogError(bool IsFatal, const char* Function, const char* FullFilePath, int if (Diligent::DebugMessageCallback != nullptr) Diligent::DebugMessageCallback(Severity, _msg.c_str(), nullptr, nullptr, 0); \ } while (false) -#define LOG_FATAL_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DebugMessageSeverity::FatalError, ##__VA_ARGS__) -#define LOG_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DebugMessageSeverity::Error, ##__VA_ARGS__) -#define LOG_WARNING_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DebugMessageSeverity::Warning, ##__VA_ARGS__) -#define LOG_INFO_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DebugMessageSeverity::Info, ##__VA_ARGS__) +#define LOG_FATAL_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__) +#define LOG_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__) +#define LOG_WARNING_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__) +#define LOG_INFO_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__) #define LOG_DEBUG_MESSAGE_ONCE(Severity, ...) \ @@ -135,10 +135,10 @@ void LogError(bool IsFatal, const char* Function, const char* FullFilePath, int } \ } while (false) -#define LOG_FATAL_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DebugMessageSeverity::FatalError, ##__VA_ARGS__) -#define LOG_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DebugMessageSeverity::Error, ##__VA_ARGS__) -#define LOG_WARNING_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DebugMessageSeverity::Warning, ##__VA_ARGS__) -#define LOG_INFO_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DebugMessageSeverity::Info, ##__VA_ARGS__) +#define LOG_FATAL_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__) +#define LOG_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__) +#define LOG_WARNING_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__) +#define LOG_INFO_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__) #define CHECK(Expr, Severity, ...) \ @@ -150,10 +150,10 @@ void LogError(bool IsFatal, const char* Function, const char* FullFilePath, int } \ } while (false) -#define CHECK_FATAL_ERR(Expr, ...) CHECK(Expr, Diligent::DebugMessageSeverity::FatalError, ##__VA_ARGS__) -#define CHECK_ERR(Expr, ...) CHECK(Expr, Diligent::DebugMessageSeverity::Error, ##__VA_ARGS__) -#define CHECK_WARN(Expr, ...) CHECK(Expr, Diligent::DebugMessageSeverity::Warning, ##__VA_ARGS__) -#define CHECK_INFO(Expr, ...) CHECK(Expr, Diligent::DebugMessageSeverity::Info, ##__VA_ARGS__) +#define CHECK_FATAL_ERR(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__) +#define CHECK_ERR(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__) +#define CHECK_WARN(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__) +#define CHECK_INFO(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__) #define CHECK_THROW(Expr, ...) \ do \ diff --git a/Primitives/interface/FileStream.h b/Primitives/interface/FileStream.h index bfd3e955..91ca581e 100644 --- a/Primitives/interface/FileStream.h +++ b/Primitives/interface/FileStream.h @@ -33,14 +33,17 @@ #include "Object.h" #include "DataBlob.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) + /// IFileStream interface unique identifier // {E67F386C-6A5A-4A24-A0CE-C66435465D41} -static constexpr INTERFACE_ID IID_FileStream = +static const struct INTERFACE_ID IID_FileStream = {0xe67f386c, 0x6a5a, 0x4a24, {0xa0, 0xce, 0xc6, 0x64, 0x35, 0x46, 0x5d, 0x41}}; + +#if DILIGENT_CPP_INTERFACE + /// Base interface for a file stream class IFileStream : public IObject { @@ -58,4 +61,8 @@ public: virtual bool IsValid() = 0; }; -} // namespace Diligent +#else + +#endif + +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/FlagEnum.h b/Primitives/interface/FlagEnum.h index 4eb9a5b1..574e561a 100644 --- a/Primitives/interface/FlagEnum.h +++ b/Primitives/interface/FlagEnum.h @@ -29,17 +29,25 @@ #include "BasicTypes.h" +#if DILIGENT_C_INTERFACE + +# define DEFINE_FLAG_ENUM_OPERATORS(ENUMTYPE) + +#else + template using _UNDERLYING_ENUM_T = typename std::underlying_type::type; -#define DEFINE_FLAG_ENUM_OPERATORS(ENUMTYPE) \ - extern "C++" \ - { \ - inline ENUMTYPE& operator|=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) |= static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline ENUMTYPE& operator&=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) &= static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline ENUMTYPE& operator^=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) ^= static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline constexpr ENUMTYPE operator|(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) | static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline constexpr ENUMTYPE operator&(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) & static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline constexpr ENUMTYPE operator^(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) ^ static_cast<_UNDERLYING_ENUM_T>(b)); } \ - inline constexpr ENUMTYPE operator~(ENUMTYPE a) throw() { return static_cast(~static_cast<_UNDERLYING_ENUM_T>(a)); } \ - } +# define DEFINE_FLAG_ENUM_OPERATORS(ENUMTYPE) \ + extern "C++" \ + { \ + inline ENUMTYPE& operator|=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) |= static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline ENUMTYPE& operator&=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) &= static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline ENUMTYPE& operator^=(ENUMTYPE& a, ENUMTYPE b) throw() { return reinterpret_cast(reinterpret_cast<_UNDERLYING_ENUM_T&>(a) ^= static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline constexpr ENUMTYPE operator|(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) | static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline constexpr ENUMTYPE operator&(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) & static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline constexpr ENUMTYPE operator^(ENUMTYPE a, ENUMTYPE b) throw() { return static_cast(static_cast<_UNDERLYING_ENUM_T>(a) ^ static_cast<_UNDERLYING_ENUM_T>(b)); } \ + inline constexpr ENUMTYPE operator~(ENUMTYPE a) throw() { return static_cast(~static_cast<_UNDERLYING_ENUM_T>(a)); } \ + } + +#endif \ No newline at end of file diff --git a/Primitives/interface/InterfaceID.h b/Primitives/interface/InterfaceID.h index c670e49c..1d04f10b 100644 --- a/Primitives/interface/InterfaceID.h +++ b/Primitives/interface/InterfaceID.h @@ -27,12 +27,14 @@ #pragma once -#include +#if DILIGENT_CPP_INTERFACE +# include +#endif + #include "BasicTypes.h" /// Unique identification structures -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) /// Unique interface identifier struct INTERFACE_ID @@ -42,6 +44,7 @@ struct INTERFACE_ID Uint16 Data3; Uint8 Data4[8]; +#if DILIGENT_CPP_INTERFACE bool operator==(const INTERFACE_ID& rhs) const { return Data1 == rhs.Data1 && @@ -49,9 +52,10 @@ struct INTERFACE_ID Data3 == rhs.Data3 && std::memcmp(Data4, rhs.Data4, sizeof(Data4)) == 0; } +#endif }; /// Unknown interface -static constexpr INTERFACE_ID IID_Unknown = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; +static const struct INTERFACE_ID IID_Unknown = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; -} // namespace Diligent +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/MemoryAllocator.h b/Primitives/interface/MemoryAllocator.h index 4cc95c94..734f507c 100644 --- a/Primitives/interface/MemoryAllocator.h +++ b/Primitives/interface/MemoryAllocator.h @@ -32,8 +32,10 @@ #include "BasicTypes.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) + + +#if DILIGENT_CPP_INTERFACE /// Base interface for a raw memory allocator class IMemoryAllocator @@ -46,4 +48,8 @@ public: virtual void Free(void* Ptr) = 0; }; -} // namespace Diligent +#else + +#endif + +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/Object.h b/Primitives/interface/Object.h index a3191f51..6dc8333f 100644 --- a/Primitives/interface/Object.h +++ b/Primitives/interface/Object.h @@ -33,8 +33,9 @@ #include "InterfaceID.h" #include "ReferenceCounters.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) + +#if DILIGENT_CPP_INTERFACE /// Base interface for all dynamic objects in the engine class IObject @@ -81,4 +82,8 @@ public: virtual IReferenceCounters* GetReferenceCounters() const = 0; }; -} // namespace Diligent +#else + +#endif + +DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Primitives/interface/ReferenceCounters.h b/Primitives/interface/ReferenceCounters.h index 1b36fffc..297d71b8 100644 --- a/Primitives/interface/ReferenceCounters.h +++ b/Primitives/interface/ReferenceCounters.h @@ -32,8 +32,9 @@ #include "InterfaceID.h" -namespace Diligent -{ +DILIGENT_BEGIN_NAMESPACE(Diligent) + +#if DILIGENT_CPP_INTERFACE /// Base interface for a reference counter object that stores the number of strong and /// weak references and the pointer to the object. It is necessary to separate reference @@ -117,4 +118,8 @@ public: virtual CounterValueType GetNumWeakRefs() const = 0; }; -} // namespace Diligent +#else + +#endif + +DILIGENT_END_NAMESPACE // namespace Diligent -- cgit v1.2.3