Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

constantpool.cpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #include "constantpool.hpp"
00011 
00012 namespace ClassEncoder
00013 {
00014 using namespace std;
00015 
00017 
00020 ConstantPool::ConstantPool()
00021 {
00022     ConstantPoolCount = 0;
00023     // push a dummy element in front since position 0 is reserved.
00024     // This is to avoid stupid of by one bugs in conjunction with the position references in the pool
00025     Entries.push_back( new Utf8_info( "dummy" ) );
00026 }
00027 
00029 
00032 ConstantPool::~ConstantPool()
00033 {
00034     // delete constant_pool items
00035     vector<cp_info*>::iterator it = Entries.begin();
00036     while( it != Entries.end() )
00037     {
00038         delete (*it);
00039         ++it;
00040     }
00041     Entries.clear();
00042     ConstantPoolCount = 0;
00043 }
00044 
00046 
00050 unsigned int ConstantPool::add( cp_info *entry )
00051 {
00052     Entries.push_back( entry );
00053     return ++ConstantPoolCount;
00054 }
00055 
00057 
00060 void ConstantPool::write( ofstream &stream ) const
00061 {
00062     vector<cp_info*>::const_iterator it = Entries.begin();
00063     ++it; // skip dummy
00064     while( it != Entries.end() )
00065     {
00066         (*it)->write( stream );
00067         ++it;
00068     }
00069 }
00070 
00072 
00075 void cp_info::write( ofstream &stream ) const
00076 {
00077     writeu1( stream, tag );
00078 }
00079 
00081 
00084 void Class_info::write( ofstream &stream ) const
00085 {
00086     cp_info::write( stream );
00087     writeu2( stream, name_index );
00088 }
00089 
00091 
00094 void Methodref_info::write( ofstream &stream ) const
00095 {
00096     cp_info::write( stream );
00097     writeu2( stream, class_index );
00098     writeu2( stream, name_and_type_index );
00099 }
00100 
00102 
00105 void InterfaceMethodref_info::write( ofstream &stream ) const
00106 {
00107     cp_info::write( stream );
00108     writeu2( stream, class_index );
00109     writeu2( stream, name_and_type_index );
00110 }
00111 
00113 
00116 void Fieldref_info::write( ofstream &stream ) const
00117 {
00118     cp_info::write( stream );
00119     writeu2( stream, class_index );
00120     writeu2( stream, name_and_type_index );
00121 }
00122 
00124 
00127 void NameAndType_info::write( ofstream &stream ) const
00128 {
00129     cp_info::write( stream );
00130     writeu2( stream, name_index );
00131     writeu2( stream, descriptor_index );
00132 }
00133 
00135 
00138 void String_info::write( ofstream &stream ) const
00139 {
00140     cp_info::write( stream );
00141     writeu2( stream, string_index );
00142 }
00143 
00145 
00148 void Integer_info::write( ofstream &stream ) const
00149 {
00150     cp_info::write( stream );
00151     writeu4( stream, bytes );
00152 }
00153 
00155 
00158 void Float_info::write( ofstream &stream ) const
00159 {
00160     cp_info::write( stream );
00161     writeu4( stream, bytes );
00162 }
00163 
00165 
00168 void Long_info::write( ofstream &stream ) const
00169 {
00170     cp_info::write( stream );
00171     writeu4( stream, high_bytes );
00172     writeu4( stream, low_bytes );
00173 }
00174 
00176 
00179 void Double_info::write( ofstream &stream ) const
00180 {
00181     cp_info::write( stream );
00182     writeu4( stream, high_bytes );
00183     writeu4( stream, low_bytes );
00184 }
00185 
00187 
00191 void Utf8_info::write( ofstream &stream ) const
00192 {
00193     cp_info::write( stream );
00194     writeu2( stream, length );
00195     /* TODO: UTF8 encoding */
00196     writeu1Array( stream, bytes.c_str(), length );
00197 }
00198 
00199 } // end namespace

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3