1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #include "attribute-list.h"
22 #include "singleton.h"
26 /*********************************************************************
27 * The AttributeList container implementation
28 *********************************************************************/
30 AttributeList::AttributeList ()
33 AttributeList::AttributeList (const AttributeList &o)
35 for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++)
38 attr.checker = i->checker;
39 attr.value = i->value->Copy ();
40 m_attributes.push_back (attr);
44 AttributeList::operator = (const AttributeList &o)
47 for (Attrs::const_iterator i = o.m_attributes.begin (); i != o.m_attributes.end (); i++)
50 attr.checker = i->checker;
51 attr.value = i->value->Copy ();
52 m_attributes.push_back (attr);
56 AttributeList::~AttributeList ()
62 AttributeList::Set (std::string name, const AttributeValue &value)
64 struct TypeId::AttributeInfo info;
65 bool ok = TypeId::LookupAttributeByFullName (name, &info);
68 NS_FATAL_ERROR ("Could not find attribute "<<name);
70 ok = DoSet (&info, value);
73 NS_FATAL_ERROR ("Could not set value for attribute "<<name);
77 AttributeList::SetFailSafe (std::string name, const AttributeValue &value)
79 struct TypeId::AttributeInfo info;
80 bool ok = TypeId::LookupAttributeByFullName (name, &info);
85 ok = DoSet (&info, value);
89 AttributeList::SetWithTid (TypeId tid, std::string name, const AttributeValue & value)
91 struct TypeId::AttributeInfo info;
92 bool ok = tid.LookupAttributeByName (name, &info);
95 NS_FATAL_ERROR ("Could not find attribute "<<tid.GetName ()<<"::"<<name);
97 ok = DoSet (&info, value);
100 NS_FATAL_ERROR ("Could not set value for attribute "<<tid.GetName ()<<"::"<<name);
105 AttributeList::DoSetOne (Ptr<const AttributeChecker> checker, const AttributeValue &value)
107 // get rid of any previous value stored in this
109 for (Attrs::iterator k = m_attributes.begin (); k != m_attributes.end (); k++)
111 if (k->checker == checker)
113 m_attributes.erase (k);
117 // store the new value.
119 attr.checker = checker;
120 attr.value = value.Copy ();
121 m_attributes.push_back (attr);
124 AttributeList::DoSet (struct TypeId::AttributeInfo *info, const AttributeValue &value)
126 if (info->checker == 0)
130 bool ok = info->checker->Check (value);
133 DoSetOne (info->checker, value);
137 // attempt to convert to string.
138 const StringValue *str = dynamic_cast<const StringValue *> (&value);
143 // attempt to convert back to value.
144 Ptr<AttributeValue> v = info->checker->Create ();
145 ok = v->DeserializeFromString (str->Get (), info->checker);
150 ok = info->checker->Check (*v);
155 DoSetOne (info->checker, *v);
159 AttributeList::Reset (void)
161 m_attributes.clear ();
164 AttributeList::GetGlobal (void)
166 return Singleton<AttributeList>::Get ();
170 AttributeList::LookupAttributeFullNameByChecker (Ptr<const AttributeChecker> checker) const
172 for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
174 TypeId tid = TypeId::GetRegistered (i);
175 for (uint32_t j = 0; j < tid.GetAttributeN (); j++)
177 if (checker == tid.GetAttributeChecker (j))
179 return tid.GetAttributeFullName (j);
183 NS_FATAL_ERROR ("Could not find requested Accessor.");
189 AttributeList::SerializeToString (void) const
191 std::ostringstream oss;
192 for (Attrs::const_iterator i = m_attributes.begin (); i != m_attributes.end ();)
194 std::string name = LookupAttributeFullNameByChecker (i->checker);
195 oss << name << "=" << i->value->SerializeToString (i->checker);
197 if (i != m_attributes.end ())
205 AttributeList::DeserializeFromString (std::string str)
209 std::string::size_type cur;
212 std::string::size_type equal = str.find ("=", cur);
213 if (equal == std::string::npos)
215 NS_FATAL_ERROR ("Error while parsing serialized attribute: \"" << str << "\"");
220 std::string name = str.substr (cur, equal-cur);
221 struct TypeId::AttributeInfo info;
222 if (!TypeId::LookupAttributeByFullName (name, &info))
224 NS_FATAL_ERROR ("Error while parsing serialized attribute: name does not exist: \"" << name << "\"");
229 std::string::size_type next = str.find ("|", cur);
231 if (next == std::string::npos)
233 value = str.substr (equal+1, str.size () - (equal+1));
238 value = str.substr (equal+1, next - (equal+1));
241 Ptr<AttributeValue> val = info.checker->Create ();
242 bool ok = val->DeserializeFromString (value, info.checker);
245 NS_FATAL_ERROR ("Error while parsing serialized attribute: value invalid: \"" << value << "\"");
250 DoSetOne (info.checker, *val);
254 } while (cur != str.size ());