/*======================================================================== Copyright (C) 2006 by Geir-Tore Lindsve, Torbjørn Meistad and Yngve Raudberget, hereby refered to as "the authors". All rights reserved Permission is hereby granted, without written agreement and without license or royalty fees, to use, reproduce, prepare derivative works, distribute, and display this software and its documentation for NONCOMMERCIAL RESEARCH AND EDUCATIONAL PURPOSES, provided that (1) the above copyright notice and the following two paragraphs appear in all copies of the source code and (2) redistributions, including without limitation binaries, reproduce these notices in the supporting documentation. IN NO EVENT SHALL THE AUTHORS, OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ========================================================================*/ using System; using System.Collections.Generic; using System.Text; using System.Collections; using CLab.Exceptions; namespace CLab.CSP { /// /// A class for representing enumeration types in the CSP part of CLab. /// The different possible domain values for an enumeration are encoded as /// a range with an end point. The start point is assumed always to be 0. /// Implements the abstract CSPType class. /// public class CSPTypeEnum : CSPType { private Hashtable constantToID; private List idToConstant; private int lastID = -1; private StringComparer stringComparer; /// /// Initializes a new instance of the class. /// /// Name of the type. public CSPTypeEnum(String typeName) : base(typeName, -1) { stringComparer = StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true); constantToID = new Hashtable(stringComparer); idToConstant = new List(); } /// /// Adds a new enumeration domain value (constant). /// This method makes a mapping from string representation /// to int representation and vice versa. /// /// The constant. /// public int AddConstant(String constant) { lastID++; constantToID[constant] = lastID; idToConstant.Add(constant); base.End = lastID; return lastID; } /// /// Returns the constant. /// /// The constant. public override CPTypes TypeFormat() { return CPTypes.enum_type; } /// /// Gets the string representation of an int within the range of a type. /// Uses the internal mapping from int to string. /// /// An integer representing an enumeration domain value (constant). /// /// The string representation of the domain value. /// /// public override String GetStringRepOfDomainInt(int domainInt) { if (domainInt > -1 && domainInt < idToConstant.Count) { return idToConstant[domainInt]; } throw new ClabException("Invalid domain int"); } /// /// Gets the int representation of the string representation of a domain value. /// Uses the internal mapping from string to int. /// /// The string representation of a domain value. /// /// The int representation of the domain value. /// /// public override int GetIntRepOfDomainString(String domainString) { try { return (int)constantToID[domainString]; } catch (Exception e) { throw new ClabException("Invalid domain string", e); } } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { String retur = "CSPTypeEnumerator - name: " +base.ToString() + "\n"; IDictionaryEnumerator enumer = constantToID.GetEnumerator(); while(enumer.MoveNext()){ retur += enumer.Key + ": " + enumer.Value + " - "; } return retur; } } }