TclListSerialize.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2006 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "debug/DebugUtils.h"
00040 #include "TclListSerialize.h"
00041 
00042 namespace oasys {
00043 
00044 TclListSerialize::TclListSerialize(Tcl_Interp* interp,
00045                                    Tcl_Obj*    list_obj,
00046                                    context_t   context,
00047                                    int         options)
00048     : SerializeAction(Serialize::MARSHAL, context, options),
00049       interp_(interp), list_obj_(list_obj)
00050 {
00051 }
00052 
00053 TclListSerialize::~TclListSerialize()
00054 {
00055 }
00056 
00057 void
00058 TclListSerialize::process(const char* name, u_int32_t* i)
00059 {
00060     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00061     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewIntObj(*i));
00062 }
00063 
00064 void
00065 TclListSerialize::process(const char* name, u_int16_t* i)
00066 {
00067     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00068     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewIntObj(*i));
00069 }
00070 
00071 void
00072 TclListSerialize::process(const char* name, u_int8_t* i)
00073 {
00074     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00075     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewIntObj(*i));
00076 }
00077 
00078 void
00079 TclListSerialize::process(const char* name, bool* b)
00080 {
00081     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00082     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewBooleanObj(*b));
00083 }
00084 
00085 void
00086 TclListSerialize::process(const char* name, u_char* bp, size_t len)
00087 {
00088     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00089     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewByteArrayObj(bp, len));
00090 }
00091 
00092 void
00093 TclListSerialize::process(const char* name, std::string* s)
00094 {
00095     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00096     Tcl_ListObjAppendElement(interp_, list_obj_,
00097                              Tcl_NewStringObj(s->data(), s->length()));
00098 }
00099 
00100 void
00101 TclListSerialize::process(const char* name, u_char** bp,
00102                           size_t* lenp, int flags)
00103 {
00104     if (flags & Serialize::NULL_TERMINATED) {
00105         Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00106         Tcl_ListObjAppendElement(interp_, list_obj_,
00107                                  Tcl_NewStringObj((char*)*bp, -1));
00108     } else {
00109         Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00110         Tcl_ListObjAppendElement(interp_, list_obj_,
00111                                  Tcl_NewStringObj((char*)*bp, *lenp));
00112     }
00113 }
00114 
00115 void
00116 TclListSerialize::process(const char* name, SerializableObject* object)
00117 {
00118     Tcl_Obj* old_list_obj = list_obj_;
00119     Tcl_Obj* new_list_obj = Tcl_NewListObj(0, NULL);
00120 
00121     list_obj_ = new_list_obj;
00122     object->serialize(this);
00123     list_obj_ = old_list_obj;
00124     
00125     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00126 
00127     int length = 0;
00128     int ok = Tcl_ListObjLength(interp_, new_list_obj, &length);
00129     ASSERT(ok == TCL_OK);
00130     
00131     if ((length != 2) || (options_ & KEEP_SINGLETON_SUBLISTS)) {
00132         Tcl_ListObjAppendElement(interp_, list_obj_, new_list_obj);
00133     } else {
00134         Tcl_Obj* obj;
00135         int ok = Tcl_ListObjIndex(interp_, new_list_obj, 1, &obj);
00136         ASSERT(ok == TCL_OK);
00137         
00138         Tcl_ListObjAppendElement(interp_, list_obj_, obj);
00139         Tcl_IncrRefCount(new_list_obj);
00140         Tcl_DecrRefCount(new_list_obj); // free it
00141     }
00142 }
00143 
00144 
00145 } // namespace oasys

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1