aboutsummaryrefslogtreecommitdiff
path: root/libbutl/uuid-io.cxx
blob: 7eed1342f053ce4f96cddf1815f31568509a9870 (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
// file      : libbutl/uuid-io.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbutl/uuid-io.hxx>

#include <ostream>
#include <istream>
#include <stdexcept> // invalid_argument

using namespace std;

namespace butl
{
  ostream&
  operator<< (ostream& os, const uuid& u)
  {
    return os << u.c_string ().data ();
  }

  istream&
  operator>> (istream& is, uuid& u)
  {
    u = uuid ();

    char s[37];
    if (is.read (s, 36))
    {
      s[36] ='\0';

      try
      {
        u = uuid (s);
      }
      catch (const invalid_argument&)
      {
        is.setstate (istream::failbit);
      }
    }

    return is;
  }
}