Skip to content

Commit

Permalink
Fix static-analysis issues: apply hidden friend pattern to binary ope…
Browse files Browse the repository at this point in the history
…rators
  • Loading branch information
egorodet committed Dec 29, 2024
1 parent 59a6225 commit 2bef25d
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 271 deletions.
8 changes: 0 additions & 8 deletions Apps/07-ParallelRendering/ParallelRenderingApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ constexpr char const* g_app_variant_name = "Root Constants";
constexpr char const* g_app_variant_name = "Buffer Views";
#endif

bool operator==(const ParallelRenderingApp::Settings& left,
const ParallelRenderingApp::Settings& right) noexcept
{
META_FUNCTION_TASK();
return std::tie(left.cubes_grid_size, left.render_thread_count, left.parallel_rendering_enabled) ==
std::tie(right.cubes_grid_size, right.render_thread_count, right.parallel_rendering_enabled);
}

uint32_t ParallelRenderingApp::Settings::GetTotalCubesCount() const noexcept
{
META_FUNCTION_TASK();
Expand Down
6 changes: 5 additions & 1 deletion Apps/07-ParallelRendering/ParallelRenderingApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class ParallelRenderingApp final // NOSONAR - destructor required
uint32_t render_thread_count = std::thread::hardware_concurrency();
bool parallel_rendering_enabled = true;

friend bool operator==(const Settings& left, const Settings& right) noexcept;
friend bool operator==(const Settings& left, const Settings& right) noexcept
{
return std::tie(left.cubes_grid_size, left.render_thread_count, left.parallel_rendering_enabled) ==
std::tie(right.cubes_grid_size, right.render_thread_count, right.parallel_rendering_enabled);
}

uint32_t GetTotalCubesCount() const noexcept;
uint32_t GetActiveRenderThreadCount() const noexcept;
Expand Down
79 changes: 49 additions & 30 deletions Modules/Data/Types/Include/Methane/Data/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,30 @@ class RawVector // NOSONAR - class has more than 35 methods
template<size_t sz = size, typename = std::enable_if_t<sz == 4>>
[[nodiscard]] HlslVector<T, 4> AsHlsl() const noexcept { return HlslVector<T, 4>(GetX(), GetY(), GetZ(), GetW()); }

[[nodiscard]] bool operator==(const RawVectorType& other) const noexcept
[[nodiscard]] friend bool operator==(const RawVectorType& left, const RawVectorType& right) noexcept
{
if (m_components[0] != other[0])
if (left.m_components[0] != right[0])
{
return false;
}
if (m_components[1] != other[1])
if (left.m_components[1] != right[1])
{
return false;
}
if constexpr (size > 2)
{
if (m_components[2] != other[2])
if (left.m_components[2] != right[2])
return false;
}
if constexpr (size > 3)
{
if (m_components[3] != other[3])
if (left.m_components[3] != right[3])
return false;
}
return true;
}

[[nodiscard]] bool operator!=(const RawVectorType& other) const noexcept { return !operator==(other); }
[[nodiscard]] friend bool operator!=(const RawVectorType& left, const RawVectorType& right) noexcept { return !(left == right); }

RawVectorType& operator*=(T multiplier) noexcept
{
Expand All @@ -219,14 +219,14 @@ class RawVector // NOSONAR - class has more than 35 methods
return *this;
}

[[nodiscard]] RawVectorType operator*(T multiplier) const noexcept
[[nodiscard]] friend RawVectorType operator*(const RawVectorType& v, T multiplier) noexcept
{
return UnrollComputeComponents([multiplier](T component, size_t) { return component * multiplier; });
return UnrollComputeComponents(v, [multiplier](T component, size_t) { return component * multiplier; });
}

[[nodiscard]] RawVectorType operator/(T divisor) const noexcept
[[nodiscard]] friend RawVectorType operator/(const RawVectorType& v, T divisor) noexcept
{
return UnrollComputeComponents([divisor](T component, size_t) { return component / divisor; });
return UnrollComputeComponents(v, [divisor](T component, size_t) { return component / divisor; });
}

RawVectorType& operator+=(const RawVectorType& other) noexcept
Expand All @@ -241,14 +241,14 @@ class RawVector // NOSONAR - class has more than 35 methods
return *this;
}

