Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions include/vsg/core/Allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2018 Robert Osfield

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

</editor-fold> */

#include <vsg/core/Inherit.h>
#include <vsg/core/ref_ptr.h>
#include <vsg/utils/stream.h>

namespace vsg
{

class Allocator : public Inherit<Object, Allocator>
{
public:
virtual void* allocate(std::size_t n, const void* hint );

virtual void* allocate(std::size_t size);

virtual void deallocate(const void* ptr, std::size_t size=0);

Auxiliary* getOrCreateSharedAuxiliary();

void detachSharedAuxiliary(Auxiliary* auxiliary);

template<typename T, typename... Args>
ref_ptr<T> create(Args&&... args)
{
// need to think about alignment...
std::size_t size = sizeof(T);
void* ptr = allocate(size);
T* object = new (ptr) T(std::forward<Args>(args)...);
object->setAuxiliary(getOrCreateSharedAuxiliary());

std::size_t new_size = object->getSizeOf();
if (new_size != size)
{
throw make_string("Warning: Allocator::create(",typeid(T).name(),") mismatch sizeof() = ",size,", ",new_size);
}
return object;
}

protected:
virtual ~Allocator();

Auxiliary* _sharedAuxiliary = nullptr;
std::size_t _bytesAllocated = 0;
std::size_t _countAllocated = 0;
std::size_t _bytesDeallocated = 0;
std::size_t _countDeallocated = 0;
};

}
2 changes: 2 additions & 0 deletions include/vsg/core/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace vsg
Array(std::initializer_list<value_type> l) : _size(l.size()), _data(new value_type[l.size()]) { value_type* ptr = _data; for (value_type const & v : l) { (*ptr++) = v; } }
Array(const Array& rhs) : _data(rhs.data) {}

std::size_t getSizeOf() const noexcept override { return sizeof(Data); }

// implementation provided by Visitor.h
void accept(Visitor& visitor) override ;

Expand Down
27 changes: 16 additions & 11 deletions include/vsg/core/Auxiliary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

</editor-fold> */

#include <vsg/core/Object.h>
#include <vsg/core/Allocator.h>
#include <vsg/core/ref_ptr.h>

#include <map>
#include <algorithm>

