2 /// Conversion helpers used by from_nasal<T>(naContext, naRef)
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_FROM_NASAL_DETAIL_HXX_
21 #define SG_FROM_NASAL_DETAIL_HXX_
23 #include <simgear/nasal/nasal.h>
25 #include <boost/utility/enable_if.hpp>
26 #include <boost/type_traits.hpp>
29 #include <typeinfo> // std::bad_cast
37 * Thrown when converting a type from/to Nasal has failed
44 * Construct with generic error message
49 * Construct from an error message
51 * @param msg Error message/description
53 explicit bad_nasal_cast(const std::string& msg);
55 virtual ~bad_nasal_cast() throw();
58 * Get a description of the cause of the failed cast.
60 virtual const char* what() const throw();
67 * Convert Nasal string to std::string
69 std::string from_nasal(naContext c, naRef ref, std::string*);
72 * Convert a Nasal hash to a nasal::Hash
74 Hash from_nasal(naContext c, naRef ref, Hash*);
77 * Convert a Nasal number to a C++ numeric type
80 typename boost::enable_if< boost::is_arithmetic<T>,
83 from_nasal(naContext c, naRef ref, T*)
85 naRef num = naNumValue(ref);
87 throw bad_nasal_cast("Not a number");
89 return static_cast<T>(num.num);
93 * Convert a Nasal vector to a std::vector
95 template<class Vector, class T>
96 typename boost::enable_if< boost::is_same<Vector, std::vector<T> >,
99 from_nasal(naContext c, naRef ref, Vector*)
101 if( !naIsVector(ref) )
102 throw bad_nasal_cast("Not a vector");
104 int size = naVec_size(ref);
107 for(int i = 0; i < size; ++i)
108 vec[i] = from_nasal<T>(c, naVec_get(ref, i));
115 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */