vsg: include/vsg/core/Inherit.h Source File

vsg 1.1.8 VulkanSceneGraph library

Loading...
Searching...
No Matches
Inherit.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#include <vsg/app/RecordTraversal.h>
16#include <vsg/core/ConstVisitor.h>
17#include <vsg/core/Visitor.h>
18#include <vsg/core/ref_ptr.h>
19#include <vsg/core/type_name.h>
20
21namespace vsg
22{
23
26 template<class ParentClass, class Subclass>
27 class Inherit : public ParentClass
28 {
29 public:
30 template<typename... Args>
31 Inherit(Args&&... args) :
32 ParentClass(std::forward<Args>(args)...) {}
33
34 template<typename... Args>
35 static ref_ptr<Subclass> create(Args&&... args)
36 {
37 return ref_ptr<Subclass>(new Subclass(std::forward<Args>(args)...));
38 }
39
40 template<typename... Args>
41 static ref_ptr<Subclass> create_if(bool flag, Args&&... args)
42 {
43 if (flag) return ref_ptr<Subclass>(new Subclass(std::forward<Args>(args)...));
44 return {};
45 }
46
47 std::size_t sizeofObject() const noexcept override { return sizeof(Subclass); }
48 const char* className() const noexcept override { return type_name<Subclass>(); }
49 const std::type_info& type_info() const noexcept override { return typeid(Subclass); }
50 bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Subclass) == type || ParentClass::is_compatible(type); }
51
52 int compare(const Object& rhs) const override
53 {
54 int result = ParentClass::compare(rhs);
55 if (result != 0) return result;
56
57 size_t startOfSubclass = sizeof(ParentClass);
58 size_t size = sizeof(Subclass) - startOfSubclass;
59
60 // Subclass adds no extra data to compare
61 if (size == 0) return 0;
62
63 const char* lhs_ptr = reinterpret_cast<const char*>(this);
64 const char* rhs_ptr = reinterpret_cast<const char*>(&rhs);
65
66 // compare the data that Subclass adds over ParentClass
67 return std::memcmp(lhs_ptr + startOfSubclass, rhs_ptr + startOfSubclass, size);
68 }
69
70 void accept(Visitor& visitor) override { visitor.apply(static_cast<Subclass&>(*this)); }
71 void accept(ConstVisitor& visitor) const override { visitor.apply(static_cast<const Subclass&>(*this)); }
72 void accept(RecordTraversal& visitor) const override { visitor.apply(static_cast<const Subclass&>(*this)); }
73 };
74
75} // namespace vsg
vsg::ConstVisitor
Definition ConstVisitor.h:172
vsg::Inherit
Definition Inherit.h:28
vsg::Object
Definition Object.h:60
vsg::RecordTraversal
RecordTraversal traverses a scene graph doing view frustum culling and invoking state/commands to rec...
Definition RecordTraversal.h:69
vsg::Visitor
Definition Visitor.h:172
vsg::ref_ptr
Definition ref_ptr.h:22