data_store_map.h 1.58 KB
#ifndef DATA_STORE_MAP
#define DATA_STORE_MAP

#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <functional>
#include <regex>

#include "data_store.h"
#include "../multithread/buffer_producer.h"
#include "data_basic.h"

/*
 * Regex dependencies on gcc >= 4.9
 * */

namespace data{
class Producer_map: public multithread::Buffer_producer<data::Data_basic*>{
	protected:
		std::unordered_map<std::string,data::Data_basic*> * const data;
		std::string pattern;
		void work_out(){
			for(auto const &v: (*this->data)){
				std::regex ex(this->pattern);
				if(std::regex_match(v.first,ex)){
					data::Data_basic *tmp = v.second->clone();
					this->push(tmp);
				}
			}
		}
	public:
		Producer_map(std::unordered_map<std::string,data::Data_basic*> * const map,std::string pattern):
			data(map),pattern(pattern){}

		virtual ~Producer_map(){
			this->join();
		}
};

	class Data_Store_Map: public data::Data_Store{
		protected:
			std::unordered_map<std::string,data::Data_basic*> data_map;

			void add_internal(data::Data_basic *data){
				this->data_map[data->getName()] = data;
				this->size ++;
			}

		public:
			Data_Store_Map(){}
			Data_Store_Map(std::string file_name):data::Data_Store(file_name){}
			~Data_Store_Map(){
				//~data::Data_Store::~Data_Store();
				for(auto const &kv: this->data_map){
					delete kv.second;
				}
			}

			multithread::Buffer<data::Data_basic*> *get(std::string pattern){
				Producer_map *tmp = new Producer_map(&(this->data_map),pattern);
				tmp->start();
				return tmp;
			}
	};
}

#endif // DATA_STORE_MAP