nx_metadata_sdk 1.0 Metadata SDK

interface.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <cstring>
6
7 #include <nx/sdk/uuid.h>
8
9 #include "i_ref_countable.h"
10
11 namespace nx {
12 namespace sdk {
13
48 template<class DerivedInterface, class BaseInterface = IRefCountable>
49 class Interface: public BaseInterface
50 {
51 public:
52 using IRefCountable::queryInterface; //< Needed to enable overloaded template versions.
53
54 protected:
55 virtual IRefCountable* queryInterface(const IRefCountable::InterfaceId* id) override
56 {
57 return doQueryInterface(id);
58 }
59
61 IRefCountable* queryInterfaceSupportingDeprecatedId(
62 const IRefCountable::InterfaceId* id,
63 const Uuid& deprecatedInterfaceId)
64 {
65 static_assert(Uuid::size() == IRefCountable::InterfaceId::minSize(),
66 "Broken compatibility with old SDK");
67 if (memcmp(id, deprecatedInterfaceId.data(), Uuid::size()) == 0)
68 {
69 this->addRef();
70 // The cast is needed to shift the pointer in case of multiple inheritance.
71 return static_cast<DerivedInterface*>(this);
72 }
73 return doQueryInterface(id);
74 }
75
76 private:
77 static_assert(std::is_base_of<IRefCountable, BaseInterface>::value,
78 "Template parameter BaseInterface should be derived from IRefCountable");
79
84 Interface()
85 {
86 // Assure that DerivedInterface has interfaceId().
87 (void) &DerivedInterface::interfaceId;
88 }
89
90 friend DerivedInterface;
91
92 IRefCountable* doQueryInterface(const IRefCountable::InterfaceId* id)
93 {
94 if (*DerivedInterface::interfaceId() == *id)
95 {
96 this->addRef();
97 // The cast is needed to shift the pointer in case of multiple inheritance.
98 return static_cast<DerivedInterface*>(this);
99 }
100 return BaseInterface::queryInterface(id);
101 }
102 };
103
104 } // namespace sdk
105 } // namespace nx
nx::sdk::Interface::queryInterfaceSupportingDeprecatedId
IRefCountable * queryInterfaceSupportingDeprecatedId(const IRefCountable::InterfaceId *id, const Uuid &deprecatedInterfaceId)
Definition: interface.h:61
nx::sdk::IRefCountable::InterfaceId
Definition: i_ref_countable.h:61
nx::sdk::Interface
Definition: interface.h:49
nx::sdk::Uuid
Definition: uuid.h:22
nx
Definition: apple_utils.h:6
nx::sdk::IRefCountable
Definition: i_ref_countable.h:49