Graphite  Version 3
An experimental 3D geometry processing program
connection.h
Go to the documentation of this file.
1 /*
2  * GXML/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 #ifndef H_OGF_GOM_TYPES_CONNECTION_H
38 #define H_OGF_GOM_TYPES_CONNECTION_H
39 
40 #include <OGF/gom/common/common.h>
41 #include <OGF/gom/types/callable.h>
42 
43 #include <string>
44 #include <vector>
45 #include <set>
46 
51 namespace OGF {
52 
53  class ConnectionList;
54 
55  /******************************************************************/
56 
65  gom_attribute(abstract, "true")
66  gom_class GOM_API Connection : public Callable {
67  public:
68 
75  Object* source, const std::string& sig_name
76  );
77 
81  virtual ~Connection();
82 
90  virtual bool invoke(const ArgList& args, Any& ret_val);
91 
92 
99  virtual bool invoke_target(
100  const ArgList& args, Any& ret_val
101  ) = 0;
102 
107  Object* source() const {
108  return source_;
109  }
110 
115  const std::string& signal_name() const {
116  return signal_name_;
117  }
118 
119  gom_slots:
120 
132  Connection* if_arg(
133  const std::string& name, const std::string& value
134  ) {
135  if(conditions_.has_arg(name)) {
136  Logger::err("GOM")
137  << "if_arg(): duplicate condition for " << name
138  << std::endl;
139  return this;
140  }
141  conditions_.create_arg(name, value);
142  return this;
143  }
144 
154  Connection* add_arg(const std::string& name, const std::string& value) {
155  if(args_.has_arg(name)) {
156  Logger::err("GOM")
157  << "add_arg(): argument " << name
158  << " already exists"
159  << std::endl;
160  return this;
161  }
162  args_.create_arg(name, value);
163  return this;
164  }
165 
175  Connection* rename_arg(const std::string& name, const std::string& new_name) {
176  if(rename_args_.has_arg(name)) {
177  Logger::err("GOM")
178  << "rename_arg(): duplicate rename for " << name
179  << std::endl;
180  return this;
181  }
182  rename_args_.create_arg(name, new_name);
183  return this;
184  }
185 
194  Connection* discard_arg(const std::string& name) {
195  discard_args_.insert(name);
196  return this;
197  }
198 
203  void remove();
204 
205  protected:
214  bool test_arg_conditions(const ArgList& args) const;
215 
226  const std::string& value, const std::string& condition
227  ) const;
228 
235  void translate_args(const ArgList& args_in, ArgList& args_out);
236 
237  private:
238  Object* source_;
239  std::string signal_name_;
240  ArgList conditions_;
241  ArgList args_;
242  ArgList rename_args_;
243  std::set<std::string> discard_args_;
244  };
245 
250 
251  /**********************************************************************/
252 
256  gom_class GOM_API SlotConnection : public Connection {
257  public:
267  Object* source, const std::string& sig_name,
268  Object* target, const std::string& slot_name
269  );
270 
275  const ArgList& args, Any& ret_val
276  ) override;
277 
278  private:
279  Object* target_;
280  std::string slot_name_;
281  };
282 
283  /**********************************************************************/
284 
292  gom_class GOM_API CallableConnection : public Connection {
293  public:
302  Object* source, const std::string& sig_name, Callable* target
303  );
304 
309  const ArgList& args, Any& ret_val
310  ) override;
311 
312  private:
313  Callable_var target_;
314  };
315 
316 
317  /**********************************************************************/
318 
323  class ConnectionList : public std::vector<Connection_var> {
324  public:
329  void invoke(const ArgList& args) {
330  Any ret_val;
331  for(unsigned int i=0; i<size(); i++) {
332  (*this)[i]->invoke(args, ret_val);
333  }
334  }
335 
341  void remove(Connection* conn) {
342  std::vector<Connection_var>::iterator it = this->begin();
343  while(it != this->end()) {
344  if(it->get() == conn) {
345  erase(it);
346  return;
347  }
348  ++it;
349  }
351  }
352  };
353 
354  /******************************************************************/
355 
356 }
357 
358 #endif
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition: assert.h:177
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
A class that stores a variable of arbitrary type.
Definition: any.h:62
Represents a list of name-value pairs.
Definition: arg_list.h:65
A Connection between a signal and an abstract Callable object.
Definition: connection.h:292
bool invoke_target(const ArgList &args, Any &ret_val) override
Directly invokes the target with an arguments list, without any argument translation.
CallableConnection(Object *source, const std::string &sig_name, Callable *target)
CallableConnection constructor.
A Callable object.
Definition: callable.h:50
A list of connections.
Definition: connection.h:323
void remove(Connection *conn)
Removes a connection from a ConnectionList.
Definition: connection.h:341
void invoke(const ArgList &args)
Invokes all the connected slots.
Definition: connection.h:329
virtual bool invoke(const ArgList &args, Any &ret_val)
Invokes the target with an arguments list.
virtual bool invoke_target(const ArgList &args, Any &ret_val)=0
Directly invokes the target with an arguments list, without any argument translation.
Connection * rename_arg(const std::string &name, const std::string &new_name)
Renames an argument.
Definition: connection.h:175
void remove()
Removes this connection from the slot it is connected to.
Connection * discard_arg(const std::string &name)
Discards an argument.
Definition: connection.h:194
bool test_arg_conditions(const ArgList &args) const
Tests whether an argument list satisfies the argument conditions.
void translate_args(const ArgList &args_in, ArgList &args_out)
Applies all the argument list transformations.
virtual ~Connection()
Connection destructor.
const std::string & signal_name() const
Gets the signal name.
Definition: connection.h:115
Object * source() const
Gets the source.
Definition: connection.h:107
bool test_arg_condition(const std::string &value, const std::string &condition) const
Tests whether an argument satisfies a condition.
Connection * add_arg(const std::string &name, const std::string &value)
Adds an argument.
Definition: connection.h:154
Connection(Object *source, const std::string &sig_name)
Connection constructor.
Base class for all objects in the GOM system.
Definition: object.h:65
A Connection between a signal and a slot.
Definition: connection.h:256
SlotConnection(Object *source, const std::string &sig_name, Object *target, const std::string &slot_name)
SlotConnection constructor.
bool invoke_target(const ArgList &args, Any &ret_val) override
Directly invokes the target with an arguments list, without any argument translation.
Global Graphite namespace.
Definition: common.h:76
SmartPointer< Connection > Connection_var
An automatic reference-counted pointer to a Connection.
Definition: connection.h:249
Definitions common to all include files in the gom library.