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 * Simple pass through for unified handling also of naRef.
69 inline naRef from_nasal_helper(naContext, naRef ref, naRef*) { return ref; }
72 * Convert Nasal string to std::string
74 std::string from_nasal_helper(naContext c, naRef ref, std::string*);
77 * Convert a Nasal hash to a nasal::Hash
79 Hash from_nasal_helper(naContext c, naRef ref, Hash*);
82 * Convert a Nasal number to a C++ numeric type
85 typename boost::enable_if< boost::is_arithmetic<T>,
88 from_nasal_helper(naContext c, naRef ref, T*)
90 naRef num = naNumValue(ref);
92 throw bad_nasal_cast("Not a number");
94 return static_cast<T>(num.num);
98 * Convert a Nasal vector to a std::vector
101 std::vector<T> from_nasal_helper(naContext c, naRef ref, std::vector<T>*)
103 if( !naIsVector(ref) )
104 throw bad_nasal_cast("Not a vector");
106 int size = naVec_size(ref);
107 std::vector<T> vec(size);
109 for(int i = 0; i < size; ++i)
110 vec[i] = from_nasal_helper(c, naVec_get(ref, i), static_cast<T*>(0));
117 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */