2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
27 #include <boost/lexical_cast.hpp>
29 #include <common/log.h>
30 #include <common/except.h>
34 namespace caspar { namespace core {
36 template<typename T> class variable_impl;
41 std::wstring original_expr_;
44 variable(const std::wstring& expr, bool is_public)
45 : original_expr_(expr), is_public_(is_public)
53 const std::wstring& original_expr() const
55 return original_expr_;
58 bool is_public() const
72 return dynamic_cast<variable_impl<T>&>(*this).value();
76 const binding<T>& as() const
78 return dynamic_cast<const variable_impl<T>&>(*this).value();
81 virtual void from_string(const std::wstring& raw_value) = 0;
82 virtual std::wstring to_string() const = 0;
84 virtual bool is(const std::type_info& type) const = 0;
88 class variable_impl : public variable
98 variable_impl(const std::wstring& expr, bool is_public, T initial_value = T())
99 : variable(expr, is_public)
100 , value_(initial_value)
109 const binding<T>& value() const
114 virtual void from_string(const std::wstring& raw_value)
116 if (std::is_same<bool, T>::value)
118 binding<bool>& bool_binding = *reinterpret_cast<binding<bool>*>(&value_);
120 if (raw_value == L"true")
121 bool_binding.set(true);
122 else if (raw_value == L"false")
123 bool_binding.set(false);
125 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"bool constants should be true or false"));
128 value_.set(boost::lexical_cast<T>(raw_value));
131 virtual std::wstring to_string() const
133 return boost::lexical_cast<std::wstring>(value_.get());
136 virtual bool is(const std::type_info& type) const
138 return typeid(T) == type;