1 // =================================
 2 // Copyright (c) 2021 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             HashMap<stringResource*>.ConstIterator it = resourceMap.CFind(resourceName);
21             if (it != resourceMap.CEnd())
22             {
23                 return it->second;
24             }
25             return null;
26         }
27         public Cursor& GetCursor(const string& cursorName)
28         {
29             Cursor* cursor = null;
30             try
31             {
32                 Resource* result = GetResource(cursorName);
33                 if (result != null && result is Cursor*)
34                 {
35                     return *cast<Cursor*>(result);
36                 }
37                 UniquePtr<Resource> resource(new Cursor(LoadCursor(cursorName)));
38                 cursor = cast<Cursor*>(resource.Get());
39                 resources.Add(Rvalue(resource));
40                 return *cursor;
41             }
42             catch (const Exception& ex)
43             {
44                 throw Exception("could not load cursor '" + cursorName + "': " + ex.Message());
45             }
46             return *cursor;
47         }
48         public Icon& GetIcon(const string& iconName)
49         {
50             Icon* icon = null;
51             try
52             {
53                 Resource* result = GetResource(iconName);
54                 if (result != null && result is Icon*)
55                 {
56                     return *cast<Icon*>(result);
57                 }
58                 UniquePtr<Resource> resource(new Icon(LoadIcon(iconName)));
59                 icon = cast<Icon*>(resource.Get());
60                 resources.Add(Rvalue(resource));
61                 return *icon;
62             }
63             catch (const Exception& ex)
64             {
65                 throw Exception("could not load icon '" + iconName + "': " + ex.Message());
66             }
67             return *icon;
68         }
69         public WinBitmap& GetBitmap(const string& bitmapName)
70         {
71             WinBitmap* bitmap = null;
72             try
73             {
74                 Resource* result = GetResource(bitmapName);
75                 if (result != null && result is WinBitmap*)
76                 {
77                     return *cast<WinBitmap*>(result);
78                 }
79                 UniquePtr<Resource> resource(new WinBitmap(LoadBitmap(bitmapName)));
80                 bitmap = cast<WinBitmap*>(resource.Get());
81                 resources.Add(Rvalue(resource));
82                 return *bitmap;
83             }
84             catch (const Exception& ex)
85             {
86                 throw Exception("could not load bitmap '" + bitmapName + "': " + ex.Message());
87             }
88             return *bitmap;
89         }
90         private HashMap<stringResource*> resourceMap;
91         private List<UniquePtr<Resource>> resources;
92     }
93 }