Back to index

texmacs  1.0.7.15
generic_tree.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : generic_tree.hpp
00004 * DESCRIPTION: generic objects as trees
00005 * COPYRIGHT  : (C) 2009  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef GENERIC_TREE_H
00013 #define GENERIC_TREE_H
00014 #include "tree.hpp"
00015 #include "blackbox.hpp"
00016 
00017 class generic_rep: public tree_rep {
00018 public:
00019   blackbox data;
00020   template<typename T> inline generic_rep (const T& x):
00021     tree_rep ((tree_label) type_helper<T>::id),
00022     data (close_box<T> (x)) {}
00023   friend class tree;
00024 };
00025 
00026 inline blackbox
00027 as_blackbox (const tree& t) {
00028   return ((generic_rep*) t.rep) -> data;
00029 }
00030 
00031 template<typename T, typename F>
00032 struct convert_helper {
00033   static inline T op (const F& x) { return (T) x; }
00034 };
00035 
00036 template<typename F>
00037 struct convert_helper<tree,F> {
00038   static inline tree op (const F& data) {
00039     return tree ((tree_rep*) tm_new<generic_rep> (data)); }
00040 };
00041 
00042 template<typename T>
00043 struct convert_helper<T,tree> {
00044   static inline T op (const tree& t) {
00045     return open_box<T> (as_blackbox (t)); }
00046 };
00047 
00048 template<typename T> inline bool is (const tree& t) {
00049   return ((int) L(t)) == type_helper<T>::id; }
00050 template<typename T, typename F> inline T as (const F& t) {
00051   return convert_helper<F,T>::op (t); }
00052 
00053 #endif // defined GENERIC_TREE_H