pm.hh 1.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once

#include <glm/glm.hpp>

#include <iostream>
#include <string>

#include "../Mesh.hh"
#include "../attribute_collection.hh"
#include "../attributes.hh"

#include "../detail/AttributeSerializer.hh"

namespace polymesh
{
void write_pm(std::string const &filename, Mesh const &mesh, attribute_collection const &attributes);
void write_pm(std::ostream &out, Mesh const &mesh, attribute_collection const &attributes);

bool read_pm(std::string const &filename, Mesh &mesh, attribute_collection &attributes);
bool read_pm(std::istream &input, Mesh &mesh, attribute_collection &attributes);

namespace detail
{
template <typename T>
struct bytewise_serdes
{
27
28
    void serialize(std::ostream &out, T const *data, size_t num_items) const { out.write(reinterpret_cast<char const *>(data), num_items * sizeof(T)); }
    void deserialize(std::istream &in, T *data, size_t num_items) const { in.read(reinterpret_cast<char *>(data), num_items * sizeof(T)); }
29
30
};

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
struct string_serdes
{
    void serialize(std::ostream &out, std::string const *strings, size_t num_items) const
    {
        for (size_t i = 0; i < num_items; i++)
            out.write(strings[i].c_str(), strings[i].size() + 1);
    }
    void deserialize(std::istream &in, std::string *strings, size_t num_items) const
    {
        for (size_t i = 0; i < num_items; i++)
            std::getline(in, strings[i], '\0');
    }
};

void register_attribute_serializer(std::string const &identifier, std::unique_ptr<detail::GenericAttributeSerializer> ptr);
46
47
}

48
49
template <typename T, typename serdes = detail::bytewise_serdes<T>>
void register_type(std::string const &identifier, serdes &&serializer = detail::bytewise_serdes<T>{})
50
{
51
52
    auto ptr = std::make_unique<detail::AttributeSerializer<T, serdes>>(serializer);
    detail::register_attribute_serializer(identifier, std::move(ptr));
53
54
}
}