Rice  3.0.0
Object_defn.hpp
1 #ifndef Rice__Object_defn__hpp_
2 #define Rice__Object_defn__hpp_
3 
7 #include "Identifier.hpp"
8 #include "detail/ruby.hpp"
9 
10 #include <iosfwd>
11 #include <vector>
12 
13 namespace Rice
14 {
15 
16 class Class;
17 class String;
18 class Array;
19 
21 
24 class Object
25 {
26 public:
28  Object(VALUE value = Qnil) : value_(value) { }
29 
31  Object(Object const & other) : value_(other.value()) { }
32 
34  virtual ~Object() { }
35 
38  // Having this conversion also prevents accidental conversion to
39  // undesired integral types (e.g. long or int) by making the
40  // conversion ambiguous.
41  bool test() const { return RTEST(value_); }
42 
45  operator bool() const { return test(); }
46 
48  bool is_nil() const { return NIL_P(value_); }
49 
51  operator VALUE() const { return value_; }
52 
54  // Returns a const ref so that Address_Registration_Guard can access
55  // the address where the VALUE is stored
56  VALUE const volatile & value() const { return value_; }
57 
59 
61  Class class_of() const;
62 
64 
68  int compare(Object const & other) const;
69 
71 
75  String to_s() const;
76 
78 
82  String inspect() const;
83 
85  void freeze();
86 
88 
90  bool is_frozen() const;
91 
93  void swap(Object & other);
94 
96 
101 
103 
107  int rb_type() const;
108 
110 
114  bool is_a(Object klass) const;
115 
117 
121  bool respond_to(Identifier id) const;
122 
124 
127  bool is_instance_of(Object klass) const;
128 
130 
135  template<typename T>
136  void iv_set(
137  Identifier name,
138  T const & value);
139 
141 
145  Identifier name) const;
146 
148  //unset.
153  Identifier name) const;
154 
156 
172  template<typename ...ArgT>
173  Object call(Identifier id, ArgT... args) const;
174 
176 
183 
185  void mark() const;
186 
187 protected:
189  void set_value(VALUE v);
190 
192  template<typename ...ArgT>
193  std::vector<VALUE> convert_args(ArgT&... args) const;
194 
195 private:
196  volatile VALUE value_;
197 };
198 
199 std::ostream & operator<<(std::ostream & out, Object const & obj);
200 
201 bool operator==(Object const & lhs, Object const & rhs);
202 bool operator!=(Object const & lhs, Object const & rhs);
203 bool operator<(Object const & lhs, Object const & rhs);
204 bool operator>(Object const & lhs, Object const & rhs);
205 
206 extern Object const Nil;
207 extern Object const True;
208 extern Object const False;
209 extern Object const Undef;
210 
211 } // namespace Rice
212 
213 #endif // Rice__Object_defn__hpp_
214 
A wrapper for the ruby Array class.
Definition: Array.hpp:24
A helper for defining a Class and its methods.
Definition: Class_defn.hpp:24
A wrapper for the ID type.
Definition: Identifier.hpp:16
The base class for all Objects.
Definition: Object_defn.hpp:25
int rb_type() const
Return the type of the underlying C object.
VALUE const volatile & value() const
Explicitly get the encapsulated VALUE.
Definition: Object_defn.hpp:56
bool respond_to(Identifier id) const
Determine if the objects responds to a method.
String to_s() const
Return a string representation of an object.
void iv_set(Identifier name, T const &value)
Set an instance variable.
Object call(Identifier id, ArgT... args) const
Call the Ruby method specified by 'id' on object 'obj'.
bool is_frozen() const
Determine if the object is frozen.
Object(VALUE value=Qnil)
Encapsulate an existing ruby object.
Definition: Object_defn.hpp:28
bool is_nil() const
Returns true if the object is nil, false otherwise.
Definition: Object_defn.hpp:48
void freeze()
Freeze the object.
virtual ~Object()
Destructor.
Definition: Object_defn.hpp:34
bool is_a(Object klass) const
Determine whether the object is an instance of a class/module.
void mark() const
Mark the object with the garbage collector.
Object(Object const &other)
Copy constructor.
Definition: Object_defn.hpp:31
int compare(Object const &other) const
Compare this object to another object.
Object attr_get(Identifier name) const
Get the value of an instance variable, but don't warn if it is.
Object iv_get(Identifier name) const
Get the value of an instance variable.
std::vector< VALUE > convert_args(ArgT &... args) const
Unpack the provided arguments and convert them all to Ruby types.
Object instance_eval(String const &s)
Evaluate the given string in the context of the object.
void swap(Object &other)
Swap with another Object.
void set_value(VALUE v)
Set the encapsulated value.
Class class_of() const
Get the class of an object.
bool is_instance_of(Object klass) const
Determine whether class is the object's class.
bool test() const
Definition: Object_defn.hpp:41
String inspect() const
Inspect the object.
Object vcall(Identifier id, Array args)
Vectorized call.
A Wraper for the ruby String class.
Definition: String.hpp:25