]> git.sesse.net Git - casparcg/blob - core/producer/variable.h
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / variable.h
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 #pragma once
23
24 #include <string>
25 #include <typeinfo>
26
27 #include <boost/lexical_cast.hpp>
28
29 #include <common/log.h>
30 #include <common/except.h>
31
32 #include "binding.h"
33
34 namespace caspar { namespace core {
35
36 template<typename T> class variable_impl;
37
38 class variable
39 {
40 private:
41         std::wstring original_expr_;
42         bool is_public_;
43 public:
44         variable(const std::wstring& expr, bool is_public)
45                 : original_expr_(expr), is_public_(is_public)
46         {
47         }
48
49         virtual ~variable()
50         {
51         }
52
53         const std::wstring& original_expr() const
54         {
55                 return original_expr_;
56         }
57
58         bool is_public() const
59         {
60                 return is_public_;
61         }
62
63         template<typename T>
64         bool is() const
65         {
66                 return is(typeid(T));
67         }
68
69         template<typename T>
70         binding<T>& as()
71         {
72                 return dynamic_cast<variable_impl<T>&>(*this).value();
73         }
74
75         template<typename T>
76         const binding<T>& as() const
77         {
78                 return dynamic_cast<const variable_impl<T>&>(*this).value();
79         }
80
81         virtual void from_string(const std::wstring& raw_value) = 0;
82         virtual std::wstring to_string() const = 0;
83 private:
84         virtual bool is(const std::type_info& type) const = 0;
85 };
86
87 template<typename T>
88 class variable_impl : public variable
89 {
90 private:
91         binding<T> value_;
92 public:
93         variable_impl()
94                 : variable(L"", true)
95         {
96         }
97
98         variable_impl(const std::wstring& expr, bool is_public, T initial_value = T())
99                 : variable(expr, is_public)
100                 , value_(initial_value)
101         {
102         }
103
104         binding<T>& value()
105         {
106                 return value_;
107         }
108
109         const binding<T>& value() const
110         {
111                 return value_;
112         }
113
114         virtual void from_string(const std::wstring& raw_value)
115         {
116                 if (std::is_same<bool, T>::value)
117                 {
118                         binding<bool>& bool_binding = *reinterpret_cast<binding<bool>*>(&value_);
119
120                         if (raw_value == L"true")
121                                 bool_binding.set(true);
122                         else if (raw_value == L"false")
123                                 bool_binding.set(false);
124                         else
125                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"bool constants should be true or false"));
126                 }
127                 else
128                         value_.set(boost::lexical_cast<T>(raw_value));
129         }
130
131         virtual std::wstring to_string() const
132         {
133                 return boost::lexical_cast<std::wstring>(value_.get());
134         }
135 private:
136         virtual bool is(const std::type_info& type) const
137         {
138                 return typeid(T) == type;
139         }
140 };
141
142 }}