aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/serializer/wildcard/email.hxx
blob: 6fd873d978aa85dea4f253ea3f0c289f1f503d00 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// file      : examples/cxx/serializer/wildcard/email.hxx
// copyright : not copyrighted - public domain

#ifndef EMAIL_HXX
#define EMAIL_HXX

#include <string>
#include <vector>
#include <cstddef> // std::size_t
#include <cstring> // std::memcpy

namespace email
{
  //
  //
  enum body_type
  {
    text_body,
    binary_body
  };

  struct body
  {
    virtual body_type
    type () const = 0;

    virtual
    ~body ()
    {
    }
  };

  //
  //
  struct text: body
  {
    text (const std::string& content)
        : content_ (content)
    {
    }

    virtual body_type
    type () const
    {
      return text_body;
    }

    const std::string&
    content () const
    {
      return content_;
    }

  private:
    std::string content_;
  };

  //
  //
  struct binary: body
  {
    binary (const std::string& name,
            const std::string& mime,
            const char* data,
            std::size_t size)
        : name_ (name), mime_ (mime), data_ (0), size_ (size)
    {
      if (size_ != 0)
      {
        data_ = new char[size_];
        std::memcpy (data_, data, size_);
      }
    }

    virtual
    ~binary ()
    {
      delete[] data_;
    }

    virtual body_type
    type () const
    {
      return binary_body;
    }

    const std::string&
    name () const
    {
      return name_;
    }

    const std::string&
    mime () const
    {
      return mime_;
    }

    const char*
    data () const
    {
      return data_;
    }

    std::size_t
    size () const
    {
      return size_;
    }

  private:
    std::string name_;
    std::string mime_;
    char* data_;
    std::size_t size_;
  };

  //
  //
  struct envelope
  {
    envelope (const std::string& to,
              const std::string& from,
              const std::string& subject,
              unsigned int thread_id)
        : to_ (to), from_ (from), subject_ (subject), thread_id_ (thread_id)
    {
    }

    const std::string&
    to () const
    {
      return to_;
    }

    const std::string&
    from () const
    {
      return from_;
    }

    const std::string&
    subject () const
    {
      return subject_;
    }

    unsigned int
    thread_id () const
    {
      return thread_id_;
    }

    // Assumes ownership of the passed object.
    //
    void
    add_body (body* b)
    {
      bodies_.push_back (b);
    }

    typedef std::vector<body*> bodies;
    typedef bodies::const_iterator body_iterator;

    body_iterator
    begin_body () const
    {
      return bodies_.begin ();
    }

    body_iterator
    end_body () const
    {
      return bodies_.end ();
    }

    ~envelope ()
    {
      for (body_iterator i = begin_body (); i != end_body (); ++i)
        delete *i;
    }

  private:
    const std::string to_;
    const std::string from_;
    const std::string subject_;
    unsigned int thread_id_;

    bodies bodies_;
  };
}

#endif // EMAIL_HXX