Synopsis - Cross-Reference

File: /src/Synopsis/ASG/DeclaredTypeId.hh
 1//
 2// Copyright (C) 2004 Stefan Seefeld
 3// All rights reserved.
 4// Licensed to the public under the terms of the GNU LGPL (>= 2),
 5// see the file COPYING for details.
 6//
 7
 8#ifndef _Synopsis_ASG_DeclaredTypeId_hh
 9#define _Synopsis_ASG_DeclaredTypeId_hh
10
11#include <Synopsis/ASG/TypeId.hh>
12#include <Synopsis/ASG/Declaration.hh>
13
14namespace Synopsis
15{
16namespace ASG
17{
18
19class DeclaredTypeId : public NamedTypeId
20{
21public:
22  DeclaredTypeId() {}
23  DeclaredTypeId(const Object &o, bool check = true)
24    : NamedTypeId(o, false) { if (check) assert_type("DeclaredTypeId");}
25
26  Declaration declaration() const { return attr("declaration")();}
27
28  virtual void accept(TypeIdVisitor *v) { v->visit_declared_type_id(this);}
29};
30
31//. Template types are declared template types. They have a name, a
32//. declaration (which is an AST::Class) and a vector of parameters
33//. declare this template. Each parameter (using AST::Parameter) should be
34//. either the correct type for non-type parameters, or a Dependent for type
35//. parameters. In either case, there may be default values.
36class TemplateId : public DeclaredTypeId
37{
38public:
39  typedef Python::TypedList<Parameter> Parameters;
40
41  TemplateId() {}
42  TemplateId(const Python::Object &o, bool check = true)
43    : DeclaredTypeId(o, false) { if (check) assert_type("TemplateId");}
44
45  Parameters parameters() { return narrow<Parameters>(attr("parameters")());}
46  // specializations() ???
47
48  virtual void accept(TypeIdVisitor *v) { v->visit_template_id(this);}
49};
50
51class ParametrizedTypeId : public TypeId
52{
53public:
54  ParametrizedTypeId() {}
55  ParametrizedTypeId(const Python::Object &o, bool check = true)
56    : TypeId(o, false) { if (check) assert_type("ParametrizedTypeId");}
57
58  TemplateId template_() const { return narrow<TemplateId>(attr("template")());}  
59  TypeIdList parameters() const { return narrow<TypeIdList>(attr("parameters")());}
60
61  virtual void accept(TypeIdVisitor *v) { v->visit_parametrized_type_id(this);}
62};
63
64//. Safely extracts typed Declarations from Named types. The type is first
65//. safely cast to Declared, then the declaration() safely cast to
66//. the template type.
67template <typename T>
68T declared_type_id_cast(const TypeId &type) throw (Python::Object::TypeError)
69{
70  if (DeclaredTypeId declared = Python::Object::try_narrow<DeclaredTypeId>(type))
71    if (Declaration decl = declared.declaration())
72      if (T derived = Python::Object::try_narrow<T>(decl))
73	return derived;
74  throw Python::Object::TypeError();
75}
76
77// //. Safely extracts typed Declarations from Type types. The type is first
78// //. safely cast to Declared, then the declaration() safely cast to
79// //. the template type.
80// template <typename T>
81// T declared_cast(const Type &type) throw (Object::TypeError)
82// {
83//   if (Declared declared = Object::try_narrow<Declared>(type))
84//     if (Declaration decl = declared.declaration())
85//       if (T derived = Object::try_narrow<T>(decl))
86// 	return derived;
87//   throw Object::TypeError();
88// }
89
90}
91}
92
93#endif