1 // Conversion helpers used by from_nasal<T>(naContext, naRef)
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef SG_FROM_NASAL_DETAIL_HXX_
20 #define SG_FROM_NASAL_DETAIL_HXX_
22 #include <simgear/nasal/nasal.h>
24 #include <boost/utility/enable_if.hpp>
25 #include <boost/type_traits.hpp>
28 #include <typeinfo> // std::bad_cast
36 * Thrown when converting a type from/to Nasal has failed
43 explicit bad_nasal_cast(const std::string& msg);
45 virtual ~bad_nasal_cast() throw();
46 virtual const char* what() const throw();
53 * Convert Nasal string to std::string
55 std::string from_nasal(naContext c, naRef ref, std::string*);
58 * Convert a Nasal hash to a nasal::Hash
60 Hash from_nasal(naContext c, naRef ref, Hash*);
63 * Convert a Nasal number to a C++ numeric type
66 typename boost::enable_if< boost::is_arithmetic<T>,
69 from_nasal(naContext c, naRef ref, T*)
71 naRef num = naNumValue(ref);
73 throw bad_nasal_cast("Not a number");
75 return static_cast<T>(num.num);
79 * Convert a Nasal vector to a std::vector
81 template<class Vector, class T>
82 typename boost::enable_if< boost::is_same<Vector, std::vector<T> >,
85 from_nasal(naContext c, naRef ref, Vector*)
87 if( !naIsVector(ref) )
88 throw bad_nasal_cast("Not a vector");
90 int size = naVec_size(ref);
93 for(int i = 0; i < size; ++i)
94 vec[i] = from_nasal<T>(c, naVec_get(ref, i));
101 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */