Rice  1.5.2
 All Classes Files Functions Variables Typedefs Friends Pages
Arg_impl.hpp
1 #ifndef Rice__Arg_Impl_hpp_
2 #define Rice__Arg_Impl_hpp_
3 
4 namespace Rice {
5 
7 
26  class Arg
27  {
28  public:
30 
34  Arg(const char* name)
35  : name_(name)
36  , defaultValue(0)
37  {}
38 
40  Arg(const Arg& other)
41  : name_(other.name()),
42  defaultValue(other.defaultValue ? other.defaultValue->clone() : 0)
43  {}
44 
45  virtual ~Arg()
46  {
47  if(defaultValue) {
48  delete defaultValue;
49  }
50  }
51 
53 
59  template<typename Arg_Type>
60  Arg& operator=(Arg_Type val)
61  {
62  defaultValue = new type<Arg_Type>(val);
63  return *this;
64  }
65 
67  bool hasDefaultValue() const {
68  return defaultValue != 0;
69  }
70 
72 
74  template<typename Arg_Type>
75  Arg_Type getDefaultValue()
76  {
77  return static_cast< type<Arg_Type>* >(defaultValue)->held;
78  }
79 
81  const char* name() const
82  {
83  return name_;
84  }
85 
86  private:
87 
89  const char* name_;
90 
96  class type_base
97  {
98  public:
99  virtual ~type_base() {}
100  virtual type_base* clone() const = 0;
101  };
102 
103  template<typename Type>
104  class type : public type_base
105  {
106  public:
107  type(Type value)
108  :held(value)
109  {}
110 
111  virtual ~type() { }
112 
113  virtual type_base* clone() const
114  {
115  return new type(held);
116  }
117 
118  Type held;
119  };
120 
121  public:
122 
124  type_base* defaultValue;
125  };
126 
127 }
128 
129 #endif // Rice__Arg_Impl_hpp_
type_base * defaultValue
Our saved default value.
Definition: Arg_impl.hpp:124
bool hasDefaultValue() const
Check if this Arg has a default value associated with it.
Definition: Arg_impl.hpp:67
Arg_Type getDefaultValue()
Return the default value associated with this Arg.
Definition: Arg_impl.hpp:75
Arg(const Arg &other)
Copy Constructor.
Definition: Arg_impl.hpp:40
Arg & operator=(Arg_Type val)
Set the default value for this Arg.
Definition: Arg_impl.hpp:60
const char * name() const
Get the name of this Arg.
Definition: Arg_impl.hpp:81
Arg(const char *name)
Initialize a new Arg with the name of the argument.
Definition: Arg_impl.hpp:34
Helper for defining default arguments of a method.
Definition: Arg_impl.hpp:26