]> git.sesse.net Git - casparcg/blob - common/software_version.h
[CHANGELOG] Updated
[casparcg] / common / software_version.h
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <string>
25 #include <vector>
26
27 #include <boost/array.hpp>
28 #include <boost/algorithm/string/split.hpp>
29
30 namespace caspar {
31
32 template<size_t NUM>
33 class software_version
34 {
35         boost::array<int, NUM> numbers_;
36         std::string version_;
37 public:
38         software_version(std::string version)
39                 : version_(std::move(version))
40         {
41                 std::fill(numbers_.begin(), numbers_.end(), 0);
42                 std::vector<std::string> tokens;
43                 boost::split(tokens, version_, boost::is_any_of("."));
44                 auto num = std::min(NUM, tokens.size());
45
46                 for (size_t i = 0; i < num; ++i)
47                 {
48                         try
49                         {
50                                 numbers_[i] = boost::lexical_cast<int>(tokens[i]);
51                         }
52                         catch (const boost::bad_lexical_cast&)
53                         {
54                                 return;
55                         }
56                 }
57         };
58
59         const std::string& to_string() const
60         {
61                 return version_;
62         }
63
64         bool operator<(const software_version<NUM>& other) const
65         {
66                 for (int i = 0; i < NUM; ++i)
67                 {
68                         int this_num = numbers_[i];
69                         int other_num = other.numbers_[i];
70
71                         if (this_num < other_num)
72                                 return true;
73                         else if (this_num > other_num)
74                                 return false;
75                 }
76
77                 return false;
78         }
79 };
80
81 }