[[nodiscard]] RawVectorType operator+(const RawVectorType& other) const noexcept
[[nodiscard]] friend RawVectorType operator+(const RawVectorType& v, const RawVectorType& other) noexcept
{
return UnrollComputeComponents([&other](T component, size_t i) { return component + other[i]; });
return UnrollComputeComponents(v, [&other](T component, size_t i) { return component + other[i]; });
}

[[nodiscard]] RawVectorType operator-(const RawVectorType& other) const noexcept
[[nodiscard]] friend RawVectorType operator-(const RawVectorType& v, const RawVectorType& other) noexcept
{
return UnrollComputeComponents([&other](T component, size_t i) { return component - other[i]; });
return UnrollComputeComponents(v, [&other](T component, size_t i) { return component - other[i]; });
}

[[nodiscard]] T GetLength() const noexcept
Expand Down Expand Up @@ -285,43 +285,62 @@ class RawVector // NOSONAR - class has more than 35 methods

private:
template<typename ComponentFn /* [](T component) -> void */>
constexpr void ForEachComponent(ComponentFn component_action) const noexcept
static constexpr void ForEachComponent(const RawVectorType& v, ComponentFn component_action) noexcept
{
component_action(m_components[0]);
component_action(m_components[1]);
component_action(v.m_components[0]);
component_action(v.m_components[1]);
if constexpr (size >= 3)
component_action(m_components[2]);
component_action(v.m_components[2]);
if constexpr (size == 4)
component_action(m_components[3]);
component_action(v.m_components[3]);
}

template<typename ComponentFn /* [](T component) -> void */>
constexpr void ForEachComponent(ComponentFn component_action) const noexcept
{
ForEachComponent(*this, component_action);
}

template<typename ComponentFn /* [](T& component, size_t index) -> void */>
constexpr void UnrollUpdateComponents(ComponentFn component_modifier) noexcept
static constexpr void UnrollUpdateComponents(RawVectorType& v, ComponentFn component_modifier) noexcept
{
component_modifier(m_components[0], 0);
component_modifier(m_components[1], 1);
component_modifier(v.m_components[0], 0);
component_modifier(v.m_components[1], 1);
if constexpr (size >= 3)
component_modifier(m_components[2], 2);
component_modifier(v.m_components[2], 2);
if constexpr (size == 4)
component_modifier(m_components[3], 3);
component_modifier(v.m_components[3], 3);
}

template<typename ComponentFn /* [](T& component, size_t index) -> void */>
constexpr void UnrollUpdateComponents(ComponentFn component_modifier) noexcept
{
UnrollUpdateComponents(*this, component_modifier);
}

template<typename ComponentFn /* [](T component, size_t index) -> T */>
constexpr RawVectorType UnrollComputeComponents(ComponentFn component_compute) const noexcept
static constexpr RawVectorType UnrollComputeComponents(const RawVectorType& v, ComponentFn component_compute) noexcept
{
RawVectorType result;
result[0] = component_compute(m_components[0], 0);
result[1] = component_compute(m_components[1], 1);
result[0] = component_compute(v.m_components[0], 0);
result[1] = component_compute(v.m_components[1], 1);
if constexpr (size >= 3)
result[2] = component_compute(m_components[2], 2);
result[2] = component_compute(v.m_components[2], 2);
if constexpr (size == 4)
result[3] = component_compute(m_components[3], 3);
result[3] = component_compute(v.m_components[3], 3);
return result;
}

template<typename ComponentFn /* [](T component, size_t index) -> T */>
constexpr RawVectorType UnrollComputeComponents(ComponentFn component_compute) const noexcept
{
return UnrollComputeComponents(*this, component_compute);
}

