ConfigDictionary.h 3.51 KB
Newer Older
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#ifndef CONFIGDICTIONARY_H
#define CONFIGDICTIONARY_H


#include "TObject.h"
#include "ReturnCodes.h"
#include "TStopwatch.h"
#include "TROOT.h"
#include <string>
#include <map>
#include <sstream>
#include <vector>
#include "TLorentzVector.h"
#include <iostream>
#include "TMath.h"
#include "TString.h"
#include "TTree.h"


#define ARRAY_DELIM '$'

class ConfigDictionary{
public:
	typedef std::map<std::string,std::string>::iterator CDIter;
	ConfigDictionary();
	ConfigDictionary(std::string);
	virtual ~ConfigDictionary(){};//empty virtual destructor

	ClassDef(ConfigDictionary,1);
	std::string ToString();
	void FromString(std::string);

	//These throw errors if couldn't find key:
	//Most of methods below are just using templates to
	//make them usable in Cint.
	std::string GetString(std::string)throw(std::string);
	int	GetInt(std::string)throw(std::string);
	double GetDouble(std::string)throw(std::string);
	bool GetBool(std::string)throw(std::string);

	//These will always set 'something' into map:
	void SetString(std::string,std::string);
	void SetDouble(std::string,double);
	void SetInt(std::string,int);
	void SetBool(std::string,bool);
	
	CDIter Begin(){return configMap.begin();};
	CDIter End(){return configMap.end();};


	//_________________________________________________________________________
	template <class T>
	T Get(std::string key) throw (std::string) {
		//This method will get object of any class
		//that was - presumably - stored before in string format in CD.
		//Object needs to have proper >> operator.
		//If operation fails, returned item should have default value
		//depending on its empty constructor.
		//For strings containing spaces it won't work properly, as it
		//would extract only first space followed item!
		if (configMap.find(key) == configMap.end()) {
			Error(	"ConfigDictionary::Get",
					"Couldn't find the key: %s!",
					key.c_str());
			throw(key);
		}
		std::istringstream inStream(configMap[key]);
		T item;
		inStream >> item;
		return item;
	}


	//_____________________________________________________________________________
	template <class T>
	void Set(std::string key,T value){
		//Sets value to key, converts T to string first with << operator.
		std::stringstream ss;
		ss<<value;
		configMap[key] = ss.str();
	}


	//_________________________________________________________________________
	template <class T>
	int GetArray(std::string key,std::vector<T> & output)throw(std::string){
		//Preconditions: T must have >> operator
		//Effect: searches for key in map and pushes values stored in it into output vector
		//Returns: number of elements pushed into vector
		int retNumber = 0;
		if (configMap.find(key) == configMap.end()){
			Error(	"ConfigDictionary::GetInt",
					"Couldn't find the key: %s!",key.c_str());
			throw(key);
		}
		std::istringstream arrayString(configMap[key]);
		std::string itemString;
		//Dangerous - maybe? - exceptions for ss not handled...
		while (arrayString){
			std::getline(arrayString,itemString,ARRAY_DELIM);
			std::stringstream itemStream(itemString);
			if (itemString.length() > 0){
				T item;
				itemStream>>item;
				output.push_back(item);
				retNumber++;
			}
		}
		return retNumber;
	}

	//_________________________________________________________________________
	template <class T>
	void SetArray(std::string key,std::vector<T> & input){
		std::ostringstream arrayString;
		for (unsigned int i=0;i<input.size();i++){
			arrayString<<input[i]<<ARRAY_DELIM;
		}
		configMap[key] = arrayString.str();
	}



private:
	std::map<std::string,std::string> configMap;
};

#endif