]> git.sesse.net Git - casparcg/blob - core/producer/scene/expression_parser.h
* Added some libraries in linux
[casparcg] / core / producer / scene / expression_parser.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 "../variable.h"
25
26 namespace caspar { namespace core { namespace scene {
27
28 typedef std::function<variable& (const std::wstring& name)> variable_repository;
29
30 boost::any parse_expression(
31                 std::wstring::const_iterator& cursor,
32                 const std::wstring& str,
33                 const variable_repository& var_repo);
34
35 template<typename T>
36 bool is(const boost::any& value)
37 {
38         return value.type() == typeid(T);
39 }
40
41 template<typename T>
42 T as(const boost::any& value)
43 {
44         return boost::any_cast<T>(value);
45 }
46
47 template<typename T>
48 static binding<T> parse_expression(
49                 const std::wstring& str, const variable_repository& var_repo)
50 {
51         auto cursor = str.cbegin();
52         auto expr = parse_expression(cursor, str, var_repo);
53
54         if (is<binding<T>>(expr))
55                 return boost::any_cast<binding<T>>(expr);
56         else
57                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(
58                                 L"parse_expression() Unsupported type "
59                                 + u16(expr.type().name())));
60 }
61
62 template<>
63 binding<std::wstring> parse_expression(
64                 const std::wstring& str, const variable_repository& var_repo)
65 {
66         auto cursor = str.cbegin();
67         auto expr = parse_expression(cursor, str, var_repo);
68
69         if (is<binding<std::wstring>>(expr))
70                 return as<binding<std::wstring>>(expr);
71         else if (is<binding<double>>(expr))
72                 return as<binding<double>>(expr).as<std::wstring>();
73         else
74                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(
75                                 L"parse_expression() Unsupported type "
76                                 + u16(expr.type().name())));
77 }
78
79 }}}