]> git.sesse.net Git - casparcg/blob - core/system_info_provider.cpp
Merge pull request #506 from dimitry-ishenko-casparcg/fixes-flags
[casparcg] / core / system_info_provider.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@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 #include "StdAfx.h"
23
24 #include "system_info_provider.h"
25
26 #include <boost/property_tree/ptree.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread/lock_guard.hpp>
29 #include <boost/algorithm/string/case_conv.hpp>
30
31 #include <vector>
32 #include <map>
33
34 namespace caspar { namespace core {
35
36 struct system_info_provider_repository::impl
37 {
38         mutable boost::mutex                                            mutex_;
39         std::vector<system_info_provider>                       info_providers_;
40         std::map<std::wstring, version_provider>        version_providers_;
41
42         void register_system_info_provider(system_info_provider provider)
43         {
44                 boost::lock_guard<boost::mutex> lock(mutex_);
45
46                 info_providers_.push_back(std::move(provider));
47         }
48
49         void register_version_provider(const std::wstring& version_name, version_provider provider)
50         {
51                 boost::lock_guard<boost::mutex> lock(mutex_);
52
53                 version_providers_.insert(std::make_pair(boost::algorithm::to_lower_copy(version_name), std::move(provider)));
54         }
55
56         void fill_information(boost::property_tree::wptree& info) const
57         {
58                 boost::lock_guard<boost::mutex> lock(mutex_);
59
60                 for (auto& provider : info_providers_)
61                         provider(info);
62         }
63
64         std::wstring get_version(const std::wstring& version_name) const
65         {
66                 boost::lock_guard<boost::mutex> lock(mutex_);
67                 
68                 auto found = version_providers_.find(boost::algorithm::to_lower_copy(version_name));
69
70                 if (found == version_providers_.end())
71                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"No version provider with name " + version_name));
72
73                 return found->second();
74         }
75 };
76
77 system_info_provider_repository::system_info_provider_repository()
78         : impl_(new impl)
79 {
80 }
81
82 void system_info_provider_repository::register_system_info_provider(system_info_provider provider)
83 {
84         impl_->register_system_info_provider(std::move(provider));
85 }
86
87 void system_info_provider_repository::register_version_provider(
88                 const std::wstring& version_name, version_provider provider)
89 {
90         impl_->register_version_provider(version_name, provider);
91 }
92
93 void system_info_provider_repository::fill_information(boost::property_tree::wptree& info) const
94 {
95         impl_->fill_information(info);
96 }
97
98 std::wstring system_info_provider_repository::get_version(const std::wstring& version_name) const
99 {
100         return impl_->get_version(version_name);
101 }
102
103 }}