Skip to content

Commit

Permalink
Merge pull request #779 from vsg-dev/master
Browse files Browse the repository at this point in the history
Merged fixes to master for vsg::Auxilary::compare() and vsg::Data::compare()
  • Loading branch information
robertosfield authored Apr 12, 2023
2 parents 609781c + e9435c1 commit 7ae3532
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(vsg
LANGUAGES CXX
)
set(VSG_SOVERSION 12)
SET(VSG_RELEASE_CANDIDATE 2)
SET(VSG_RELEASE_CANDIDATE 3)
set(Vulkan_MIN_VERSION 1.1.70.0)

set(VSG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Root source directory of VulkanSceneGraph.")
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/Auxiliary.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace vsg
void unref_nodelete() const;
inline unsigned int referenceCount() const { return _referenceCount.load(); }

virtual int compare(const Auxiliary& rhs) const;

void setObject(const std::string& key, ref_ptr<Object> object)
{
userObjects[key] = object;
Expand Down
20 changes: 1 addition & 19 deletions include/vsg/core/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,7 @@ namespace vsg
std::size_t sizeofObject() const noexcept override { return sizeof(Data); }
bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Data) == type || Object::is_compatible(type); }

int compare(const Object& rhs_object) const override
{
int result = Object::compare(rhs_object);
if (result != 0) return result;

auto& rhs = static_cast<decltype(*this)>(rhs_object);

if ((result = properties.compare(rhs.properties))) return result;

// the shorter data is less
if (dataSize() < rhs.dataSize()) return -1;
if (dataSize() > rhs.dataSize()) return 1;

// if both empty then they must be equal
if (dataSize() == 0) return 0;

// use memcpy to compare the contents of the data
return std::memcmp(dataPointer(), rhs.dataPointer(), dataSize());
}
int compare(const Object& rhs_object) const override;

void read(Input& input) override;
void write(Output& output) const override;
Expand Down
14 changes: 1 addition & 13 deletions include/vsg/core/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,7 @@ namespace vsg
const T* cast() const { return is_compatible(typeid(T)) ? static_cast<const T*>(this) : nullptr; }

/// compare two objects, return -1 if this object is less than rhs, return 0 if it's equal, return 1 if rhs is greater,
virtual int compare(const Object& rhs) const
{
if (this == &rhs) return 0;
auto this_id = std::type_index(typeid(*this));
auto rhs_id = std::type_index(typeid(rhs));
if (this_id < rhs_id) return -1;
if (this_id > rhs_id) return 1;

if (_auxiliary < rhs._auxiliary) return -1;
if (_auxiliary > rhs._auxiliary) return 1;

return 0;
}
virtual int compare(const Object& rhs) const;

virtual void accept(Visitor& visitor);
virtual void traverse(Visitor&) {}
Expand Down
32 changes: 26 additions & 6 deletions include/vsg/core/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,36 @@ namespace vsg
output.write("Value", _value);
}

std::size_t valueSize() const override { return sizeof(value_type); }
std::size_t valueSize() const override
{
if constexpr (std::is_same_v<T, std::string>)
return _value.size();
else
return sizeof(value_type);
}
std::size_t valueCount() const override { return 1; }

bool dataAvailable() const override { return true; }
std::size_t dataSize() const override { return sizeof(value_type); }
std::size_t dataSize() const override { return valueSize(); }

void* dataPointer() override { return &_value; }
const void* dataPointer() const override { return &_value; }
void* dataPointer() override
{
if constexpr (std::is_same_v<T, std::string>)
return _value.data();
else
return &_value;
}

void* dataPointer(size_t) override { return &_value; }
const void* dataPointer(size_t) const override { return &_value; }
const void* dataPointer() const override
{
if constexpr (std::is_same_v<T, std::string>)
return _value.data();
else
return &_value;
}

void* dataPointer(size_t) override { return dataPointer(); }
const void* dataPointer(size_t) const override { return dataPointer(); }

void* dataRelease() override { return nullptr; }

Expand Down Expand Up @@ -167,6 +186,7 @@ namespace vsg
}

VSG_value(stringValue, std::string);

VSG_value(boolValue, bool);
VSG_value(intValue, int);
VSG_value(uintValue, unsigned int);
Expand Down
2 changes: 1 addition & 1 deletion include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ namespace vsg
virtual void apply(floatValue&);
virtual void apply(doubleValue&);
virtual void apply(vec2Value&);
virtual void apply(vec4Value&);
virtual void apply(vec3Value&);
virtual void apply(vec4Value&);
virtual void apply(dvec2Value&);
virtual void apply(dvec3Value&);
virtual void apply(dvec4Value&);
Expand Down
27 changes: 27 additions & 0 deletions src/vsg/core/Auxiliary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/core/Auxiliary.h>
#include <vsg/core/compare.h>
#include <vsg/io/Input.h>
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
Expand Down Expand Up @@ -74,3 +75,29 @@ void Auxiliary::resetConnectedObject()

_connectedObject = nullptr;
}

int Auxiliary::compare(const Auxiliary& rhs) const
{
auto lhs_itr = userObjects.begin();
auto rhs_itr = rhs.userObjects.begin();
int result = 0;
while (lhs_itr != userObjects.end() && rhs_itr != rhs.userObjects.end())
{
if (lhs_itr->first < rhs_itr->first) return -1;
if (lhs_itr->first > rhs_itr->first) return 1;
if ((result = vsg::compare_pointer(lhs_itr->second, rhs_itr->second))) return result;
}

// only can get here if either lhs_itr == userObjects.end() || rhs_itr == rhs.userObjects.end()
if (lhs_itr == userObjects.end())
{
if (rhs_itr != rhs.userObjects.end())
return -1;
else
return 0;
}
else
{
return 1;
}
}
20 changes: 20 additions & 0 deletions src/vsg/core/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ void Data::operator delete(void* ptr)
vsg::deallocate(ptr);
}

int Data::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;

auto& rhs = static_cast<decltype(*this)>(rhs_object);

if ((result = properties.compare(rhs.properties))) return result;

// the shorter data is less
if (dataSize() < rhs.dataSize()) return -1;
if (dataSize() > rhs.dataSize()) return 1;

// if both empty then they must be equal
if (dataSize() == 0) return 0;

// use memcpy to compare the contents of the data
return std::memcmp(dataPointer(), rhs.dataPointer(), dataSize());
}

void Data::read(Input& input)
{
Object::read(input);
Expand Down
12 changes: 12 additions & 0 deletions src/vsg/core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ void Object::_attemptDelete() const
}
}

int Object::compare(const Object& rhs) const
{
if (this == &rhs) return 0;
auto this_id = std::type_index(typeid(*this));
auto rhs_id = std::type_index(typeid(rhs));
if (this_id < rhs_id) return -1;
if (this_id > rhs_id) return 1;

if (_auxiliary == rhs._auxiliary) return 0;
return _auxiliary ? (rhs._auxiliary ? _auxiliary->compare(*rhs._auxiliary) : 1) : (rhs._auxiliary ? -1 : 0);
}

void Object::accept(Visitor& visitor)
{
visitor.apply(*this);
Expand Down

0 comments on commit 7ae3532

Please sign in to comment.