namespace vsg
{
Expand All @@ -25,11 +24,11 @@ namespace vsg
class VSG_EXPORT Auxiliary
{
public:
Auxiliary();

Object* getConnectedObject() { return _connectedObject; }
const Object* getConnectedObject() const { return _connectedObject; }

virtual std::size_t getSizeOf() const { return sizeof(Auxiliary); }

void ref() const;
void unref() const;
void unref_nodelete() const;
Expand All @@ -43,23 +42,29 @@ namespace vsg
ObjectMap& getObjectMap() { return _objectMap; }
const ObjectMap& getObjectMap() const { return _objectMap; }

void setAllocator(Allocator* allocator) { _allocator = allocator; }
Allocator* getAllocator() { return _allocator; }

protected:

virtual ~Auxiliary();
Auxiliary(Allocator* allocator);
Auxiliary(Object* object, Allocator* allocator=nullptr);

void setConnectedObject(Object* object);
virtual ~Auxiliary();

/// reset the ConnectedObject pointer to 0 unless the ConnectedObject referenceCount goes back above 0,
/// return true if ConnectedObject should still be deleted, or false if the object should be kept.
bool signalConnectedObjectToBeDeleted();

friend class Object;

mutable std::atomic_uint _referenceCount;
void resetConnectedObject();

std::atomic<Object*> _connectedObject;
friend class Object;
friend class Allocator;

ObjectMap _objectMap;
mutable std::atomic_uint _referenceCount;
std::atomic<Object*> _connectedObject;
ref_ptr<Allocator> _allocator;
ObjectMap _objectMap;

};

Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace vsg

Data() {};

std::size_t getSizeOf() const noexcept override { return sizeof(Data); }

virtual std::size_t valueSize() const = 0;
virtual std::size_t valueCount() const = 0;

Expand Down
35 changes: 35 additions & 0 deletions include/vsg/core/Inherit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2018 Robert Osfield

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

</editor-fold> */

#include <vsg/core/Visitor.h>
#include <vsg/traversals/DispatchTraversal.h>

namespace vsg
{

// Use the Curiously Reacurring Template Pattern
// to provide the classes versions of accept(..) and getSizeOf()
template<class ParentClass, class Subclass>
class Inherit : public ParentClass
{
public:
template<typename... Args>
Inherit(Args&&... args) : ParentClass(std::forward<Args>(args)...) {}

void accept(Visitor& visitor) override { visitor.apply(static_cast<Subclass&>(*this)); }
void accept(DispatchTraversal& visitor) const override { visitor.apply(static_cast<const Subclass&>(*this)); }
std::size_t getSizeOf() const noexcept override { return sizeof(Subclass); }
};

}
12 changes: 11 additions & 1 deletion include/vsg/core/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace vsg
class Auxiliary;
class Visitor;
class DispatchTraversal;
class Allocator;

class VSG_EXPORT Object
{
Expand All @@ -35,6 +36,8 @@ namespace vsg
Object(const Object&) = delete;
Object& operator = (const Object&) = delete;

virtual std::size_t getSizeOf() const noexcept { return sizeof(Object); }

virtual void accept(Visitor& visitor);
virtual void traverse(Visitor&) {}

Expand Down Expand Up @@ -78,16 +81,23 @@ namespace vsg
Object* getObject(const Key& key);
const Object* getObject(const Key& key) const;

Auxiliary* getOrCreateAuxiliary();
Auxiliary* getOrCreateUniqueAuxiliary();
Auxiliary* getAuxiliary() { return _auxiliary; }
const Auxiliary* getAuxiliary() const { return _auxiliary; }

Allocator* getAllocator() const;

protected:
virtual ~Object();

private:
virtual void _delete() const;

void setAuxiliary(Auxiliary* auxiliary);

friend class Allocator;
friend class Auxiliary;

mutable std::atomic_uint _referenceCount;

Auxiliary* _auxiliary;
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace vsg
Value(const value_type& in_value) : _value(in_value) {}
Value(const Value& rhs) : _value(rhs._value) {}

std::size_t getSizeOf() const noexcept override { return sizeof(Value); }

// implementation provided by Visitor.h
void accept(Visitor& visitor) override;

Expand Down
8 changes: 4 additions & 4 deletions include/vsg/core/observer_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace vsg

template<class R>
observer_ptr(R* ptr):
_auxiliary(ptr ? ptr->getOrCreateAuxiliary() : nullptr)
_auxiliary(ptr ? ptr->getOrCreateUniqueAuxiliary() : nullptr)
{
}

Expand All @@ -43,7 +43,7 @@ namespace vsg

template<class R>
observer_ptr(const ref_ptr<R>& ptr):
_auxiliary(ptr.valid() ? ptr->getOrCreateAuxiliary() : nullptr)
_auxiliary(ptr.valid() ? ptr->getOrCreateUniqueAuxiliary() : nullptr)
{
}

Expand All @@ -54,7 +54,7 @@ namespace vsg
template<class R>
observer_ptr& operator = (R* ptr)
{
_auxiliary = ptr ? ptr->getOrCreateAuxiliary() : nullptr;
_auxiliary = ptr ? ptr->getOrCreateUniqueAuxiliary() : nullptr;
return *this;
}

Expand All @@ -74,7 +74,7 @@ namespace vsg
template<class R>
observer_ptr& operator = (const ref_ptr<R>& rhs)
{
_auxiliary = rhs.valid() ? rhs->getOrCreateAuxiliary() : nullptr;
_auxiliary = rhs.valid() ? rhs->getOrCreateUniqueAuxiliary() : nullptr;
return *this;
}

Expand Down
5 changes: 1 addition & 4 deletions include/vsg/nodes/FixedGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
namespace vsg
{
template<int NUM_CHILDREN>
class FixedGroup : public vsg::Node
class FixedGroup : public Inherit<Node, FixedGroup<NUM_CHILDREN>>
{
public:
FixedGroup() {}

template<class N, class V> static void t_traverse(N& node, V& visitor) { for (auto& child : node._children) child->accept(visitor); }

void accept(Visitor& visitor) override { visitor.apply(*this); }
void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }

void accept(DispatchTraversal& visitor) const override { visitor.apply(*this); }
void traverse(DispatchTraversal& visitor) const override { t_traverse(*this, visitor); }

void setChild(std::size_t pos, vsg::Node* node) { _children[pos] = node; }
Expand Down
6 changes: 2 additions & 4 deletions include/vsg/nodes/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

namespace vsg
{
class VSG_EXPORT Group : public vsg::Node
class VSG_EXPORT Group : public Inherit<Node, Group>
{
public:
Group(size_t numChildren=0);

template<class N, class V> static void t_traverse(N& node, V& visitor) { for (auto& child : node._children) child->accept(visitor); }

void accept(Visitor& visitor) override { visitor.apply(*this); }
void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }

void accept(DispatchTraversal& visitor) const override { visitor.apply(*this); }
void traverse(DispatchTraversal& visitor) const override { t_traverse(*this, visitor); }

std::size_t addChild(vsg::Node* child) { std::size_t pos = _children.size(); _children.push_back(child); return pos; }
Expand All @@ -55,4 +52,5 @@ namespace vsg

Children _children;
};

}
12 changes: 5 additions & 7 deletions include/vsg/nodes/LOD.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ namespace vsg
double radius;
};

class VSG_EXPORT LOD : public vsg::Node
class VSG_EXPORT LOD : public Inherit<Node, LOD>
{
public:
LOD() {}

template<class N, class V> static void t_traverse(N& node, V& visitor) { for (auto& child : node._children) child->accept(visitor); }

inline void accept(Visitor& visitor) override { visitor.apply(*this); }
inline void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }
inline void accept(DispatchTraversal& visitor) const override { visitor.apply(*this); }
inline void traverse(DispatchTraversal& visitor) const override { t_traverse(*this, visitor); }

/// set the BondingSphere to use in culling/computation of which child is active.
Expand All @@ -53,17 +51,17 @@ namespace vsg
void setMinimumArea(std::size_t pos, double area) { _minimumAreas[pos] = area; }
double getMinimumArea(std::size_t pos) const { return _minimumAreas[pos]; }

void setChild(std::size_t pos, vsg::Node* node) { _children[pos] = node;}
vsg::Node* getChild(std::size_t pos) { return _children[pos].get(); }
const vsg::Node* getChild(std::size_t pos) const { return _children[pos].get(); }
void setChild(std::size_t pos, Node* node) { _children[pos] = node;}
Node* getChild(std::size_t pos) { return _children[pos].get(); }
const Node* getChild(std::size_t pos) const { return _children[pos].get(); }

std::size_t getNumChildren() const { return 2; }

using MinimumAreas = std::array< double, 2 > ;
MinimumAreas& getMinimumAreas() { return _minimumAreas; }
const MinimumAreas& getMinimumAreas() const { return _minimumAreas; }

using Children = std::array< ref_ptr< vsg::Node>, 2 >;
using Children = std::array< ref_ptr<Node>, 2 >;
Children& getChildren() { return _children; }
const Children& getChildren() const { return _children; }

Expand Down
9 changes: 2 additions & 7 deletions include/vsg/nodes/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

</editor-fold> */

#include <vsg/core/Object.h>
#include <vsg/core/Visitor.h>
#include <vsg/traversals/DispatchTraversal.h>
#include <vsg/core/Inherit.h>

namespace vsg
{
class VSG_EXPORT Node : public vsg::Object
class VSG_EXPORT Node : public Inherit<Object, Node>
{
public:
Node();

void accept(Visitor& visitor) override { visitor.apply(*this); }
void accept(DispatchTraversal& visitor) const override { visitor.apply(*this); }

protected:

virtual ~Node();
Expand Down
5 changes: 1 addition & 4 deletions include/vsg/nodes/QuadGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

namespace vsg
{
class VSG_EXPORT QuadGroup : public vsg::Node
class VSG_EXPORT QuadGroup : public Inherit<Node, QuadGroup>
{
public:
QuadGroup();

template<class N, class V> static void t_traverse(N& node, V& visitor) { for(int i=0; i<4; ++i) node._children[i]->accept(visitor); }

void accept(Visitor& visitor) override { visitor.apply(*this); }
void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }

void accept(DispatchTraversal& visitor) const override { visitor.apply(*this); }
void traverse(DispatchTraversal& visitor) const override { t_traverse(*this, visitor); }

void setChild(std::size_t pos, vsg::Node* node) { _children[pos] = node; }
Expand Down
Loading