TclListSerialize.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "debug/DebugUtils.h"
00019 #include "TclListSerialize.h"
00020 
00021 namespace oasys {
00022 
00023 TclListSerialize::TclListSerialize(Tcl_Interp* interp,
00024                                    Tcl_Obj*    list_obj,
00025                                    context_t   context,
00026                                    int         options)
00027     : SerializeAction(Serialize::MARSHAL, context, options),
00028       interp_(interp), list_obj_(list_obj)
00029 {
00030 }
00031 
00032 TclListSerialize::~TclListSerialize()
00033 {
00034 }
00035 
00036 void
00037 TclListSerialize::process(const char* name, u_int64_t* i)
00038 {
00039     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00040     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewWideIntObj(*i));
00041 }
00042 
00043 void
00044 TclListSerialize::process(const char* name, u_int32_t* i)
00045 {
00046     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00047     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewIntObj(*i));
00048 }
00049 
00050 void
00051 TclListSerialize::process(const char* name, u_int16_t* i)
00052 {
00053     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00054     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewIntObj(*i));
00055 }
00056 
00057 void
00058 TclListSerialize::process(const char* name, u_int8_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, bool* b)
00066 {
00067     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00068     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewBooleanObj(*b));
00069 }
00070 
00071 void
00072 TclListSerialize::process(const char* name, u_char* bp, u_int32_t len)
00073 {
00074     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00075     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewByteArrayObj(bp, len));
00076 }
00077 
00078 void
00079 TclListSerialize::process(const char* name, std::string* s)
00080 {
00081     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00082     Tcl_ListObjAppendElement(interp_, list_obj_,
00083                              Tcl_NewStringObj(s->data(), s->length()));
00084 }
00085 
00086 void
00087 TclListSerialize::process(const char* name, u_char** bp,
00088                           u_int32_t* lenp, int flags)
00089 {
00090     if (flags & Serialize::NULL_TERMINATED) {
00091         Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00092         Tcl_ListObjAppendElement(interp_, list_obj_,
00093                                  Tcl_NewStringObj((char*)*bp, -1));
00094     } else {
00095         Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00096         Tcl_ListObjAppendElement(interp_, list_obj_,
00097                                  Tcl_NewStringObj((char*)*bp, *lenp));
00098     }
00099 }
00100 
00101 void
00102 TclListSerialize::process(const char* name, SerializableObject* object)
00103 {
00104     Tcl_Obj* old_list_obj = list_obj_;
00105     Tcl_Obj* new_list_obj = Tcl_NewListObj(0, NULL);
00106 
00107     list_obj_ = new_list_obj;
00108     object->serialize(this);
00109     list_obj_ = old_list_obj;
00110     
00111     Tcl_ListObjAppendElement(interp_, list_obj_, Tcl_NewStringObj(name, -1));
00112 
00113     int length = 0;
00114     int ok = Tcl_ListObjLength(interp_, new_list_obj, &length);
00115     ASSERT(ok == TCL_OK);
00116     
00117     if ((length != 2) || (options_ & KEEP_SINGLETON_SUBLISTS)) {
00118         Tcl_ListObjAppendElement(interp_, list_obj_, new_list_obj);
00119     } else {
00120         Tcl_Obj* obj;
00121         int ok = Tcl_ListObjIndex(interp_, new_list_obj, 1, &obj);
00122         ASSERT(ok == TCL_OK);
00123         
00124         Tcl_ListObjAppendElement(interp_, list_obj_, obj);
00125         Tcl_IncrRefCount(new_list_obj);
00126         Tcl_DecrRefCount(new_list_obj); // free it
00127     }
00128 }
00129 
00130 
00131 } // namespace oasys

Generated on Thu Jun 7 12:54:29 2007 for DTN Reference Implementation by  doxygen 1.5.1