1 /*************************************************************************
2  *
3  *
4  *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "CEGUI/WindowRendererManager.h"
28 #include "CEGUI/Exceptions.h"
29 #include <algorithm>
30 
31 // Start CEGUI namespace
32 namespace CEGUI
33 {
34 
35 /*************************************************************************
36     Static data
37 *************************************************************************/
38 template<> WindowRendererManager* Singleton<WindowRendererManager>::ms_Singleton = 0;
39 WindowRendererManager::OwnedFactoryList WindowRendererManager::d_ownedFactories;
40 
41 /*************************************************************************
42     Singleton functions
43 *************************************************************************/
getSingleton(void)44 WindowRendererManager& WindowRendererManager::getSingleton(void)
45 {
46     return Singleton<WindowRendererManager>::getSingleton();
47 }
getSingletonPtr(void)48 WindowRendererManager* WindowRendererManager::getSingletonPtr(void)
49 {
50     return Singleton<WindowRendererManager>::getSingletonPtr();
51 }
52 
53 /*************************************************************************
54     Constructor / Destructor
55 *************************************************************************/
WindowRendererManager()56 WindowRendererManager::WindowRendererManager()
57 {
58     char addr_buff[32];
59     sprintf(addr_buff, "(%p)", static_cast<void*>(this));
60     Logger::getSingleton().logEvent(
61         "CEGUI::WindowRendererManager singleton created " + String(addr_buff));
62 
63     // complete addition of any pre-added WindowRendererFactory objects
64     OwnedFactoryList::iterator i = d_ownedFactories.begin();
65 
66     if (d_ownedFactories.end() != i)
67     {
68         Logger::getSingleton().logEvent(
69         "---- Adding pre-registered WindowRendererFactory objects ----");
70 
71         for (; d_ownedFactories.end() != i; ++i)
72             addFactory(*i);
73     }
74 }
75 
~WindowRendererManager()76 WindowRendererManager::~WindowRendererManager()
77 {
78     char addr_buff[32];
79     sprintf(addr_buff, "(%p)", static_cast<void*>(this));
80     Logger::getSingleton().logEvent(
81         "CEGUI::WindowRendererManager singleton destroyed " + String(addr_buff));
82 }
83 
84 /*************************************************************************
85     Is there a WindowRenderer by this name?
86 *************************************************************************/
isFactoryPresent(const String & name) const87 bool WindowRendererManager::isFactoryPresent(const String& name) const
88 {
89     return (d_wrReg.find(name) != d_wrReg.end());
90 }
91 
92 /*************************************************************************
93     Get the named WindowRenderer
94 *************************************************************************/
getFactory(const String & name) const95 WindowRendererFactory* WindowRendererManager::getFactory(const String& name) const
96 {
97     WR_Registry::const_iterator i = d_wrReg.find(name);
98     if (i != d_wrReg.end())
99     {
100         return (*i).second;
101     }
102     CEGUI_THROW(UnknownObjectException("There is no WindowRendererFactory named '"+name+"' available"));
103 }
104 
105 /*************************************************************************
106     Add a new WindowRenderer factory
107 *************************************************************************/
addFactory(WindowRendererFactory * wr)108 void WindowRendererManager::addFactory(WindowRendererFactory* wr)
109 {
110     if (wr == 0)
111     {
112         return;
113     }
114     if (d_wrReg.insert(std::make_pair(wr->getName(), wr)).second == false)
115     {
116         CEGUI_THROW(AlreadyExistsException("A WindowRendererFactory named '"+wr->getName()+"' already exist"));
117     }
118 
119     char addr_buff[32];
120     sprintf(addr_buff, "(%p)", static_cast<void*>(wr));
121     Logger::getSingleton().logEvent("WindowRendererFactory '"+wr->getName()+
122         "' added. " + addr_buff);
123 }
124 
125 /*************************************************************************
126     Remove a factory by name
127 *************************************************************************/
removeFactory(const String & name)128 void WindowRendererManager::removeFactory(const String& name)
129 {
130     WR_Registry::iterator i = d_wrReg.find(name);
131 
132 	// non-existing or already removed? The latter can happen when more then one Scheme
133 	// was loaded using the same renderer.
134 	if (i == d_wrReg.end())
135 	{
136 		return;
137 	}
138 
139     // see if we own this factory
140     OwnedFactoryList::iterator j = std::find(d_ownedFactories.begin(),
141                                              d_ownedFactories.end(),
142                                              (*i).second);
143 
144     char addr_buff[32];
145     sprintf(addr_buff, "(%p)", static_cast<void*>((*i).second));
146 
147     d_wrReg.erase(name);
148 
149     Logger::getSingleton().logEvent("WindowRendererFactory for '" + name +
150                                     "' WindowRenderers removed. " + addr_buff);
151 
152     // delete factory object if we created it
153     if (j != d_ownedFactories.end())
154     {
155         Logger::getSingleton().logEvent("Deleted WindowRendererFactory for '" +
156                                         (*j)->getName() +
157                                         "' WindowRenderers.");
158 
159         CEGUI_DELETE_AO (*j);
160         d_ownedFactories.erase(j);
161     }
162 }
163 
164 /*************************************************************************
165     Create a WindowRenderer instance by factory name
166 *************************************************************************/
createWindowRenderer(const String & name)167 WindowRenderer* WindowRendererManager::createWindowRenderer(const String& name)
168 {
169     WindowRendererFactory* factory = getFactory(name);
170     return factory->create();
171 }
172 
173 /*************************************************************************
174     Destroy a WindowRenderer using its factory
175 *************************************************************************/
destroyWindowRenderer(WindowRenderer * wr)176 void WindowRendererManager::destroyWindowRenderer(WindowRenderer* wr)
177 {
178     WindowRendererFactory* factory = getFactory(wr->getName());
179     factory->destroy(wr);
180 }
181 
182 } // CEGUI
183