Permalink
Browse files

Generalize the Vector specializations

  • Loading branch information...
1 parent af364a1 commit 987624854acc9a1b6f9fdbda53fa32f7044683ff Martin York committed Apr 24, 2012
@@ -15,8 +15,11 @@ namespace ThorsAnvil
template<typename C>
struct ContainerTraits;
-template<typename SerializeInfo, typename C, bool EnablePod = boost::is_fundamental<typename ContainerTraits<C>::DataType>::value>
-class JsonContainerImportAction: public ThorsAnvil::Json::SaxAction
+template<typename SerializeInfo, typename C, bool EnablePod = boost::is_fundamental<typename ContainerTraits<C>::DataType>::value, bool isConstContainer = ContainerTraits<C>::isConstContainer>
+class JsonContainerImportAction;
+
+template<typename SerializeInfo, typename C>
+class JsonContainerImportAction<SerializeInfo, C, false, true>: public ThorsAnvil::Json::SaxAction
{
typedef C LocalType;
typedef typename ContainerTraits<C>::ValueType ValueType;
@@ -39,7 +42,7 @@ class JsonContainerImportAction: public ThorsAnvil::Json::SaxAction
}
};
template<typename SerializeInfo, typename C>
-class JsonContainerImportAction<SerializeInfo, C, true>: public ThorsAnvil::Json::SaxAction
+class JsonContainerImportAction<SerializeInfo, C, true, true>: public ThorsAnvil::Json::SaxAction
{
C& destination;
public:
@@ -55,8 +58,46 @@ class JsonContainerImportAction<SerializeInfo, C, true>: public ThorsAnvil::Json
destination.insert(value.getValue<typename ContainerTraits<C>::DataType>());
}
};
+template<typename SerializeInfo, typename C>
+class JsonContainerImportAction<SerializeInfo, C, false, false>: public ThorsAnvil::Json::SaxAction
+{
+ typedef C LocalType;
+ typedef typename ContainerTraits<C>::DataType DataType;
+ LocalType& destination;
+ public:
+ JsonContainerImportAction(LocalType& dst)
+ : destination(dst)
+ {}
+ virtual void doPreAction(ThorsAnvil::Json::ScannerSax& parser, ThorsAnvil::Json::Key const& key)
+ {
+ destination.push_back(DataType());
+ boost::mpl::for_each<typename JsonSerializeTraits<DataType>::SerializeInfo>(MPLForEachActivateItem<DataType, ThorsAnvil::Json::ScannerSax>(parser, destination.back()));
+ }
+ virtual void doAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&, JsonValue const&)
+ {}
+};
+
+template<typename SerializeInfo, typename C>
+class JsonContainerImportAction<SerializeInfo, C, true, false>: public ThorsAnvil::Json::SaxAction
+{
+ typedef C LocalType;
+ typedef typename ContainerTraits<C>::DataType DataType;
+ LocalType& destination;
+
+ public:
+ JsonContainerImportAction(LocalType& dst)
+ : destination(dst)
+ {}
+
+ virtual void doPreAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&)
+ {}
+ virtual void doAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&, JsonValue const& value)
+ {
+ destination.push_back(value.getValue<DataType>());
+ }
+};
template<typename SerializeInfo, typename C>
@@ -65,6 +106,7 @@ ThorsAnvil::Json::SaxAction* new_JsonImportAction(C& dst)
return new JsonContainerImportAction<SerializeInfo,C>(dst);
}
+
template<typename C>
class JsonContainerAttributeAccessor
{
@@ -89,7 +131,6 @@ class JsonContainerAttributeAccessor
}
};
-
}
}
}
@@ -18,6 +18,7 @@ namespace ThorsAnvil
template<typename K, typename V>
struct ContainerTraits<std::map<K,V> >
{
+ static bool const isConstContainer = true;
typedef V DataType;
typedef std::pair<K,V> ValueType;
};
@@ -56,7 +57,7 @@ class JsonContainerAttributeAccessor<std::map<std::string, V> >
};
template<typename SerializeInfo, typename V>
-class JsonContainerImportAction<SerializeInfo, std::map<std::string,V>, true>: public ThorsAnvil::Json::SaxAction
+class JsonContainerImportAction<SerializeInfo, std::map<std::string,V>, true, true>: public ThorsAnvil::Json::SaxAction
{
std::map<std::string,V>& destination;
public:
@@ -74,7 +75,7 @@ class JsonContainerImportAction<SerializeInfo, std::map<std::string,V>, true>: p
};
template<typename SerializeInfo, typename V>
-class JsonContainerImportAction<SerializeInfo, std::map<std::string, V>, false>: public ThorsAnvil::Json::SaxAction
+class JsonContainerImportAction<SerializeInfo, std::map<std::string, V>, false, true>: public ThorsAnvil::Json::SaxAction
{
std::map<std::string,V>& destination;
public:
@@ -15,6 +15,7 @@ namespace ThorsAnvil
template<typename T>
struct ContainerTraits<std::set<T> >
{
+ static bool const isConstContainer = true;
typedef T DataType;
typedef T ValueType;
};
@@ -13,82 +13,23 @@ namespace ThorsAnvil
namespace Json
{
-template<typename SerializeInfo, typename T, bool EnablePod = boost::is_fundamental<T>::value>
-class JsonArrImportAction: public ThorsAnvil::Json::SaxAction
-{
- typedef std::vector<T> LocalType;
- LocalType& destination;
-
- public:
- JsonArrImportAction(LocalType& dst)
- : destination(dst)
- {}
-
- virtual void doPreAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&)
- {}
- virtual void doAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&, JsonValue const& value)
- {
- destination.push_back(value.getValue<T>());
- }
-};
-
-template<typename SerializeInfo, typename T>
-class JsonArrImportAction<SerializeInfo, T, false>: public ThorsAnvil::Json::SaxAction
-{
- typedef std::vector<T> LocalType;
- LocalType& destination;
-
- public:
- JsonArrImportAction(LocalType& dst)
- : destination(dst)
- {}
-
- virtual void doPreAction(ThorsAnvil::Json::ScannerSax& parser, ThorsAnvil::Json::Key const& key)
- {
- destination.push_back(T());
- boost::mpl::for_each<typename JsonSerializeTraits<T>::SerializeInfo>(MPLForEachActivateItem<T, ThorsAnvil::Json::ScannerSax>(parser, destination.back()));
- }
- virtual void doAction(ThorsAnvil::Json::ScannerSax&, ThorsAnvil::Json::Key const&, JsonValue const&)
- {}
-};
-template<typename SerializeInfo, typename T>
-ThorsAnvil::Json::SaxAction* new_JsonImportAction(std::vector<T>& dst)
-{
- return new JsonArrImportAction<SerializeInfo,T>(dst);
-}
-
template<typename T>
-class JsonArrAttributeAccessor
+struct ContainerTraits<std::vector<T> >
{
- public:
- void serialize(std::vector<T> const& src, std::ostream& stream) const
- {
- if (!src.empty())
- {
- typename std::vector<T>::const_iterator loop = src.begin();
- stream << jsonInternalExport(*loop);
-
- for(++loop; loop != src.end(); ++loop)
- {
- stream << "," << jsonInternalExport(*loop);
- }
- }
- }
- std::auto_ptr<ThorsAnvil::Json::SaxAction> action(std::vector<T>& dst) const
- {
- std::auto_ptr<ThorsAnvil::Json::SaxAction> action(new_JsonImportAction<typename JsonSerializeTraits<T>::SerializeInfo>(dst));
- return action;
- }
+ static bool const isConstContainer = false;
+ typedef T DataType;
};
+
template<typename T>
struct JsonSerializeTraits<std::vector<T> >
{
static JsonSerializeType const type = Array;
- typedef std::vector<T> LocalType;
- typedef JsonArrAttributeAccessor<T> Accessor;
+
+ typedef std::vector<T> LocalType;
+ typedef JsonContainerAttributeAccessor<LocalType> Accessor;
THORSANVIL_SERIALIZE_JsonGenericArrAttributeAccess(LocalType, Accessor);
- typedef boost::mpl::vector<genericAccessor> SerializeInfo;
+ typedef boost::mpl::vector<genericAccessor> SerializeInfo;
};

0 comments on commit 9876248

Please sign in to comment.