std::array<T, size> m_components{{ }};

static_assert(sizeof(m_components) == sizeof(T) * size, "RawVector components raw array does not satisfy to dense memory packing requirement.");
static_assert(sizeof(m_components) == sizeof(T) * size,
"RawVector components raw array does not satisfy to dense memory packing requirement.");
};

using RawVector2F = RawVector<float, 2>;
Expand Down
6 changes: 5 additions & 1 deletion Modules/Graphics/Mesh/Include/Methane/Graphics/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ class Mesh

Edge(Mesh::Index v1_index, Mesh::Index v2_index);

[[nodiscard]] bool operator<(const Edge& other) const;
[[nodiscard]] friend bool operator<(const Edge& left, const Edge& right)
{
return left.first_index < right.first_index ||
(left.first_index == right.first_index && left.second_index < right.second_index);
}
};

using VertexFieldOffsets = std::array<int32_t, static_cast<size_t>(VertexField::Count)>;
Expand Down
6 changes: 0 additions & 6 deletions Modules/Graphics/Mesh/Sources/Methane/Graphics/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,5 @@ Mesh::Edge::Edge(Mesh::Index v1_index, Mesh::Index v2_index)
, second_index(v1_index < v2_index ? v2_index : v1_index)
{
}

bool Mesh::Edge::operator<(const Edge& other) const
{
return first_index < other.first_index ||
(first_index == other.first_index && second_index < other.second_index);
}

} // namespace Methane::Graphics
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class SkyBox::Impl
return ProgramBindingsAndUniformArgumentBinding(std::move(program_bindings), &uniforms_arg_binding);
}

void Update(Rhi::IProgramArgumentBinding& uniforms_argument_binding)
void Update(Rhi::IProgramArgumentBinding& uniforms_argument_binding) const
{
META_FUNCTION_TASK();
uniforms_argument_binding.SetRootConstant(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ ProgramBindings::operator std::string() const
void ProgramBindings::Initialize()
{
META_FUNCTION_TASK();
auto& program = static_cast<Program&>(GetProgram());
const auto& program = static_cast<const Program&>(GetProgram());
Rhi::IDescriptorManager& descriptor_manager = program.GetContext().GetDescriptorManager();
descriptor_manager.AddProgramBindings(*this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ struct BufferSettings
[[nodiscard]] static BufferSettings ForConstantBuffer(Data::Size size, bool addressable = false, bool is_volatile = false);
[[nodiscard]] static BufferSettings ForReadBackBuffer(Data::Size size);

bool operator==(const BufferSettings& other) const;
bool operator!=(const BufferSettings& other) const;
friend bool operator==(const BufferSettings& left, const BufferSettings& right)
{
return std::tie(left.type, left.usage_mask, left.size, left.item_stride_size, left.data_format, left.storage_mode)
== std::tie(right.type, right.usage_mask, right.size, right.item_stride_size, right.data_format, right.storage_mode);
}

friend bool operator!=(const BufferSettings& left, const BufferSettings& right)
{
return !(left == right);
}
};

struct IBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ struct ShaderMacroDefinition
explicit ShaderMacroDefinition(std::string name);
ShaderMacroDefinition(std::string name, std::string value);

bool operator==(const ShaderMacroDefinition& other) const noexcept;
bool operator!=(const ShaderMacroDefinition& other) const noexcept;
friend bool operator==(const ShaderMacroDefinition& left, const ShaderMacroDefinition& right) noexcept
{
return std::tie(left.name, left.value) == std::tie(right.name, right.value);
}

friend bool operator!=(const ShaderMacroDefinition& left, const ShaderMacroDefinition& right) noexcept
{
return !(left == right);
}

[[nodiscard]] static std::string ToString(const std::vector<ShaderMacroDefinition>& macro_definitions,
std::string_view splitter = ", ") noexcept;
Expand All @@ -71,8 +78,16 @@ struct ShaderEntryFunction
std::string file_name;
std::string function_name;

bool operator==(const ShaderEntryFunction& other) const noexcept;
bool operator!=(const ShaderEntryFunction& other) const noexcept;
friend bool operator==(const ShaderEntryFunction& left, const ShaderEntryFunction& right) noexcept
{
return std::tie(left.file_name, left.function_name)
== std::tie(right.file_name, right.function_name);
}

friend bool operator!=(const ShaderEntryFunction& left, const ShaderEntryFunction& right) noexcept
{
return !(left == right);
}
};

struct ShaderSettings
Expand All @@ -85,8 +100,19 @@ struct ShaderSettings
std::string source_file_path;
std::string source_compile_target;

bool operator==(const ShaderSettings& other) const noexcept;
bool operator!=(const ShaderSettings& other) const noexcept;
friend bool operator==(const ShaderSettings& left, const ShaderSettings& right) noexcept
{
if (std::addressof(left.data_provider) != std::addressof(right.data_provider))
return false;

return std::tie(left.entry_function, left.compile_definitions, left.source_file_path, left.source_compile_target)
== std::tie(right.entry_function, right.compile_definitions, right.source_file_path, right.source_compile_target);
}

friend bool operator!=(const ShaderSettings& left, const ShaderSettings& right) noexcept
{
return !(left == right);
}
};

struct IContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class TextureView
const SubResource::Count& subresource_count = {},
Opt<TextureDimensionType> texture_dimension_type_opt = {});

