Skip to content

Commit 74f77ca

Browse files
committed
Allow specialisation of vsg::Inherit for sublasses, alternative implementation.
Like e07a87f / vsg-dev#1609, but a bit simpler and easier to use
1 parent 3c6c4fa commit 74f77ca

File tree

5 files changed

+44
-94
lines changed

5 files changed

+44
-94
lines changed

include/vsg/core/Inherit.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
2020

2121
namespace vsg
2222
{
23+
template<class ParentClass, class Subclass, class Enable = void>
24+
struct InheritHelper
25+
{
26+
using Type = ParentClass;
27+
};
28+
29+
template<class ParentClass, class Subclass>
30+
struct InheritHelper<ParentClass, Subclass, typename ParentClass::template InheritExtras<ParentClass, Subclass>>
31+
{
32+
using Type = typename ParentClass::template InheritExtras<ParentClass, Subclass>;
33+
};
2334

2435
/// Inherit<> uses the Curiously Recurring Template Pattern
2536
/// to provide the classes versions of create, accept(..), RTTI and sizeofObject()
2637
template<class ParentClass, class Subclass>
27-
class Inherit : public ParentClass
38+
class Inherit : public InheritHelper<ParentClass, Subclass>::Type
2839
{
2940
public:
41+
using BaseClass = typename InheritHelper<ParentClass, Subclass>::Type;
42+
3043
template<typename... Args>
3144
Inherit(Args&&... args) :
32-
ParentClass(std::forward<Args>(args)...) {}
45+
BaseClass(std::forward<Args>(args)...) {}
3346

3447
template<typename... Args>
3548
static ref_ptr<Subclass> create(Args&&... args)
@@ -47,11 +60,11 @@ namespace vsg
4760
std::size_t sizeofObject() const noexcept override { return sizeof(Subclass); }
4861
const char* className() const noexcept override { return type_name<Subclass>(); }
4962
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); }
63+
bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Subclass) == type || BaseClass::is_compatible(type); }
5164

5265
int compare(const Object& rhs) const override
5366
{
54-
int result = ParentClass::compare(rhs);
67+
int result = BaseClass::compare(rhs);
5568
if (result != 0) return result;
5669

5770
size_t startOfSubclass = sizeof(ParentClass);

include/vsg/state/ArrayState.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ namespace vsg
2727
class VSG_DECLSPEC ArrayState : public Inherit<ConstVisitor, ArrayState>
2828
{
2929
public:
30+
template<class ParentClass, class Subclass>
31+
class InheritExtras : public ParentClass
32+
{
33+
public:
34+
using ParentClass::ParentClass;
35+
36+
explicit InheritExtras(const ArrayState& rhs, const CopyOp& copyop = {}) :
37+
ParentClass(rhs, copyop)
38+
{
39+
}
40+
41+
ref_ptr<ArrayState> cloneArrayState() override
42+
{
43+
return Subclass::create(static_cast<Subclass&>(*this));
44+
}
45+
46+
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override
47+
{
48+
return Subclass::create(static_cast<Subclass&>(*arrayState));
49+
}
50+
};
51+
3052
ArrayState() = default;
3153
ArrayState(const ArrayState& rhs, const CopyOp& copyop = {});
3254

@@ -106,9 +128,6 @@ namespace vsg
106128
NullArrayState();
107129
explicit NullArrayState(const ArrayState& as);
108130

109-
ref_ptr<ArrayState> cloneArrayState() override;
110-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
111-
112131
using ArrayState::apply;
113132

114133
void apply(const vec3Array&) override;
@@ -124,9 +143,6 @@ namespace vsg
124143
TranslationArrayState(const TranslationArrayState& rhs);
125144
explicit TranslationArrayState(const ArrayState& rhs);
126145

127-
ref_ptr<ArrayState> cloneArrayState() override;
128-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
129-
130146
uint32_t translation_attribute_location = 7;
131147
AttributeDetails translationAttribute;
132148

@@ -145,9 +161,6 @@ namespace vsg
145161
TranslationRotationScaleArrayState(const TranslationRotationScaleArrayState& rhs);
146162
explicit TranslationRotationScaleArrayState(const ArrayState& rhs);
147163

148-
ref_ptr<ArrayState> cloneArrayState() override;
149-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
150-
151164
uint32_t translation_attribute_location = 7;
152165
uint32_t rotation_attribute_location = 8;
153166
uint32_t scale_attribute_location = 9;
@@ -168,10 +181,7 @@ namespace vsg
168181
public:
169182
DisplacementMapArrayState();
170183
DisplacementMapArrayState(const DisplacementMapArrayState& rhs);
171-
explicit DisplacementMapArrayState(const ArrayState& rhs);
172-
173-
ref_ptr<ArrayState> cloneArrayState() override;
174-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
184+
explicit DisplacementMapArrayState(const ArrayState& rhs, const CopyOp& copyop = {});
175185

176186
// binding of displacement map
177187
uint32_t normal_attribute_location = 1;
@@ -207,9 +217,6 @@ namespace vsg
207217
uint32_t translation_attribute_location = 7;
208218
AttributeDetails translationAttribute;
209219

210-
ref_ptr<ArrayState> cloneArrayState() override;
211-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
212-
213220
void apply(const VertexInputState& vas) override;
214221
ref_ptr<const vec3Array> vertexArray(uint32_t instanceIndex) override;
215222
};
@@ -223,9 +230,6 @@ namespace vsg
223230
BillboardArrayState(const BillboardArrayState& rhs);
224231
explicit BillboardArrayState(const ArrayState& rhs);
225232

226-
ref_ptr<ArrayState> cloneArrayState() override;
227-
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override;
228-
229233
uint32_t translation_attribute_location = 7;
230234
AttributeDetails translationAttribute;
231235

src/vsg/state/ArrayState.cpp

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,6 @@ NullArrayState::NullArrayState(const ArrayState& as) :
219219
vertices = {};
220220
}
221221

