fieldpkg.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2000-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Mon Nov 13 09:58:16 2000
00010 // commit: $Id: fieldpkg.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/tcl-io/fieldpkg.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_TCL_IO_FIELDPKG_CC_UTC20050712162004_DEFINED
00035 #define GROOVX_TCL_IO_FIELDPKG_CC_UTC20050712162004_DEFINED
00036 
00037 #include "tcl-io/fieldpkg.h"
00038 
00039 #include "io/fields.h"
00040 
00041 #include "nub/ref.h"
00042 
00043 #include "tcl/list.h"
00044 
00045 #include "tcl-io/objreader.h"
00046 #include "tcl-io/objwriter.h"
00047 
00048 #include "rutz/iter.h"
00049 
00050 #include "rutz/trace.h"
00051 #include "rutz/debug.h"
00052 GVX_DBG_REGISTER
00053 
00054 namespace tcl
00055 {
00056   tcl::obj getField(const Field& field,
00057                     nub::ref<FieldContainer> item)
00058   {
00059     tcl::obj_writer w;
00060     field.writeValueTo(item.get(), w);
00061     return w.get_obj();
00062   }
00063 
00064   void setField(const Field& field, nub::ref<FieldContainer> item,
00065                 const tcl::obj& newValue)
00066   {
00067     tcl::obj_reader r(newValue);
00068     field.readValueFrom(item.get(), r);
00069     item->touch(); // emit a signal saying that the object has changed
00070   }
00071 
00072   struct FieldsLister
00073   {
00074     const FieldMap& itsFields;
00075     tcl::list itsFieldList;
00076     bool isItRecursive;
00077     bool isItInited;
00078 
00079     FieldsLister(const FieldMap& fields, bool recurse) :
00080       itsFields(fields),
00081       itsFieldList(),
00082       isItRecursive(recurse),
00083       isItInited(false)
00084     {}
00085 
00086     ~FieldsLister() throw() {}
00087 
00088     typedef void retn_t;
00089 
00090     void operator()(tcl::call_context& ctx);
00091   };
00092 }
00093 
00094 
00095 void tcl::FieldsLister::operator()(tcl::call_context& ctx)
00096 {
00097 GVX_TRACE("tcl::FieldsLister::operator()");
00098   if (!isItInited)
00099     {
00100       for (const FieldMap* fmap = &itsFields;
00101            fmap != 0;
00102            fmap = isItRecursive ? fmap->parent() : 0)
00103         {
00104           for (FieldMap::Iterator itr(fmap->ioFields()); itr.is_valid(); ++itr)
00105             {
00106               const Field& field = *itr;
00107 
00108               tcl::list sub_list;
00109 
00110               sub_list.append(field.name());           // property name
00111               sub_list.append(field.min());            // min value
00112               sub_list.append(field.max());            // max value
00113               sub_list.append(field.res());            // resolution value
00114 
00115               tcl::list flags;
00116               if (field.startsNewGroup()) flags.append("NEW_GROUP");
00117               if (field.isTransient())    flags.append("TRANSIENT");
00118               if (field.isString())       flags.append("STRING");
00119               if (field.isMultiValued())  flags.append("MULTI");
00120               if (field.isChecked())      flags.append("CHECKED");
00121               if (!field.allowGet())      flags.append("NO_GET");
00122               if (!field.allowSet())      flags.append("NO_SET");
00123               if (field.isPrivate())      flags.append("PRIVATE");
00124               if (field.isBoolean())      flags.append("BOOLEAN");
00125 
00126               sub_list.append(flags);
00127 
00128               itsFieldList.append(sub_list);
00129             }
00130         }
00131 
00132       isItInited = true;
00133     }
00134 
00135   ctx.set_result(itsFieldList);
00136 }
00137 
00139 //
00140 // Free function definitions
00141 //
00143 
00144 void tcl::defField(tcl::pkg* pkg, const Field& field,
00145                    const rutz::file_pos& src_pos)
00146 {
00147 GVX_TRACE("tcl::defField");
00148 
00149   const unsigned int keyarg = 1;
00150 
00151   pkg->def_vec( field.name().c_str(), "objref(s)",
00152                 rutz::bind_first(getField, field), keyarg, src_pos );
00153   pkg->def_vec( field.name().c_str(), "objref(s) new_val(s)",
00154                 rutz::bind_first(setField, field), keyarg, src_pos );
00155 }
00156 
00157 void tcl::defAllFields(tcl::pkg* pkg, const FieldMap& fieldmap,
00158                        const rutz::file_pos& src_pos)
00159 {
00160 GVX_TRACE("tcl::defAllFields");
00161 
00162   for (const FieldMap* fmap = &fieldmap; fmap != 0; fmap = fmap->parent())
00163     {
00164       for (FieldMap::Iterator itr(fmap->ioFields()); itr.is_valid(); ++itr)
00165         {
00166           defField(pkg, *itr, src_pos);
00167         }
00168     }
00169 
00170   pkg->def_raw("fields", tcl::arg_spec(1), "",
00171                FieldsLister(fieldmap, false), src_pos);
00172 
00173   pkg->def_raw("allFields", tcl::arg_spec(1), "",
00174                FieldsLister(fieldmap, true), src_pos);
00175 }
00176 
00177 static const char __attribute__((used)) vcid_groovx_tcl_io_fieldpkg_cc_utc20050712162004[] = "$Id: fieldpkg.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00178 #endif // !GROOVX_TCL_IO_FIELDPKG_CC_UTC20050712162004_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:41 2008 by Doxygen version 1.5.5.