// $Id$ // // Copyright 2007-2008 Cisco Systems Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. using System; using System.Collections.Generic; using Etch.Msg; namespace Etch.Support { /// /// Validator for custom type /// public class Validator_custom : TypeValidator { /// /// Class of the custom type /// number of dimensions. 0 for scalar. /// an instance of the validator public static Validator Get( Type clss, int nDims, bool subclassOk ) { CheckDims( nDims ); if ( nDims > MAX_CACHED ) return new Validator_custom( clss, nDims, subclassOk ); Key key = new Key(clss, nDims, subclassOk); Validator v = null; try { v = validators[key]; } catch(Exception) { v = null; } if (v == null) { v = new Validator_custom(clss, nDims, subclassOk); validators.Add(key, v); } return v; } private class Key { public Key( Type clss, int dims, bool subclassOk ) { this.clss = clss; this.dims = dims; this.subclassOk = subclassOk; } private Type clss; private int dims; private bool subclassOk; public override int GetHashCode() { return clss.GetHashCode() ^ (dims * 9131) ^ (subclassOk ? 21357 : 8547); } public override bool Equals( Object obj ) { if (obj == this) return true; if (obj == null) return false; if (obj.GetType() != GetType()) return false; Key other = (Key) obj; return other.clss.Equals( clss) && other.dims == dims && other.subclassOk == subclassOk; } } private static Dictionary validators = new Dictionary(); /// /// Constructs the validator /// /// the class of the custom type /// the number of dimensions. 0 for scalar. private Validator_custom( Type clss, int nDims, bool subclassOk ) : base(clss, clss, nDims, subclassOk ? clss.ToString() + "[" + nDims + "]*" : clss.ToString() + "[" + nDims + "]", subclassOk) { this.clss = clss; } private Type clss; public override Validator ElementValidator() { return Get( clss, nDims - 1, subclassOk ); } } }