222-
ref_ptr<ArrayState> NullArrayState::cloneArrayState()
223-
{
224-
return NullArrayState::create(*this);
225-
}
226-
227-
// clone the specified ArrayState
228-
ref_ptr<ArrayState> NullArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
229-
{
230-
return NullArrayState::create(*arrayState);
231-
}
232-
233222
void NullArrayState::apply(const vsg::vec3Array&)
234223
{
235224
vertices = {};
@@ -260,16 +249,6 @@ TranslationArrayState::TranslationArrayState(const ArrayState& rhs) :
260249
{
261250
}
262251

263-
ref_ptr<ArrayState> TranslationArrayState::cloneArrayState()
264-
{
265-
return TranslationArrayState::create(*this);
266-
}
267-
268-
ref_ptr<ArrayState> TranslationArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
269-
{
270-
return TranslationArrayState::create(*arrayState);
271-
}
272-
273252
void TranslationArrayState::apply(const VertexInputState& vas)
274253
{
275254
getAttributeDetails(vas, vertex_attribute_location, vertexAttribute);
@@ -315,16 +294,6 @@ TranslationRotationScaleArrayState::TranslationRotationScaleArrayState(const Arr
315294
{
316295
}
317296

318-
ref_ptr<ArrayState> TranslationRotationScaleArrayState::cloneArrayState()
319-
{
320-
return TranslationRotationScaleArrayState::create(*this);
321-
}
322-
323-
ref_ptr<ArrayState> TranslationRotationScaleArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
324-
{
325-
return TranslationRotationScaleArrayState::create(*arrayState);
326-
}
327-
328297
void TranslationRotationScaleArrayState::apply(const VertexInputState& vas)
329298
{
330299
getAttributeDetails(vas, vertex_attribute_location, vertexAttribute);
@@ -373,21 +342,11 @@ DisplacementMapArrayState::DisplacementMapArrayState(const DisplacementMapArrayS
373342
{
374343
}
375344

376-
DisplacementMapArrayState::DisplacementMapArrayState(const ArrayState& rhs) :
377-
Inherit(rhs)
345+
DisplacementMapArrayState::DisplacementMapArrayState(const ArrayState& rhs, const CopyOp& copyop) :
346+
Inherit(rhs, copyop)
378347
{
379348
}
380349

381-
ref_ptr<ArrayState> DisplacementMapArrayState::cloneArrayState()
382-
{
383-
return DisplacementMapArrayState::create(*this);
384-
}
385-
386-
ref_ptr<ArrayState> DisplacementMapArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
387-
{
388-
return DisplacementMapArrayState::create(*arrayState);
389-
}
390-
391350
void DisplacementMapArrayState::apply(const DescriptorImage& di)
392351
{
393352
if (!di.imageInfoList.empty())
@@ -485,16 +444,6 @@ TranslationAndDisplacementMapArrayState::TranslationAndDisplacementMapArrayState
485444
{
486445
}
487446

488-
ref_ptr<ArrayState> TranslationAndDisplacementMapArrayState::cloneArrayState()
489-
{
490-
return TranslationAndDisplacementMapArrayState::create(*this);
491-
}
492-
493-
ref_ptr<ArrayState> TranslationAndDisplacementMapArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
494-
{
495-
return TranslationAndDisplacementMapArrayState::create(*arrayState);
496-
}
497-
498447
void TranslationAndDisplacementMapArrayState::apply(const VertexInputState& vas)
499448
{
500449
getAttributeDetails(vas, vertex_attribute_location, vertexAttribute);
@@ -562,16 +511,6 @@ BillboardArrayState::BillboardArrayState(const ArrayState& rhs) :
562511
{
563512
}
564513

565-
ref_ptr<ArrayState> BillboardArrayState::cloneArrayState()
566-
{
567-
return BillboardArrayState::create(*this);
568-
}
569-
570-
ref_ptr<ArrayState> BillboardArrayState::cloneArrayState(ref_ptr<ArrayState> arrayState)
571-
{
572-
return BillboardArrayState::create(*arrayState);
573-
}
574-
575514
void BillboardArrayState::apply(const VertexInputState& vas)
576515
{
577516
getAttributeDetails(vas, vertex_attribute_location, vertexAttribute);

src/vsg/text/CpuLayoutTechnique.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ class VSG_DECLSPEC CpuLayoutTechniqueArrayState : public Inherit<ArrayState, Cpu
4747
{
4848
}
4949

50-
ref_ptr<ArrayState> cloneArrayState() override
51-
{
52-
return CpuLayoutTechniqueArrayState::create(*this);
53-
}
50+
using Inherit::cloneArrayState;
5451

5552
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override
5653
{

src/vsg/text/GpuLayoutTechnique.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ class VSG_DECLSPEC GpuLayoutTechniqueArrayState : public Inherit<ArrayState, Gpu
5151
{
5252
}
5353

54-
ref_ptr<ArrayState> cloneArrayState() override
55-
{
56-
return GpuLayoutTechniqueArrayState::create(*this);
57-
}
54+
using Inherit::cloneArrayState;
5855

5956
ref_ptr<ArrayState> cloneArrayState(ref_ptr<ArrayState> arrayState) override
6057
{

0 commit comments

Comments
 (0)