Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Serializer.h

Go to the documentation of this file.
00001 #ifndef DEBTAGS_SERIALIZER_H
00002 #define DEBTAGS_SERIALIZER_H
00003 
00008 /*
00009  * Copyright (C) 2005  Enrico Zini <enrico@debian.org>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  */
00025 
00026 #include <tagcoll/Exception.h>
00027 #include <tagcoll/Serializer.h>
00028 
00029 #include <debtags/Vocabulary.h>
00030 #include <debtags/Tag.h>
00031 
00032 namespace Debtags
00033 {
00034 class Tag;
00035 class Vocabulary;
00036 };
00037 
00038 namespace Tagcoll
00039 {
00040 
00044 template<>
00045 class Converter<Debtags::Facet, std::string>
00046 {
00047 public:
00048     std::string operator()(const Debtags::Facet& item) { return item.name(); }
00049     OpSet<std::string> operator()(const OpSet<Debtags::Facet>& items)
00050     {
00051         OpSet<std::string> res;
00052         for (OpSet<Debtags::Facet>::const_iterator i = items.begin();
00053                 i != items.end(); i++)
00054             if (*i)
00055                 res += (*this)(*i);
00056         return res;
00057     }
00058 };
00059 
00063 template<>
00064 class Converter<Debtags::Tag, std::string>
00065 {
00066 public:
00067     std::string operator()(const Debtags::Tag& item) { return item.fullname(); }
00068     OpSet<std::string> operator()(const OpSet<Debtags::Tag>& items)
00069     {
00070         OpSet<std::string> res;
00071         for (OpSet<Debtags::Tag>::const_iterator i = items.begin();
00072                 i != items.end(); i++)
00073             if (*i)
00074                 res += (*this)(*i);
00075         return res;
00076     }
00077 };
00078 
00082 template<>
00083 class Converter<std::string, Debtags::Facet>
00084 {
00085 protected:
00086     const Debtags::Vocabulary& voc;
00087 
00088 public:
00089     Converter(const Debtags::Vocabulary& voc) : voc(voc) {}
00090 
00091     Debtags::Facet operator()(const std::string& item)
00092     {
00093         Debtags::Facet t = voc.getFacet(item);
00094         // FIXME: find a better default, or even return the invalid facet and
00095         // allow the caller to take care of it
00096         if (!t)
00097             return voc.getFacet("special");
00098         return t;
00099     }
00100     OpSet<Debtags::Facet> operator()(const OpSet<std::string>& items)
00101     {
00102         OpSet<Debtags::Facet> res;
00103         for (OpSet<std::string>::const_iterator i = items.begin();
00104                 i != items.end(); i++)
00105         {
00106             Debtags::Facet t((*this)(*i));
00107             if (t)
00108                 res += t;
00109         }
00110         return res;
00111     }
00112 };
00113 
00117 template<>
00118 class Converter<std::string, Debtags::Tag>
00119 {
00120 protected:
00121     const Debtags::Vocabulary& voc;
00122 
00123 public:
00124     Converter(const Debtags::Vocabulary& voc) : voc(voc) {}
00125 
00126     Debtags::Tag operator()(const std::string& item)
00127     {
00128         Debtags::Tag t = voc.getTag(item);
00129         if (!t)
00130             return voc.getTag("special::invalid-tag");
00131         return t;
00132     }
00133     OpSet<Debtags::Tag> operator()(const OpSet<std::string>& items)
00134     {
00135         OpSet<Debtags::Tag> res;
00136         for (OpSet<std::string>::const_iterator i = items.begin();
00137                 i != items.end(); i++)
00138         {
00139             Debtags::Tag t((*this)(*i));
00140             if (t)
00141                 res += t;
00142         }
00143         return res;
00144     }
00145 };
00146 
00147 };
00148 
00149 // vim:set ts=4 sw=4:
00150 #endif

Generated on Sat Sep 10 03:47:41 2005 for libdebtags by  doxygen 1.4.4