Graphite  Version 3
An experimental 3D geometry processing program
arg_list.h
Go to the documentation of this file.
1 /*
2  * OGF/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_BASIC_TYPES_ARG_LIST_H
38 #define H_OGF_BASIC_TYPES_ARG_LIST_H
39 
40 #include <OGF/gom/common/common.h>
41 #include <OGF/gom/types/any.h>
42 #include <geogram/basic/string.h>
43 #include <geogram/basic/memory.h>
44 #include <iostream>
45 #include <type_traits>
46 
52 namespace OGF {
53 
59  class NameBase {
60  };
61 
65  class GOM_API ArgList {
66 
67  public:
68 
72  ArgList() {
73  argval_.reserve(4);
74  argname_.reserve(4);
75  }
76 
81  ArgList(const ArgList& rhs) :
82  argval_(rhs.argval_), argname_(rhs.argname_) {
83  }
84 
89  ArgList& operator=(const ArgList& rhs) {
90  argval_ = rhs.argval_;
91  argname_ = rhs.argname_;
92  return *this;
93  }
94 
99  index_t nb_args() const {
100  return argname_.size();
101  }
102 
108  bool has_unnamed_args() const {
109  if(nb_args() == 0) {
110  return false;
111  }
112  for(index_t i=0; i<nb_args(); ++i) {
113  if(ith_arg_name(i) != "arg#" + String::to_string(i)) {
114  return false;
115  }
116  }
117  return true;
118  }
119 
129  bool has_arg(const std::string& name) const {
130  return (find_arg_index(name) != index_t(-1));
131  }
132 
140  index_t find_arg_index(const std::string& name) const;
141 
147  void delete_ith_arg(index_t index) {
148  geo_debug_assert(index < nb_args());
149  argval_.erase(argval_.begin()+index);
150  argname_.erase(argname_.begin()+index);
151  }
152 
160  const std::string& ith_arg_name(index_t i) const {
161  geo_debug_assert(i < nb_args());
162  return argname_[i];
163  }
164 
176  template <class T> T ith_arg_value(index_t i) const {
177  geo_debug_assert(i < nb_args());
178  T result;
179  if(
180  !argval_[i].get_value(result) &&
181  !get_name(argval_[i], result)
182  ) {
183  arg_type_error(i, typeid(T).name());
184  }
185  return result;
186  }
187 
196  const Any& ith_arg_value(index_t i) const {
197  geo_debug_assert(i < nb_args());
198  return argval_[i];
199  }
200 
210  geo_debug_assert(i < nb_args());
211  return argval_[i];
212  }
213 
222  const Any& arg_value(const std::string& name) const {
223  index_t i = find_arg_index(name);
224  geo_debug_assert(i != index_t(-1));
225  return argval_[i];
226  }
227 
235  Any& arg_value(const std::string& name) {
236  index_t i = find_arg_index(name);
237  geo_debug_assert(i != index_t(-1));
238  return argval_[i];
239  }
240 
241 
249  geo_debug_assert(i < nb_args());
250  return argval_[i].meta_type();
251  }
252 
259  Any& create_arg(const std::string& name) {
260  geo_debug_assert(!has_arg(name));
261  argval_.push_back(Any());
262  argname_.push_back(name);
263  return *(argval_.rbegin());
264  }
265 
272  geo_debug_assert(nb_args() == 0 || has_unnamed_args());
273  return create_arg("arg#" + String::to_string(nb_args()));
274  }
275 
283  template <class T> void create_arg(
284  const std::string& name, const T& value
285  ) {
286  geo_debug_assert(!has_arg(name));
287  argval_.push_back(Any());
288  argname_.push_back(name);
289  argval_.rbegin()->set_value(value);
290  }
291 
299  const std::string& name, const char* value
300  ) {
301  create_arg<std::string>(name, value);
302  }
303 
311  const std::string& name, const Any& value
312  ) {
313  geo_debug_assert(!has_arg(name));
314  argval_.push_back(value);
315  argname_.push_back(name);
316  }
317 
327  template <class T> void set_arg(
328  const std::string& name, const T& value
329  ) {
330  index_t i = find_arg_index(name);
331  if(i == index_t(-1)) {
332  create_arg(name, value);
333  } else {
334  argval_[i].set_value(value);
335  }
336  }
337 
346  void set_arg(const std::string& name, const char* value) {
347  set_arg<std::string>(name, value);
348  }
349 
350 
359  void set_arg(const std::string& name, const Any& value) {
360  index_t i = find_arg_index(name);
361  if(i == index_t(-1)) {
362  create_arg(name, value);
363  } else {
364  argval_[i] = value;
365  }
366  }
367 
375  template<class T> void set_ith_arg(index_t i, const T& value) {
376  geo_debug_assert(i < nb_args());
377  argval_[i].set_value(value);
378  }
379 
386  void set_ith_arg(index_t i, const char* value) {
387  set_ith_arg<std::string>(i, value);
388  }
389 
396  std::string get_arg(const std::string& name) const {
397  index_t i = find_arg_index(name);
398  geo_debug_assert(i != index_t(-1));
399  return argval_[i].as_string();
400  }
401 
412  template <class T> T get_arg(const std::string& name) const {
413  index_t i = find_arg_index(name);
414  geo_debug_assert(i != index_t(-1));
415  T result;
416  if(
417  !argval_[i].get_value(result) &&
418  !get_name(argval_[i], result)
419  ) {
420  arg_type_error(i, typeid(T).name());
421  }
422  return result;
423  }
424 
431  MetaType* get_arg_type(const std::string& name) const {
432  index_t i = find_arg_index(name);
433  geo_debug_assert(i != index_t(-1));
434  return argval_[i].meta_type();
435  }
436 
440  void clear() {
441  argval_.resize(0);
442  argname_.resize(0);
443  }
444 
453  void append(const ArgList& rhs, bool overwrite = true);
454 
465  const ArgList& rhs, index_t i, bool overwrite = true
466  );
467 
468  void serialize(std::ostream& out) const;
469 
477  index_t i, const std::string& expected_typeid_name
478  ) const;
479 
480 
492  template <class T> static bool get_name(const Any& argval, T& name) {
493  if(!std::is_base_of<NameBase, T>::value) {
494  return false;
495  }
496  Any name_prop;
497  if(!get_object_name(argval, name_prop)) {
498  return false;
499  }
500  return name_prop.get_value(name);
501  }
502 
513  static bool get_object_name(const Any& object, Any& name);
514 
515 
516  private:
517  vector<Any> argval_;
518  vector<std::string> argname_;
519  };
520 
521 
527  inline std::ostream& operator<<(std::ostream& out, const ArgList& args) {
528  args.serialize(out);
529  return out;
530  }
531 }
532 
533 #endif
A class to hold a value of arbitrary type.
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
Vector with aligned memory allocation.
Definition: memory.h:635
A class that stores a variable of arbitrary type.
Definition: any.h:62
bool get_value(T &value) const
Gets the stored value.
Definition: any.h:210
Represents a list of name-value pairs.
Definition: arg_list.h:65
MetaType * get_arg_type(const std::string &name) const
Gets the type of an argument.
Definition: arg_list.h:431
void append(const ArgList &rhs, bool overwrite=true)
Appends all the arguments from an ArgList to this one.
void set_ith_arg(index_t i, const char *value)
Sets an argument by index.
Definition: arg_list.h:386
ArgList()
ArgList constructor.
Definition: arg_list.h:72
void create_arg(const std::string &name, const Any &value)
Creates an argument from an Any.
Definition: arg_list.h:310
ArgList(const ArgList &rhs)
ArgList copy-constructor.
Definition: arg_list.h:81
void create_arg(const std::string &name, const char *value)
Creates an argument from a string litteral.
Definition: arg_list.h:298
void clear()
Removes all the arguments from this ArgList.
Definition: arg_list.h:440
const Any & ith_arg_value(index_t i) const
Gets argument value by index, stored as an Any.
Definition: arg_list.h:196
void delete_ith_arg(index_t index)
Deletes an argument by index.
Definition: arg_list.h:147
Any & create_arg(const std::string &name)
Creates an uninitialized argument.
Definition: arg_list.h:259
void set_ith_arg(index_t i, const T &value)
Sets an argument by index.
Definition: arg_list.h:375
Any & create_unnamed_arg()
Creates an uninitialized unnamed argument.
Definition: arg_list.h:271
bool has_arg(const std::string &name) const
Tests whether an argument of a given name exists in this ArgList.
Definition: arg_list.h:129
void set_arg(const std::string &name, const char *value)
Sets an argument from a string litteral.
Definition: arg_list.h:346
void arg_type_error(index_t i, const std::string &expected_typeid_name) const
Displays an error message for invalid argument type.
static bool get_name(const Any &argval, T &name)
Converts a pointer to Object into a string using its name attribute if target type is a xxxName.
Definition: arg_list.h:492
void append_ith_arg(const ArgList &rhs, index_t i, bool overwrite=true)
Appends an argument from an ArgList to this one.
const std::string & ith_arg_name(index_t i) const
Gets argument name by index.
Definition: arg_list.h:160
void set_arg(const std::string &name, const Any &value)
Sets an argument from an Any.
Definition: arg_list.h:359
Any & arg_value(const std::string &name)
Gets argument value by name, stored as an Any.
Definition: arg_list.h:235
T ith_arg_value(index_t i) const
Gets argument value by index.
Definition: arg_list.h:176
static bool get_object_name(const Any &object, Any &name)
Gets the name of an object.
void create_arg(const std::string &name, const T &value)
Creates an argument.
Definition: arg_list.h:283
MetaType * ith_arg_type(index_t i) const
Gets the type of an argument by index.
Definition: arg_list.h:248
T get_arg(const std::string &name) const
Gets an argument by name.
Definition: arg_list.h:412
std::string get_arg(const std::string &name) const
Gets a string representation of an argument.
Definition: arg_list.h:396
bool has_unnamed_args() const
Tests whether this ArgList has unnamed args.
Definition: arg_list.h:108
void set_arg(const std::string &name, const T &value)
Sets an argument.
Definition: arg_list.h:327
const Any & arg_value(const std::string &name) const
Gets argument value by name, stored as an Any.
Definition: arg_list.h:222
index_t nb_args() const
Gets the number of arguments.
Definition: arg_list.h:99
ArgList & operator=(const ArgList &rhs)
ArgList assignment operator.
Definition: arg_list.h:89
Any & ith_arg_value(index_t i)
Gets argument value by index, stored as an Any.
Definition: arg_list.h:209
index_t find_arg_index(const std::string &name) const
Finds argument index by name.
The representation of a type in the Meta repository.
Definition: meta_type.h:221
Base class for all Names in Graphite (GrobName ...).
Definition: arg_list.h:59
Types and functions for memory manipulation.
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Global Graphite namespace.
Definition: common.h:76
std::ostream & operator<<(std::ostream &out, const ArgList &args)
Prints an ArgList into a stream.
Definition: arg_list.h:527
Definitions common to all include files in the gom library.
Functions for string manipulation.