Ejemplo n.º 1
0
void PropertyOwner::serialize(XmlSerializer& s) const {
    // create temporary property map for serialization
    std::map<std::string, Property*> propertyMap;
    for (std::vector<Property*>::const_iterator it = properties_.begin(); it != properties_.end(); ++it)
        propertyMap[(*it)->getID()] = *it;

    // serialize properties
    const bool usePointerContentSerialization = s.getUsePointerContentSerialization();
    s.setUsePointerContentSerialization(true);
    try {
        s.serialize("Properties", propertyMap, "Property", "name");
    }
    catch (SerializationException& e) {
        LWARNING(e.what());
    }
    s.setUsePointerContentSerialization(usePointerContentSerialization);
}
Ejemplo n.º 2
0
/**
 * Tests serialization and deserialization using pointer content serialization
 * mixed up with using XML attributes.
 *
 * @attention This is a grey box test which uses implementation details
 *            to keep the test as short as possible. That is why
 *            you have to consider changing the test every time
 *            the implementation details have changed.
 */
void testUsePointerContentSerialization() {
    int i = 1;
    int* ip = &i;
    std::vector<int*> v;
    v.push_back(new int(2));
    std::map<std::string, int*> m;
    m["three"] = new int(3);

    std::stringstream stream;

    XmlSerializer s;
    s.setUsePointerContentSerialization(true);
    s.serialize("i", i);
    s.serialize("ip", ip);
    s.serialize("v", v);
    s.serialize("m", m, "number");
    s.setUseAttributes(true);
    s.serialize("ai", i);
    s.serialize("aip", ip);
    s.serialize("av", v);
    s.serialize("am", m, "number");
    s.write(stream);

    // Reset all variables to default values...
    i = 0;
    ip = new int(0);
    *v[0] = 0;
    *m["three"] = 0;

    int ai = 0;
    int* aip = new int(0);
    std::vector<int*> av;
    av.push_back(new int(0));
    std::map<std::string, int*> am;
    am["three"] = new int(0);

    XmlDeserializer d;
    d.read(stream);
    d.setUsePointerContentSerialization(true);
    d.deserialize("i", i);
    d.deserialize("ip", ip);
    d.deserialize("v", v);
    d.deserialize("m", m, "number");
    d.setUseAttributes(true);
    d.deserialize("ai", ai);
    d.deserialize("aip", aip);
    d.deserialize("av", av);
    d.deserialize("am", am, "number");

    test(i, 1, "i incorrect deserialized");
    test(ip != &i, "ip is pointing to adress of i");
    test(*ip, 1, "ip incorrect deserialized");
    test(v.size() == 1, "v: incorrect size");
    test(*v[0], 2, "v: first item incorrect deserialized");
    test(m.size() == 1, "m: incorrect size");
    test(*m["three"], 3, "m: first item incorrect deserialized");

    test(ai, 1, "ai incorrect deserialized");
    test(aip != &ai, "aip is pointing to adress of ai");
    test(*aip, 1, "aip incorrect deserialized");
    test(av.size() == 1, "av: incorrect size");
    test(*av[0], 2, "av: first item incorrect deserialized");
    test(am.size() == 1, "am: incorrect size");
    test(*am["three"], 3, "am: first item incorrect deserialized");

    delete ip;
    delete v[0];
    delete m["three"];
    delete aip;
    delete av[0];
    delete am["three"];
}