PageRenderTime 25ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/topology/physicalHostFactory.cpp

https://github.com/deltaforge/nebu-common-cpp
C++ | 55 lines | 42 code | 11 blank | 2 comment | 2 complexity | bfff1c01d19cf1134500f8f66892d58a MD5 | raw file
 1
 2#include "nebu/topology/physicalHostFactory.h"
 3#include "nebu/topology/physicalLocalStoreFactory.h"
 4#include "nebu/util/xmlUtil.h"
 5
 6// Using declarations - tinyxml2
 7using tinyxml2::XMLAttribute;
 8using tinyxml2::XMLElement;
 9// Using declarations - standard library
10using std::make_shared;
11using std::shared_ptr;
12using std::string;
13
14namespace nebu
15{
16	namespace common
17	{
18		shared_ptr<PhysicalHostFactory> PhysicalHostFactory::instance;
19
20		const string PhysicalHostFactory::ROOT_NAME = "host";
21		const string PhysicalHostFactory::ATTR_ID = "id";
22
23		shared_ptr<PhysicalHostFactory> PhysicalHostFactory::getInstance()
24		{
25			if (!PhysicalHostFactory::instance) {
26				PhysicalHostFactory::setInstance(make_shared<PhysicalHostFactory>());
27			}
28			return PhysicalHostFactory::instance;
29		}
30		void PhysicalHostFactory::setInstance(std::shared_ptr<PhysicalHostFactory> instance)
31		{
32			PhysicalHostFactory::instance = instance;
33		}
34
35		Traits<PhysicalHost>::Ptr PhysicalHostFactory::parseXML(const tinyxml2::XMLElement *elem) const
36		{
37			XMLUtil::verifyElementName(elem, PhysicalHostFactory::ROOT_NAME);
38
39			const XMLAttribute *idAttr = XMLUtil::findAttribute(elem, PhysicalHostFactory::ATTR_ID, XMLUtil::MANDATORY);
40			Traits<PhysicalHost>::Ptr host = Traits<PhysicalHost>::MakePtr(idAttr->Value());
41
42			const XMLElement *localStoreElem = XMLUtil::findChildElement(elem, PhysicalLocalStoreFactory::ROOT_NAME);
43			while (localStoreElem) {
44				Traits<PhysicalLocalStore>::Ptr localStore = PhysicalLocalStoreFactory::getInstance()->parseXML(localStoreElem);
45				host->addLocalStore(localStore);
46				localStore->setParent(host.get());
47
48				localStoreElem = XMLUtil::findNextSibling(localStoreElem, PhysicalLocalStoreFactory::ROOT_NAME);
49			}
50
51			return host;
52		}
53
54	}
55}