// $Id$ // // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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; namespace Org.Apache.Etch.Bindings.Csharp.Msg { /// /// Type denotes the type of a struct or message. When used with a /// message it typically denotes an action or event. /// public class XType : IdName { /// Constructs the Type. /// id of the type. /// name of the type. public XType( int id, String name ) : base( id, name ) { // nothing else. } /// Constructs the Type, computing the appropriate value for the id. /// name the name of the type. public XType( String name ) : base( name ) { // nothing else. } public Validator GetValidator( Field key ) { Validator v; return validators.TryGetValue(key, out v) ? v : null; } private readonly FieldMap fieldMap = new FieldMap(); /// /// Adds the validator to the chain for this key. /// /// /// public void PutValidator( Field key, Validator vldtr ) { CheckNotLocked(); if ( vldtr == null ) return; if ( fieldMap.Get( key.Id ) == null ) AddField( key ); Validator v; if (validators.TryGetValue(key, out v)) validators[key] = new ComboValidator(v, vldtr); else validators[key] = vldtr; } /// /// Removes the validator chain for this key /// /// public void ClearValidator( Field key ) { CheckNotLocked(); validators.Remove( key ); } public Dictionary validators = new Dictionary(); /// /// /// /// the result type of this message type public XType GetResult() { return rType; } public void SetResult( XType rType ) { CheckNotLocked(); this.rType = rType; } private XType rType; /// /// /// /// the associated import / export helper public ImportExportHelper GetImportExportHelper() { return helper; } /// /// Sets the associated import / export helper. /// /// public void SetImportExportHelper( ImportExportHelper helper ) { CheckNotLocked(); this.helper = helper; } private ImportExportHelper helper; /// /// /// /// the associated component type for an array of this type public Type GetComponentType() { return componentType; } /// /// Sets the associated component type for an array of this type. /// This type is not used when de-serializing the array components, /// just when allocating the array itself. /// /// public void SetComponentType(Type type) { CheckNotLocked(); this.componentType = type; } private Type componentType; /// /// /// /// an object to help dispatch the received message public Object GetStubHelper() { return stubHelper; } /// /// Sets an object to help the stub dispatch the received message /// /// public void SetStubHelper( Object stubHelper ) { // CheckNotLocked(); called from stub and not from value factory. if (this.stubHelper != null) throw new Exception("this.stubHelper != null"); this.stubHelper = stubHelper; } private Object stubHelper; ///Locks the fields for this type. public void Lock() { locked = true; fieldMap.Lock(); } private void CheckNotLocked() { if (locked) throw new Exception("locked"); } private bool locked; ///Adds a field to the set of fields. /// A field to all ///Returns the argument. If there is a collision with /// an id and name, both associated with the same field, /// then that field is returned instead of the argument /// hrows IllegalArgumentException if there is a /// collision in the id or name, or both id and name when not associated /// with the same field. public Field AddField( Field field ) { return fieldMap.Add( field ); } ///Translates a field id into the appropriate Field object. ///field id ///id translated into the appropriate Field. public Field GetField( int id ) { return fieldMap.Get( id ); } ///Translates a field name into the appropriate Field. ///A field name ///name translated into the appropriate Field. public Field GetField( string name ) { return fieldMap.Get( name ); } ///a set of all the fields. public List GetFields() { return fieldMap.Values(); } /// /// Every type ( => message) carries a timeout /// private int timeout; public int Timeout { get { return timeout; } set { CheckNotLocked(); timeout = value; } } /// /// Field containing result value /// private Field responseField; public Field ResponseField { get { return responseField; } set { CheckNotLocked(); responseField = value; } } ///Checks whether this type is assignment compatible with other. This ///means that other is a subclass of this. /// ///true if this type is assignable from other public bool IsAssignableFrom( XType other ) { return other != null && ( this.Equals( other ) || IsAssignableFrom( other.SuperType() ) ); } public void CheckIsAssignableFrom( XType other ) { if ( !IsAssignableFrom( other ) ) throw new ArgumentOutOfRangeException(); } public XType SuperType() { return superType; } ///Sets the super type of this type. If struct A extends B, then ///B is the super type of A. /// public void SetSuperType( XType superType ) { CheckNotLocked(); this.superType = superType; } private XType superType; /// /// Gets AsyncMode for this Type /// /// AsyncMode public AsyncMode GetAsyncMode() { return asyncMode; } /// /// Sets the AysncMode /// /// public void SetAsyncMode(AsyncMode mode) { CheckNotLocked(); asyncMode = mode; } private AsyncMode asyncMode; /// /// Gets the message direction. /// /// the message direction public Direction GetDirection() { return direction; } /// /// Sets the message direction. /// /// public void SetDirection(Direction direction) { CheckNotLocked(); this.direction = direction; } private Direction direction; } }