1 // =================================
 2 // Copyright (c) 2024 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Collections;
 8 
 9 namespace System.Windows
10 {
11     public abstract class Resource
12     {
13         public virtual default ~Resource();
14     }
15 
16     public class ResourceManager
17     {
18         public Resource* GetResource(const string& resourceName)
19         {
20             auto it = resourceMap.CFind(resourceName);
21             if (it != resourceMap.CEnd())
22             {
23                 return it->second;
24             }
25             return null;
26         }
27         [nodiscard]
28         public Result<Cursor*> GetCursor(const string& cursorName)
29         {
30             Cursor* cursor = null;
31             Resource* result = GetResource(cursorName);
32             if (result != null && result is Cursor*)
33             {
34                 return Result<Cursor*>(cast<Cursor*>(result));
35             }
36             auto cursorResult = LoadCursor(cursorName);
37             if (cursorResult.Error())
38             {
39                 return Result<Cursor*>(ErrorId(cursorResult.GetErrorId()));
40             }
41             UniquePtr<Resource> resource(new Cursor(Rvalue(cursorResult.Value())));
42             cursor = cast<Cursor*>(resource.Get());
43             resources.Add(Rvalue(resource));
44             return Result<Cursor*>(cursor);
45         }
46         [nodiscard]
47         public Result<Icon*> GetIcon(const string& iconName)
48         {
49             Icon* icon = null;
50             Resource* result = GetResource(iconName);
51             if (result != null && result is Icon*)
52             {
53                 return Result<Icon*>(cast<Icon*>(result));
54             }
55             auto iconResult = LoadIcon(iconName);
56             if (iconResult.Error())
57             {
58                 return Result<Icon*>(ErrorId(iconResult.GetErrorId()));
59             }
60             UniquePtr<Resource> resource(new Icon(Rvalue(iconResult.Value())));
61             icon = cast<Icon*>(resource.Get());
62             resources.Add(Rvalue(resource));
63             return Result<Icon*>(icon);
64         }
65         [nodiscard]
66         public Result<WinBitmap*> GetBitmap(const string& bitmapName)
67         {
68             WinBitmap* bitmap = null;
69             Resource* result = GetResource(bitmapName);
70             if (result != null && result is WinBitmap*)
71             {
72                 return Result<WinBitmap*>(cast<WinBitmap*>(result));
73             }
74             auto bitmapResult = LoadBitmap(bitmapName);
75             if (bitmapResult.Error())
76             {
77                 return Result<WinBitmap*>(ErrorId(bitmapResult.GetErrorId()));
78             }
79             UniquePtr<Resource> resource(new WinBitmap(Rvalue(bitmapResult.Value())));
80             bitmap = cast<WinBitmap*>(resource.Get());
81             resources.Add(Rvalue(resource));
82             return Result<WinBitmap*>(bitmap);
83         }
84         private HashMap<stringResource*> resourceMap;
85         private List<UniquePtr<Resource>> resources;
86     }