using ResourceView::operator==;
using ResourceView::operator!=;
using ResourceView::operator std::string;

[[nodiscard]] const Ptr<ITexture>& GetTexturePtr() const noexcept { return m_texture_ptr; }
Expand Down Expand Up @@ -82,8 +80,18 @@ struct TextureSettings
Opt<Data::Index> frame_index_opt; // for TextureType::FrameBuffer
Opt<DepthStencilValues> depth_stencil_clear_opt; // for TextureType::DepthStencil

bool operator==(const TextureSettings& other) const;
bool operator!=(const TextureSettings& other) const;
friend bool operator==(const TextureSettings& left, const TextureSettings& right)
{
return std::tie(left.type, left.dimension_type, left.usage_mask, left.pixel_format, left.dimensions,
left.array_length, left.mipmapped, left.frame_index_opt, left.depth_stencil_clear_opt)
== std::tie(right.type, right.dimension_type, right.usage_mask, right.pixel_format, right.dimensions,
right.array_length, right.mipmapped, right.frame_index_opt, right.depth_stencil_clear_opt);
}

friend bool operator!=(const TextureSettings& left, const TextureSettings& right)
{
return !(left == right);
}

[[nodiscard]] static TextureSettings ForImage(const Dimensions& dimensions, const Opt<uint32_t>& array_length_opt, PixelFormat pixel_format, bool mipmapped,
ResourceUsageMask usage = { ResourceUsage::ShaderRead });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ class ProgramArgument
[[nodiscard]] std::string_view GetName() const noexcept { return m_name; }
[[nodiscard]] size_t GetHash() const noexcept { return m_hash; }

[[nodiscard]] bool operator==(const ProgramArgument& other) const noexcept;
[[nodiscard]] bool operator<(const ProgramArgument& other) const noexcept;
[[nodiscard]] friend bool operator==(const ProgramArgument& left, const ProgramArgument& right) noexcept
{
return std::tie(left.m_hash, left.m_shader_type, left.m_name)
== std::tie(right.m_hash, right.m_shader_type, right.m_name);
}

[[nodiscard]] friend bool operator<(const ProgramArgument& left, const ProgramArgument& right) noexcept
{
return std::tie(left.m_hash, left.m_shader_type, left.m_name)
< std::tie(right.m_hash, right.m_shader_type, right.m_name);
}

[[nodiscard]] virtual explicit operator std::string() const noexcept;

void MergeShaderTypes(ShaderType shader_type);
Expand Down
Loading

0 comments on commit 2bef25d

Please sign in to comment.