Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Environment.h 1.73 KiB
#ifndef _ENVIRONMENT_H_
#define _ENVIRONMENT_H_

#include <alib/string>
#include <alib/map>
#include <alib/memory>

#include <abstraction/OperationAbstraction.hpp>
#include <exception/CommonException.h>

namespace cli {

class Environment {
	ext::map < std::string, std::string > m_bindings;
	ext::map < std::string, std::shared_ptr < abstraction::OperationAbstraction > > m_variables;

	std::unique_ptr < Environment > m_upper;
public:
	Environment ( ) {
	}

	Environment ( std::unique_ptr < Environment > upper ) : m_upper ( std::move ( upper ) ) {
	}

	std::string getBinding ( const std::string & name ) const {
		auto res = m_bindings.find ( name );

		if ( res != m_bindings.end ( ) )
			return res->second;

		if ( ( bool ) m_upper )
			return m_upper->getBinding ( name );
		else
			throw exception::CommonException ( "Binded value of name " + name + " not found." );
	}

	void setBinding ( std::string name, std::string value ) {
		m_bindings [ std::move ( name ) ] = std::move ( value );
	}

	bool clearBinding ( const std::string & name ) {
		return m_bindings.erase ( name );
	}

	std::shared_ptr < abstraction::OperationAbstraction > getVariable ( const std::string & name ) {
		auto res = m_variables.find ( name );

		if ( res != m_variables.end ( ) )
			return res->second;

		if ( ( bool ) m_upper )
			return m_upper->getVariable ( name );
		else
			throw exception::CommonException ( "Variable of name " + name + " not found." );
	}

	void setVariable ( std::string name, std::shared_ptr < abstraction::OperationAbstraction > value ) {
		m_variables [ std::move ( name ) ] = std::move ( value );
	}

	bool clearVariable ( const std::string & name ) {
		return m_variables.erase ( name );
	}

};

} /* namespace cli */

#endif /* _ENVIRONMENT_H_ */