Bytecode.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       Bytecode.h
00014 
00015    Primitive bytecode I/O functions.
00016    Acts as a namespace wrapper.
00017 
00018    Author:     Klaus Kaempf <kkaempf@suse.de>
00019    Maintainer: Klaus Kaempf <kkaempf@suse.de>
00020 
00021 /-*/
00022 // -*- c++ -*-
00023 
00024 #ifndef Bytecode_h
00025 #define Bytecode_h
00026 
00027 #include "ycp/YCPValue.h"
00028 #include "ycp/YCode.h"
00029 #include "ycp/YStatement.h"
00030 #include "ycp/YBlock.h"
00031 #include "ycp/Type.h"
00032 
00033 class Y2Namespace;
00034 
00035 #include <iosfwd>
00036 #include <string>
00037 #include <map>
00038 
00039 #include <fstream>
00040 
00042 class bytecodeistream : public std::ifstream
00043 {
00044         int m_major, m_minor, m_release;
00045     public:
00046         bytecodeistream (string filename);
00047         bool isVersion (int major, int minor, int revision);
00048         bool isVersionAtMost (int major, int minor, int revision);
00049         
00050         int major () const { return m_major; }
00051         int minor () const { return m_minor; }
00052         int release () const { return m_release; }
00053 };
00054 
00056 class Bytecode {
00057     static int m_namespace_nesting_level;
00058     static int m_namespace_nesting_array_size;
00059     static int m_namespace_tare_level;
00060     
00062     struct namespaceentry_t {
00063         const Y2Namespace *name_space;
00064         bool with_xrefs;        
00065     };
00066     static namespaceentry_t *m_namespace_nesting_array;
00067     static map<string, YBlockPtr>* m_bytecodeCache;
00068 
00069     public:
00076     class Invalid {};
00077 
00078         // bool I/O
00079         static std::ostream & writeBool (std::ostream & streamref, bool value);
00080         static bool readBool (bytecodeistream & streamref);
00081 
00082         // string I/O
00083         static std::ostream & writeString (std::ostream & streamref, const std::string & stringref);
00084         static bool readString (bytecodeistream & streamref, std::string & stringref);
00085 
00086         // Ustring I/O
00087         static std::ostream & writeUstring (std::ostream & streamref, const Ustring ustringref);
00088         static Ustring readUstring (bytecodeistream & streamref);
00089 
00090         // char * I/O
00091         static std::ostream & writeCharp (std::ostream & streamref, const char * charp);
00092         static char * readCharp (bytecodeistream & streamref);
00093 
00094         // bytepointer I/O
00095         static std::ostream & writeBytep (std::ostream & streamref, const unsigned char * bytep, unsigned int len);
00096         static unsigned char * readBytep (bytecodeistream & streamref);
00097 
00098         // u_int32_t I/O
00099         static std::ostream & writeInt32 (std::ostream & str, const u_int32_t value);
00100         static u_int32_t readInt32 (bytecodeistream & str);
00101 
00102         // Type I/O
00103         static std::ostream & writeType (std::ostream & str, constTypePtr type);
00104         static TypePtr readType (bytecodeistream & str);
00105 
00106         // YCPValue I/O
00107         static std::ostream & writeValue (std::ostream & str, const YCPValue value);
00108         static YCPValue readValue (bytecodeistream & str);
00109 
00110         // ycodelist_t * I/O
00111         static std::ostream & writeYCodelist (std::ostream & str, const ycodelist_t *codelist);
00112         static bool readYCodelist (bytecodeistream & str, ycodelist_t **anchor);
00113 
00114         //-----------------------------------------------------------
00115         // block nesting handling
00116         //
00117         static void namespaceInit ();
00118         // retrieve ID (nesting level) for namespace
00119         static int namespaceId (const Y2Namespace *name_space);
00120         // retrieve namespace for ID
00121         static const Y2Namespace *namespacePtr (int namespace_id);
00122 
00123         // push given namespace to id stack, return new id, -1 on error
00124         static int pushNamespace (const Y2Namespace *name_space, bool with_xrefs = false);
00125 
00126         // pop given namespace from id stack, return namespace id, -1 on error
00127         static int popNamespace (const Y2Namespace *name_space);
00128 
00129         // pop all from id stack until given namespace is reached and popped too
00130         static void popUptoNamespace (const Y2Namespace *name_space);
00131 
00132         // reset current namespace stack to 'empty' for module loading
00133         //   returns a tare id needed later
00134         static int tareStack ();
00135         static void untareStack (int tare_id);
00136 
00137         //-----------------------------------------------------------
00138         // SymbolEntry pointer (!) handling
00139         //   the SymbolEntries itself are 'owned' by Y2Namespace (YBlock in YCP) and referenced via pointers
00140         //   to SymbolEntry. These functions handle stream I/O for SymbolEntry pointers.
00141         static std::ostream &writeEntry (std::ostream & str, const SymbolEntryPtr entry);
00142         static SymbolEntryPtr readEntry (bytecodeistream & str);
00143 
00144         //-----------------------------------------------------------
00145         // YCode read.
00146         // Must be implemented outside of YCode since we have derived classes to allocate...
00147         // see YCode for write
00148         static YCodePtr readCode (bytecodeistream & str);
00149 
00150         // File I/O
00151         // reading and writing complete files is done via separate functions
00152         // which add/check a 'magic value' header denoting "YCode" and its version.
00153 
00154         // read YCode from file in Module path, return YBlock (NULL in case of error)
00155         static YBlockPtr readModule (const string & mname);
00156 
00157         // read YCode from file, return YCode (YError in case of failure)
00158         static YCodePtr readFile (const string & filename);
00159 
00160         // write YCode to file, return true on success (check errno for errors)
00161         static bool writeFile (const YCodePtr code, const string & filename);
00162 };
00163 
00164 #endif // Bytecode_h

Generated on Tue Nov 6 01:27:46 2007 for yast2-core by  doxygen 1.5.3