DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
IServiceProvider.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "Common.h"
9#include "System/Object.h"
10#include "System/SmartPointer.h"
12#include <typeindex>
13
14namespace DotNetDupe {
15 namespace System {
20 class IServiceProvider : public Object {
21 public:
23 virtual ~IServiceProvider() = default;
24
28 virtual SmartPointer<Object> GetService(const std::type_index& serviceType) = 0;
29
33 template <typename T>
35 SmartPointer<Object> spObj = GetService(typeid(T));
36 if (spObj.IsNull()) {
37 return SmartPointer<T>(nullptr);
38 }
39 return spObj.template DynamicCast<T>();
40 }
41
46 template <typename T>
48 SmartPointer<T> spService = GetService<T>();
49 if (spService.IsNull()) {
50 throw InvalidOperationException("Required service not registered.");
51 }
52 return spService;
53 }
54 };
55 }
56}
Defines common cross-platform macros, export decorators, and fundamental types.
Defines the exception thrown when a method call is invalid for the object's current state.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
Defines a mechanism for retrieving a service object; that is, an object that provides custom support ...
SmartPointer< T > GetRequiredService()
Gets service of type T, throwing InvalidOperationException if not found.
SmartPointer< T > GetService()
Gets the service object of the specified generic type T.
virtual SmartPointer< Object > GetService(const std::type_index &serviceType)=0
Gets the service object of the specified type.
virtual ~IServiceProvider()=default
Virtual destructor for polymorphic interface cleanup.
InvalidOperationException(const String &sMessage)
Initializes a new instance of the InvalidOperationException class with a specified error message.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
A unified smart pointer that supports both unique and shared ownership semantics.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
SmartPointer< To > DynamicCast(const SmartPointer< From > &sp)