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
27 template<class DerivedInterface, class BaseInterface = IRefCountable>
28 class Interface: public BaseInterface
29 {
30 public:
31 using IRefCountable::queryInterface; //< Needed to enable overloaded template versions.
32
33 protected:
34 virtual IRefCountable* queryInterface(const IRefCountable::InterfaceId* id) override
35 {
36 return doQueryInterface(id);
37 }
38
40 IRefCountable* queryInterfaceSupportingDeprecatedId(
41 const IRefCountable::InterfaceId* id,
42 const Uuid& deprecatedInterfaceId)
43 {
44 static_assert(Uuid::size() == IRefCountable::InterfaceId::minSize(),
45 "Broken compatibility with old SDK");
46 if (memcmp(id, deprecatedInterfaceId.data(), Uuid::size()) == 0)
47 {
48 this->addRef();
49 // The cast is needed to shift the pointer in case of multiple inheritance.
50 return static_cast<DerivedInterface*>(this);
51 }
52 return doQueryInterface(id);
53 }
54
55 private:
56 static_assert(std::is_base_of<IRefCountable, BaseInterface>::value,
57 "Template parameter BaseInterface should be derived from IRefCountable");
58
59 // Statically assure that DerivedInterface is inherited from this class.
60 Interface() = default;
61 friend DerivedInterface;
62
63 IRefCountable* doQueryInterface(const IRefCountable::InterfaceId* id)
64 {
65 if (*DerivedInterface::interfaceId() == *id)
66 {
67 this->addRef();
68 // The cast is needed to shift the pointer in case of multiple inheritance.
69 return static_cast<DerivedInterface*>(this);
70 }
71 return BaseInterface::queryInterface(id);
72 }
73 };
74
75 } // namespace sdk
76 } // namespace nx
nx::sdk::Interface::queryInterfaceSupportingDeprecatedId
IRefCountable * queryInterfaceSupportingDeprecatedId(const IRefCountable::InterfaceId *id, const Uuid &deprecatedInterfaceId)
Definition: interface.h:40
nx::sdk::IRefCountable::InterfaceId
Definition: i_ref_countable.h:62
nx::sdk::Interface
Definition: interface.h:28
nx::sdk::Uuid
Definition: uuid.h:21
nx
Definition: apple_utils.h:6
nx::sdk::IRefCountable
Definition: i_ref_countable.h:50