readattribmap.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2003-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Wed Jun 25 13:19:26 2003
00011 // commit: $Id: readattribmap.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/io/readattribmap.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_IO_READATTRIBMAP_H_UTC20050626084021_DEFINED
00036 #define GROOVX_IO_READATTRIBMAP_H_UTC20050626084021_DEFINED
00037 
00038 #include "io/io.h"
00039 
00040 #include "rutz/fstring.h"
00041 #include "rutz/sfmt.h"
00042 
00043 #include <utility>
00044 #include <sstream>
00045 #include <vector>
00046 
00047 namespace io
00048 {
00049   class attrib_map
00050   {
00051   public:
00052 
00053     struct attrib
00054     {
00055     private:
00056       attrib(); // not implemented
00057 
00058     public:
00059       attrib(const rutz::fstring& t, const rutz::fstring& v) :
00060         type(t), value(v) {}
00061 
00062       rutz::fstring type;
00063       rutz::fstring value;
00064     };
00065 
00066   private:
00067     typedef std::pair<rutz::fstring, attrib>  value_type;
00068     typedef std::vector<value_type>           list_type;
00069 
00070     rutz::fstring   m_obj_tag;
00071     list_type       m_attribs;
00072     io::version_id  m_version_id;
00073 
00074   public:
00075     inline
00076     attrib_map(const rutz::fstring& obj_tag);
00077 
00078     inline
00079     io::version_id get_version_id() const;
00080 
00081     inline
00082     void set_version_id(io::version_id id);
00083 
00084     inline
00085     attrib get(const rutz::fstring& attr_name);
00086 
00087     inline
00088     void add_attrib(const rutz::fstring& attr_name,
00089                     const rutz::fstring& type,
00090                     const rutz::fstring& value);
00091   };
00092 }
00093 
00094 #include "rutz/error.h"
00095 
00096 inline
00097 io::attrib_map::attrib_map(const rutz::fstring& obj_tag) :
00098   m_obj_tag(obj_tag),
00099   m_attribs(),
00100   m_version_id(0)
00101 {}
00102 
00103 inline
00104 io::version_id io::attrib_map::get_version_id() const
00105 {
00106   return m_version_id;
00107 }
00108 
00109 inline
00110 void io::attrib_map::set_version_id(io::version_id id)
00111 {
00112   m_version_id = id;
00113 }
00114 
00115 inline
00116 io::attrib_map::attrib io::attrib_map::get(const rutz::fstring& attr_name)
00117 {
00118   list_type::iterator itr = m_attribs.begin(), end = m_attribs.end();
00119   while (itr != end)
00120     {
00121       if ((*itr).first == attr_name)
00122         {
00123           attrib result = (*itr).second;
00124           m_attribs.erase(itr);
00125           return result;
00126         }
00127       ++itr;
00128     }
00129 
00130   const rutz::fstring msg =
00131     rutz::sfmt("no attribute named '%s' for %s\n"
00132                "known attributes are:\n",
00133                attr_name.c_str(),
00134                m_obj_tag.c_str());
00135 
00136   std::ostringstream buf;
00137 
00138   buf << msg;
00139 
00140   itr = m_attribs.begin();
00141   while (itr != end)
00142     {
00143       buf << '\t' << (*itr).first << '\n';
00144       ++itr;
00145     }
00146 
00147   throw rutz::error(rutz::fstring(buf.str().c_str()), SRC_POS);
00148 }
00149 
00150 inline
00151 void io::attrib_map::add_attrib(const rutz::fstring& attr_name,
00152                                 const rutz::fstring& type,
00153                                 const rutz::fstring& value)
00154 {
00155   m_attribs.push_back(value_type(attr_name, attrib(type,value)));
00156 }
00157 
00158 static const char __attribute__((used)) vcid_groovx_io_readattribmap_h_utc20050626084021[] = "$Id: readattribmap.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00159 #endif // !GROOVX_IO_READATTRIBMAP_H_UTC20050626084021_DEFINED

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