PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/topology/physicalDataCenterFactory.cpp

https://github.com/deltaforge/nebu-common-cpp
C++ | 55 lines | 42 code | 11 blank | 2 comment | 2 complexity | 204c48c88ac933c8d1fe2492dd394069 MD5 | raw file
 1
 2#include "nebu/topology/physicalDataCenterFactory.h"
 3#include "nebu/topology/physicalRackFactory.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<PhysicalDataCenterFactory> PhysicalDataCenterFactory::instance;
19
20		const string PhysicalDataCenterFactory::ROOT_NAME = "dataCenter";
21		const string PhysicalDataCenterFactory::ATTR_ID = "id";
22
23		shared_ptr<PhysicalDataCenterFactory> PhysicalDataCenterFactory::getInstance()
24		{
25			if (!PhysicalDataCenterFactory::instance) {
26				PhysicalDataCenterFactory::setInstance(make_shared<PhysicalDataCenterFactory>());
27			}
28			return PhysicalDataCenterFactory::instance;
29		}
30		void PhysicalDataCenterFactory::setInstance(std::shared_ptr<PhysicalDataCenterFactory> instance)
31		{
32			PhysicalDataCenterFactory::instance = instance;
33		}
34
35		Traits<PhysicalDataCenter>::Ptr PhysicalDataCenterFactory::parseXML(const tinyxml2::XMLElement *elem) const
36		{
37			XMLUtil::verifyElementName(elem, PhysicalDataCenterFactory::ROOT_NAME);
38
39			const XMLAttribute *idAttr = XMLUtil::findAttribute(elem, PhysicalDataCenterFactory::ATTR_ID, XMLUtil::MANDATORY);
40			Traits<PhysicalDataCenter>::Ptr dataCenter = Traits<PhysicalDataCenter>::MakePtr(idAttr->Value());
41
42			const XMLElement *rackElem = XMLUtil::findChildElement(elem, PhysicalRackFactory::ROOT_NAME);
43			while (rackElem) {
44				Traits<PhysicalRack>::Ptr rack = PhysicalRackFactory::getInstance()->parseXML(rackElem);
45				dataCenter->addRack(rack);
46				rack->setParent(dataCenter.get());
47
48				rackElem = XMLUtil::findNextSibling(rackElem, PhysicalRackFactory::ROOT_NAME);
49			}
50
51			return dataCenter;
52		}
53
54	}
55}