/*

cls_2: Curl Like Syntax
Yauheni A. Akhotnikau (C) 2001-2003
eao197@yahoo.com
-------------------------------------------------

Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:

1. This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.

3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

-------------------------------------------------

*/
#if !defined( _CLS_2__SCALAR__ONE_OF_HPP_ )
#define _CLS_2__SCALAR__ONE_OF_HPP_

#include <set>

#include <cls_2/scalar/h/constraint.hpp>

namespace cls_2 {

namespace scalar_constraint {

//
// one_of_t
//

/*
	Value must be one of allowed values
*/
template< class T, class Format = scalar_format_t< T > >
class	one_of_t : public scalar_constraint_t< T > {
	public :
		one_of_t();
		virtual ~one_of_t();

		virtual int
		check(
			parser_context_t & context,
			const T & value );

		void
		add( const T & value );

		void
		remove( const T & value );

	protected :
		typedef std::set< T > set_t;

		set_t	m_allowed_values;
};

template< class T, class Format >
one_of_t< T, Format >::one_of_t() {
}

template< class T, class Format >
one_of_t< T, Format >::~one_of_t() {
}

template< class T, class Format >
int
one_of_t< T, Format >::check(
	parser_context_t & context,
	const T & value ) {

	if( m_allowed_values.find( value ) !=
		m_allowed_values.end() )
		return c_ok;

	Format fmt;
	context.show_error( fmt.format_disallowed_value( value ) );
	context.show_error( context.find_msg(
		c_msg_invalid_value ) );
	return c_semantic_error;
}

template< class T, class Format >
void
one_of_t< T, Format >::add( const T & value ) {
	m_allowed_values.insert( value );
}

template< class T, class Format >
void
one_of_t< T, Format >::remove( const T & value ) {
	m_allowed_values.erase( value );
}

} /* namespace scalar_constraint */

} /* namespace cls_2 */

#endif
