2 /// Conversion functions to convert C++ types to Nasal types
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library 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 GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef SG_TO_NASAL_HXX_
21 #define SG_TO_NASAL_HXX_
23 #include "nasal_traits.hxx"
25 #include <simgear/nasal/nasal.h>
27 #include <boost/utility/enable_if.hpp>
28 #include <boost/type_traits.hpp>
38 * Convert std::string to Nasal string
40 naRef to_nasal(naContext c, const std::string& str);
43 * Convert C-string to Nasal string
45 // We need this to prevent the array overload of to_nasal being called
46 naRef to_nasal(naContext c, const char* str);
49 * Convert function pointer to Nasal function
51 naRef to_nasal(naContext c, naCFunction func);
54 * Convert a nasal::Hash to a Nasal hash
56 naRef to_nasal(naContext c, const Hash& hash);
59 * Simple pass-through of naRef types to allow generic usage of to_nasal
61 naRef to_nasal(naContext c, naRef ref);
64 * Convert a numeric type to Nasal number
67 typename boost::enable_if< boost::is_arithmetic<T>, naRef >::type
68 to_nasal(naContext c, T num)
74 * Convert a fixed size array to a Nasal vector
76 template<class T, size_t N>
77 naRef to_nasal(naContext c, const T(&array)[N])
79 naRef ret = naNewVector(c);
80 naVec_setsize(c, ret, N);
81 for(size_t i = 0; i < N; ++i)
82 naVec_set(ret, i, to_nasal(c, array[i]));
87 * Convert std::vector to Nasal vector
89 template< template<class T, class Alloc> class Vector,
93 typename boost::enable_if< boost::is_same< Vector<T,Alloc>,
98 to_nasal(naContext c, const Vector<T, Alloc>& vec)
100 naRef ret = naNewVector(c);
101 naVec_setsize(c, ret, vec.size());
102 for(size_t i = 0; i < vec.size(); ++i)
103 naVec_set(ret, i, to_nasal(c, vec[i]));
108 * Convert a 2d vector to Nasal vector with 2 elements
111 typename boost::enable_if<is_vec2<Vec2>, naRef>::type
112 to_nasal(naContext c, const Vec2& vec)
114 // We take just double because in Nasal every number is represented as
116 double nasal_vec[2] = {vec[0], vec[1]};
117 return to_nasal(c, nasal_vec);
122 #endif /* SG_TO_NASAL_HXX_ */