File: PresetFactoryManager.cpp

package info (click to toggle)
projectm 2.1.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 32,348 kB
  • ctags: 13,907
  • sloc: cpp: 31,087; ansic: 26,914; sh: 816; makefile: 17
file content (90 lines) | stat: -rw-r--r-- 2,312 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// C++ Implementation: PresetFactoryManager
//
// Description: 
//
//
// Author: Carmelo Piccione <carmelo.piccione@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "PresetFactoryManager.hpp"

#ifndef DISABLE_MILKDROP_PRESETS
#include "MilkdropPresetFactory/MilkdropPresetFactory.hpp"
#endif

#ifndef DISABLE_NATIVE_PRESETS
#include "NativePresetFactory/NativePresetFactory.hpp"
#endif

#include <sstream>
PresetFactoryManager::PresetFactoryManager() : _gx(0), _gy(0), initialized(false) {}

PresetFactoryManager::~PresetFactoryManager() {
	for (std::vector<PresetFactory *>::iterator pos = _factoryList.begin(); 
		pos != _factoryList.end(); ++pos) {
		assert(*pos);
		delete(*pos);
	}

  initialized = false;
}

void PresetFactoryManager::initialize(int gx, int gy) {
	_gx = gx;
	_gy = gy;
	
	if (!initialized) {
	  initialized = true;
	} else {
	  std::cout << "already initialized " << std::endl;
	  return;
	}
	  
	PresetFactory * factory;
	
	#ifndef DISABLE_MILKDROP_PRESETS
	factory = new MilkdropPresetFactory(_gx, _gy);
	registerFactory(factory->supportedExtensions(), factory);		
	#endif
	
	#ifndef DISABLE_NATIVE_PRESETS
	factory = new NativePresetFactory();
	registerFactory(factory->supportedExtensions(), factory);
	#endif
}

// Current behavior if a conflict is occurs is to override the previous request

void PresetFactoryManager::registerFactory(const std::string & extensions, PresetFactory * factory) {
	
	std::stringstream ss(extensions);	
	std::string extension;

	_factoryList.push_back(factory);

	while (ss >> extension) {
		if (_factoryMap.count(extension)) {
			std::cerr << "[PresetFactoryManager] Warning: extension \"" << extension << 
				"\" already has a factory. New factory handler ignored." << std::endl;			
		} else {
			_factoryMap.insert(std::make_pair(extension, factory));			
		}
	}
}

PresetFactory & PresetFactoryManager::factory(const std::string & extension) {

	if (!_factoryMap.count(extension)) {		
		std::ostringstream os;
		os << "No factory associated with \"" << extension << "\"." << std::endl;
		throw PresetFactoryException(os.str());
	}
	return *_factoryMap[extension];
}

bool PresetFactoryManager::extensionHandled(const std::string & extension) const {		
	return _factoryMap.count(extension);
}