001/*
002 * This file is part of the Tuning Fork Visualization Platform
003 *  (http://sourceforge.net/projects/tuningforkvp)
004 *
005 * Copyright (c) 2005 - 2008 IBM Corporation.
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the Eclipse Public License v1.0
008 * which accompanies this distribution, and is available at
009 * http://www.eclipse.org/legal/epl-v10.html
010 *
011 * Contributors:
012 *     IBM Corporation - initial API and implementation
013 */
014
015package com.ibm.tuningfork.tracegen.chunk;
016
017import org.vmmagic.pragma.Interruptible;
018import org.vmmagic.pragma.Uninterruptible;
019
020import com.ibm.tuningfork.tracegen.types.EventAttribute;
021import com.ibm.tuningfork.tracegen.types.EventType;
022
023@Uninterruptible
024public class EventTypeChunk extends Chunk {
025
026    public static final int EVENT_TYPE_ID = 4;
027    public static final int EVENT_TYPE_OFFSET = Chunk.DATA_OFFSET;
028    public static final int EVENT_DATA_OFFSET = EVENT_TYPE_OFFSET + 4;
029    private int numberOfEventTypes = 0;
030
031    public EventTypeChunk() {
032        super(EVENT_TYPE_ID);
033        seek(EVENT_DATA_OFFSET);
034    }
035
036    public boolean hasData() {
037        return numberOfEventTypes > 0;
038    }
039
040    @Interruptible
041    public boolean add(EventType et) {
042      boolean success = false;
043      int savedPosition = getPosition();
044      try {
045        if (!addInt(et.getIndex())) return false;
046        if (!addStringInternal(getChars(et.getName()))) return false;
047        if (!addStringInternal(getChars(et.getDescription()))) return false;
048        if (!addInt(et.getNumberOfInts())) return false;
049        if (!addInt(et.getNumberOfLongs())) return false;
050        if (!addInt(et.getNumberOfDoubles())) return false;
051        if (!addInt(et.getNumberOfStrings())) return false;
052        for (int i = 0; i < et.getNumberOfAttributes(); i++) {
053          EventAttribute ea = et.getAttribute(i);
054          if (!addStringInternal(getChars(ea.getName()))) return false;
055          if (!addStringInternal(getChars(ea.getDescription()))) return false;
056        }
057        success = true;
058        numberOfEventTypes++;
059        return true;
060      } finally {
061        if (!success) {
062          seek(savedPosition);
063        }
064      }
065    }
066
067    @Override
068    public void close() {
069        putIntAt(EVENT_TYPE_OFFSET, numberOfEventTypes);
070        numberOfEventTypes = 0;
071        super.close();
072    }
073
074    public void reset() {
075        resetImpl();
076        numberOfEventTypes = 0;
077        seek(EVENT_DATA_OFFSET);
078    }
079
080}