summaryrefslogtreecommitdiffstats
path: root/libdimensionxx/progress.cpp
blob: ca74571b89d10bd0775c0385fde3ae9ed62f7cb6 (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
/*************************************************************************
 * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com>               *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library is distributed in the hope that it will be      *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

#include "dimensionxx.hpp"

namespace Dimension
{
  Persist_Base::~Persist_Base()
  { }

  Progress::Progress(dmnsn_progress* progress)
    : m_progress(new dmnsn_progress*(progress))
  { }

  Progress::Progress(dmnsn_progress* progress, const Persister& persister)
    : m_progress(new dmnsn_progress*(progress)), m_persister(persister)
  { }

  Progress::~Progress()
  {
    if (m_progress && m_progress.unique()) {
      try {
        dmnsn_finish_progress(dmnsn());
      } catch (...) {
        dmnsn_error(SEVERITY_MEDIUM,
                    "Finishing worker thread failed in Progress destructor.");
      }
    }
  }

  double
  Progress::progress() const
  {
    return dmnsn_get_progress(dmnsn());
  }

  void
  Progress::wait(double progress) const
  {
    dmnsn_wait_progress(dmnsn(), progress);
  }

  void
  Progress::new_element(unsigned int total)
  {
    dmnsn_new_progress_element(dmnsn(), total);
  }

  void
  Progress::increment()
  {
    dmnsn_increment_progress(dmnsn());
  }

  void
  Progress::done()
  {
    dmnsn_progress_done(dmnsn());
  }

  void
  Progress::finish()
  {
    if (m_progress.unique()) {
      dmnsn_finish_progress(dmnsn());
      m_progress.reset();
    } else {
      throw Dimension_Error("Attempt to finish non-unique Progress.");
    }
  }

  // Access the set of persisted objects
  Persister&
  Progress::persister()
  {
    return m_persister;
  }

  dmnsn_progress*
  Progress::dmnsn()
  {
    if (!m_progress) {
      throw Dimension_Error("Attempting to access finished array.");
    }

    return *m_progress;
  }

  const dmnsn_progress*
  Progress::dmnsn() const
  {
    if (!m_progress) {
      throw Dimension_Error("Attempting to access finished array.");
    }

    return *m_progress;
  }
}