Scarab  v1.6.1
Project 8 C++ Utility Library
param.hh
Go to the documentation of this file.
1 /*
2  * param.hh
3  *
4  * Created on: Jan 14, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef SCARAB_PARAM_HH_
9 #define SCARAB_PARAM_HH_
10 
11 #include <stdint.h>
12 #include <deque>
13 #include <map>
14 #include <sstream>
15 #include <string>
16 
17 #include "error.hh"
18 //#include "logger.hh"
19 #include "path.hh"
20 
21 
22 namespace scarab
23 {
24  //LOGGER(dlog_p, "param");
25  class param_value;
26  class param_array;
27  class param_node;
28 
29 
31  {
32  public:
33  param();
34  param(const param& orig);
35  virtual ~param();
36 
37  virtual param* clone() const;
38 
39  virtual bool is_null() const;
40  virtual bool is_value() const;
41  virtual bool is_array() const;
42  virtual bool is_node() const;
43 
44  virtual bool has_subset( const param& a_subset ) const;
45 
46  param_value& as_value();
47  param_array& as_array();
48  param_node& as_node();
49 
50  const param_value& as_value() const;
51  const param_array& as_array() const;
52  const param_node& as_node() const;
53 
55  const param_value& operator()() const;
57  param_value& operator()();
58 
61  const param& operator[]( unsigned a_index ) const;
64  param& operator[]( unsigned a_index );
65 
68  const param& operator[]( const std::string& a_name ) const;
71  param& operator[]( const std::string& a_name );
72 
73  virtual std::string to_string() const;
74 
75  static unsigned s_indent_level;
76  };
77 
78 
79  class SCARAB_API param_value : public param
80  {
81  public:
82  param_value();
83  param_value( bool a_value );
84  param_value( uint8_t a_value );
85  param_value( uint16_t a_value );
86  param_value( uint32_t a_value );
87  param_value( uint64_t a_value );
88  param_value( int8_t a_value );
89  param_value( int16_t a_value );
90  param_value( int32_t a_value );
91  param_value( int64_t a_value );
92  param_value( float a_value );
93  param_value( double a_value );
94  param_value( const std::string& a_value );
95  param_value( const char* a_value );
96  param_value( const param_value& orig );
97  virtual ~param_value();
98 
99  param_value& operator=( const param_value& rhs );
100 
101  virtual param* clone() const;
102 
103  bool empty() const;
104 
105  virtual bool is_null() const;
106  virtual bool is_value() const;
107 
108  virtual bool has_subset( const param& a_subset ) const;
109 
110  std::string type() const;
111  bool is_bool() const;
112  bool is_uint() const;
113  bool is_int() const;
114  bool is_double() const;
115  bool is_string() const;
116 
117  bool as_bool() const;
118  uint64_t as_uint() const;
119  int64_t as_int() const;
120  double as_double() const;
121  const std::string& as_string() const;
122  path as_path() const;
123 
124  template< typename XValType >
125  XValType get() const;
126 
127  void set( bool a_value );
128  void set( uint8_t a_value );
129  void set( uint16_t a_value );
130  void set( uint32_t a_value );
131  void set( uint64_t a_value );
132  void set( int8_t a_value );
133  void set( int16_t a_value );
134  void set( int32_t a_value );
135  void set( int64_t a_value );
136  void set( float a_value );
137  void set( double a_value );
138  void set( const std::string& a_value );
139  void set( const char* a_value );
140  //template< typename XValType >
141  //void set( XValType a_value );
142 
143  //template< typename XStreamableType >
144  //param_value& operator<<( const XStreamableType& a_streamable );
145 
146  virtual std::string to_string() const;
147 
148  void clear();
149 
150  private:
151  union Values
152  {
153  bool f_bool;
154  uint64_t f_uint;
155  int64_t f_int;
156  double f_double;
157  std::string* f_string;
158  } f_value;
159 
161  {
167  k_invalid
168  } f_value_type;
169 
170  mutable std::string f_buffer;
171  };
172 
173 
174  class SCARAB_API param_array : public param
175  {
176  public:
177  typedef std::deque< param* > contents;
178  typedef contents::iterator iterator;
179  typedef contents::const_iterator const_iterator;
180  typedef contents::reverse_iterator reverse_iterator;
181  typedef contents::const_reverse_iterator const_reverse_iterator;
182  typedef contents::value_type contents_type;
183 
184  public:
185  param_array();
186  param_array( const param_array& orig );
187  virtual ~param_array();
188 
189  param_array& operator=( const param_array& rhs );
190 
191  virtual param* clone() const;
192 
193  virtual bool is_null() const;
194  virtual bool is_array() const;
195 
196  virtual bool has_subset( const param& a_subset ) const;
197 
198  unsigned size() const;
199  bool empty() const;
200 
203  void resize( unsigned a_size );
204 
207  std::string get_value( unsigned a_index ) const;
210  template< typename XValType >
211  XValType get_value( unsigned a_index ) const;
212 
215  std::string get_value( unsigned a_index, const std::string& a_default ) const;
216  std::string get_value( unsigned a_index, const char* a_default ) const;
219  template< typename XValType >
220  XValType get_value( unsigned a_index, XValType a_default ) const;
221 
224  const param* at( unsigned a_index ) const;
227  param* at( unsigned a_index );
228 
231  const param_value* value_at( unsigned a_index ) const;
234  param_value* value_at( unsigned a_index );
235 
238  const param_array* array_at( unsigned a_index ) const;
241  param_array* array_at( unsigned a_index );
242 
245  const param_node* node_at( unsigned a_index ) const;
248  param_node* node_at( unsigned a_index );
249 
252  const param& operator[]( unsigned a_index ) const;
255  param& operator[]( unsigned a_index );
256 
257  const param* front() const;
258  param* front();
259 
260  const param* back() const;
261  param* back();
262 
263  // assign a copy of a_value to the array at a_index
264  void assign( unsigned a_index, const param& a_value );
265  // directly assign a_value_ptr to the array at a_index
266  void assign( unsigned a_index, param* a_value_ptr );
267 
268  void push_back( const param& a_value );
269  void push_back( param* a_value_ptr );
270 
271  void push_front( const param& a_value );
272  void push_front( param* a_value_ptr );
273 
274  void append( const param_array& an_array );
275 
276  void erase( unsigned a_index );
277  param* remove( unsigned a_index );
278  void clear();
279 
280  iterator begin();
281  const_iterator begin() const;
282 
283  iterator end();
284  const_iterator end() const;
285 
286  reverse_iterator rbegin();
287  const_reverse_iterator rbegin() const;
288 
289  reverse_iterator rend();
290  const_reverse_iterator rend() const;
291 
292  virtual std::string to_string() const;
293 
294  protected:
295  contents f_contents;
296  };
297 
298 
299 
300  class SCARAB_API param_node : public param
301  {
302  public:
303  typedef std::map< std::string, param* > contents;
304  typedef contents::iterator iterator;
305  typedef contents::const_iterator const_iterator;
306  typedef contents::value_type contents_type;
307 
308  param_node();
309  param_node( const param_node& orig );
310  virtual ~param_node();
311 
312  param_node& operator=( const param_node& rhs );
313 
314  virtual param* clone() const;
315 
316  virtual bool is_null() const;
317  virtual bool is_node() const;
318 
319  virtual bool has_subset( const param& a_subset ) const;
320 
321  unsigned size() const;
322  bool empty() const;
323 
324  bool has( const std::string& a_name ) const;
325  unsigned count( const std::string& a_name ) const;
326 
329  std::string get_value( const std::string& a_name ) const;
332  template< typename XValType >
333  XValType get_value( const std::string& a_name ) const;
334 
337  std::string get_value( const std::string& a_name, const std::string& a_default ) const;
338  std::string get_value( const std::string& a_name, const char* a_default ) const;
341  template< typename XValType >
342  XValType get_value( const std::string& a_name, XValType a_default ) const;
343 
346  const param* at( const std::string& a_name ) const;
349  param* at( const std::string& a_name );
350 
351  const param_value* value_at( const std::string& a_name ) const;
352  param_value* value_at( const std::string& a_name );
353 
354  const param_array* array_at( const std::string& a_name ) const;
355  param_array* array_at( const std::string& a_name );
356 
357  const param_node* node_at( const std::string& a_name ) const;
358  param_node* node_at( const std::string& a_name );
359 
362  const param& operator[]( const std::string& a_name ) const;
365  param& operator[]( const std::string& a_name );
366 
368  bool add( const std::string& a_name, const param& a_value );
370  bool add( const std::string& a_name, param* a_value_ptr );
371 
373  void replace( const std::string& a_name, const param& a_value );
375  void replace( const std::string& a_name, param* a_value_ptr );
376 
380  void merge( const param_node& a_object );
381 
382  void erase( const std::string& a_name );
383  param* remove( const std::string& a_name );
384  void clear();
385 
386  iterator begin();
387  const_iterator begin() const;
388 
389  iterator end();
390  const_iterator end() const;
391 
392  virtual std::string to_string() const;
393 
394  protected:
395  contents f_contents;
396 
397  };
398 
399  inline param* param::clone() const
400  {
401  //std::cout << "param::clone()" << std::endl;
402  return new param( *this );
403  }
404 
405  inline bool param::is_null() const
406  {
407  return true;
408  }
409 
410  inline bool param::is_value() const
411  {
412  return false;
413  }
414 
415  inline bool param::is_array() const
416  {
417  return false;
418  }
419 
420  inline bool param::is_node() const
421  {
422  return false;
423  }
424 
426  {
427  param_value* t_cast_ptr = dynamic_cast< param_value* >( this );
428  return *t_cast_ptr;
429  }
430 
432  {
433  param_array* t_cast_ptr = dynamic_cast< param_array* >( this );
434  return *t_cast_ptr;
435  }
436 
438  {
439  param_node* t_cast_ptr = dynamic_cast< param_node* >( this );
440  return *t_cast_ptr;
441  }
442 
443  inline const param_value& param::as_value() const
444  {
445  const param_value* t_cast_ptr = dynamic_cast< const param_value* >( this );
446  return *t_cast_ptr;
447  }
448 
449  inline const param_array& param::as_array() const
450  {
451  const param_array* t_cast_ptr = dynamic_cast< const param_array* >( this );
452  return *t_cast_ptr;
453  }
454 
455  inline const param_node& param::as_node() const
456  {
457  const param_node* t_cast_ptr = dynamic_cast< const param_node* >( this );
458  return *t_cast_ptr;
459  }
460 
461  inline const param_value& param::operator()() const
462  {
463  return as_value();
464  }
465 
467  {
468  return as_value();
469  }
470 
471  inline const param& param::operator[]( unsigned a_index ) const
472  {
473  return as_array()[ a_index ];
474  }
475 
476  inline param& param::operator[]( unsigned a_index )
477  {
478  return as_array()[ a_index ];
479  }
480 
481  inline const param& param::operator[]( const std::string& a_name ) const
482  {
483  return as_node()[ a_name ];
484  }
485 
486  inline param& param::operator[]( const std::string& a_name )
487  {
488  return as_node()[ a_name ];
489  }
490 
491  inline std::string param::to_string() const
492  {
493  return std::string();
494  }
495 
496 
497  //************************************
498  //*********** VALUE ****************
499  //************************************
500 
501 
502  template<>
503  inline bool param_value::get< bool >() const
504  {
505  return as_bool();
506  }
507 
508  template<>
509  inline uint64_t param_value::get< uint64_t >() const
510  {
511  return as_uint();
512  }
513 
514  template<>
515  inline int64_t param_value::get< int64_t >() const
516  {
517  return as_int();
518  }
519 
520  template<>
521  inline double param_value::get< double >() const
522  {
523  return as_double();
524  }
525 
526  template<>
527  inline std::string param_value::get< std::string >() const
528  {
529  return as_string();
530  }
531 
532  template<>
534  {
535  return as_path();
536  }
537 
538  template< typename XValType >
539  XValType param_value::get() const
540  {
541  if( f_value_type == k_bool ) return static_cast< XValType >( as_bool() );
542  else if( f_value_type == k_uint ) return static_cast< XValType >( as_uint() );
543  else if( f_value_type == k_int ) return static_cast< XValType >( as_int() );
544  else if( f_value_type == k_double ) return static_cast< XValType >( as_double() );
545  else if( f_value_type == k_string )
546  {
547  std::stringstream t_conv;
548  t_conv << *f_value.f_string;
549  XValType t_return;
550  t_conv >> t_return;
551  return t_return;
552  }
553  return XValType();
554  }
555 
556 
557  inline param* param_value::clone() const
558  {
559  //std::cout << "param_value::clone" << std::endl;
560  return new param_value( *this );
561  }
562 
563  inline bool param_value::is_null() const
564  {
565  return false;
566  }
567 
568  inline bool param_value::is_value() const
569  {
570  return true;
571  }
572 
573  inline bool param_value::is_bool() const
574  {
575  return f_value_type == k_bool;
576  }
577 
578  inline bool param_value::is_uint() const
579  {
580  return f_value_type == k_uint;
581  }
582 
583  inline bool param_value::is_int() const
584  {
585  return f_value_type == k_int;
586  }
587 
588  inline bool param_value::is_double() const
589  {
590  return f_value_type == k_double;
591  }
592 
593  inline bool param_value::is_string() const
594  {
595  return f_value_type == k_string;
596  }
597 
598  inline void param_value::set( bool a_value )
599  {
600  if( f_value_type == k_string ) delete f_value.f_string;
601  f_value_type = k_bool;
602  f_value.f_bool = a_value;
603  return;
604  }
605 
606  inline void param_value::set( uint8_t a_value )
607  {
608  if( f_value_type == k_string ) delete f_value.f_string;
609  f_value_type = k_uint;
610  f_value.f_uint = a_value;
611  return;
612  }
613 
614  inline void param_value::set( uint16_t a_value )
615  {
616  if( f_value_type == k_string ) delete f_value.f_string;
617  f_value_type = k_uint;
618  f_value.f_uint = a_value;
619  return;
620  }
621 
622  inline void param_value::set( uint32_t a_value )
623  {
624  if( f_value_type == k_string ) delete f_value.f_string;
625  f_value_type = k_uint;
626  f_value.f_uint = a_value;
627  return;
628  }
629 
630  inline void param_value::set( uint64_t a_value )
631  {
632  if( f_value_type == k_string ) delete f_value.f_string;
633  f_value_type = k_uint;
634  f_value.f_uint = a_value;
635  return;
636  }
637 
638  inline void param_value::set( int8_t a_value )
639  {
640  if( f_value_type == k_string ) delete f_value.f_string;
641  f_value_type = k_int;
642  f_value.f_int = a_value;
643  return;
644  }
645 
646  inline void param_value::set( int16_t a_value )
647  {
648  if( f_value_type == k_string ) delete f_value.f_string;
649  f_value_type = k_int;
650  f_value.f_int = a_value;
651  return;
652  }
653 
654  inline void param_value::set( int32_t a_value )
655  {
656  if( f_value_type == k_string ) delete f_value.f_string;
657  f_value_type = k_int;
658  f_value.f_int = a_value;
659  return;
660  }
661 
662  inline void param_value::set( int64_t a_value )
663  {
664  if( f_value_type == k_string ) delete f_value.f_string;
665  f_value_type = k_int;
666  f_value.f_int = a_value;
667  return;
668  }
669 
670  inline void param_value::set( float a_value )
671  {
672  if( f_value_type == k_string ) delete f_value.f_string;
673  f_value_type = k_double;
674  f_value.f_double = a_value;
675  return;
676  }
677 
678  inline void param_value::set( double a_value )
679  {
680  if( f_value_type == k_string ) delete f_value.f_string;
681  f_value_type = k_double;
682  f_value.f_double = a_value;
683  return;
684  }
685 
686  inline void param_value::set( const std::string& a_value )
687  {
688  if( f_value_type == k_string ) delete f_value.f_string;
689  f_value_type = k_string;
690  f_value.f_string = new std::string( a_value );
691  return;
692  }
693 
694  inline void param_value::set( const char* a_value )
695  {
696  if( f_value_type == k_string ) delete f_value.f_string;
697  f_value_type = k_string;
698  f_value.f_string = new std::string( a_value );
699  return;
700  }
701 
702  inline std::string param_value::to_string() const
703  {
704  return as_string();
705  }
706 
707 
708 
709  //************************************
710  //*********** ARRAY ****************
711  //************************************
712 
713  template< typename XValType >
714  XValType param_array::get_value( unsigned a_index ) const
715  {
716  const param_value* value = value_at( a_index );
717  if( value == NULL ) throw error() << "No value is present at index <" << a_index << ">";
718  return value->get< XValType >();
719  }
720 
721  template< typename XValType >
722  XValType param_array::get_value( unsigned a_index, XValType a_default ) const
723  {
724  const param_value* value = value_at( a_index );
725  if( value == NULL ) return a_default;
726  return value->get< XValType >();
727  }
728 
729  inline param* param_array::clone() const
730  {
731  return new param_array( *this );
732  }
733 
734  inline bool param_array::is_null() const
735  {
736  return false;
737  }
738 
739  inline bool param_array::is_array() const
740  {
741  return true;
742  }
743 
744  inline unsigned param_array::size() const
745  {
746  return f_contents.size();
747  }
748  inline bool param_array::empty() const
749  {
750  return f_contents.empty();
751  }
752 
753  inline std::string param_array::get_value( unsigned a_index ) const
754  {
755  const param_value* value = value_at( a_index );
756  if( value == NULL ) throw scarab::error() << "No value at <" << a_index << "> is present at this node";
757  return value->to_string();
758  }
759 
760  inline std::string param_array::get_value( unsigned a_index, const std::string& a_default ) const
761  {
762  const param_value* value = value_at( a_index );
763  if( value == NULL ) return a_default;
764  return value->to_string();
765  }
766 
767  inline std::string param_array::get_value( unsigned a_index, const char* a_default ) const
768  {
769  return get_value( a_index, std::string( a_default ) );
770  }
771 
772  inline const param* param_array::at( unsigned a_index ) const
773  {
774  if( a_index >= f_contents.size() ) return NULL;
775  return f_contents[ a_index ];
776  }
777  inline param* param_array::at( unsigned a_index )
778  {
779  if( a_index >= f_contents.size() ) return NULL;
780  return f_contents[ a_index ];
781  }
782 
783  inline const param_value* param_array::value_at( unsigned a_index ) const
784  {
785  if( a_index >= f_contents.size() ) return NULL;
786  return &f_contents[ a_index ]->as_value();
787  }
788  inline param_value* param_array::value_at( unsigned a_index )
789  {
790  if( a_index >= f_contents.size() ) return NULL;
791  return &f_contents[ a_index ]->as_value();
792  }
793 
794  inline const param_array* param_array::array_at( unsigned a_index ) const
795  {
796  if( a_index >= f_contents.size() ) return NULL;
797  return &f_contents[ a_index ]->as_array();
798  }
799  inline param_array* param_array::array_at( unsigned a_index )
800  {
801  if( a_index >= f_contents.size() ) return NULL;
802  return &f_contents[ a_index ]->as_array();
803  }
804 
805  inline const param_node* param_array::node_at( unsigned a_index ) const
806  {
807  if( a_index >= f_contents.size() ) return NULL;
808  return &f_contents[ a_index ]->as_node();
809  }
810  inline param_node* param_array::node_at( unsigned a_index )
811  {
812  if( a_index >= f_contents.size() ) return NULL;
813  return &f_contents[ a_index ]->as_node();
814  }
815 
816  inline const param& param_array::operator[]( unsigned a_index ) const
817  {
818  return *f_contents[ a_index ];
819  }
820  inline param& param_array::operator[]( unsigned a_index )
821  {
822  return *f_contents[ a_index ];
823  }
824 
825  inline const param* param_array::front() const
826  {
827  return f_contents.front();
828  }
830  {
831  return f_contents.front();
832  }
833 
834  inline const param* param_array::back() const
835  {
836  return f_contents.back();
837  }
839  {
840  return f_contents.back();
841  }
842 
843  // assign a copy of a_value to the array at a_index
844  inline void param_array::assign( unsigned a_index, const param& a_value )
845  {
846  erase( a_index );
847  f_contents[ a_index ] = a_value.clone();
848  return;
849  }
850  // directly assign a_value_ptr to the array at a_index
851  inline void param_array::assign( unsigned a_index, param* a_value_ptr )
852  {
853  erase( a_index );
854  f_contents[ a_index ] = a_value_ptr;
855  return;
856  }
857 
858  inline void param_array::push_back( const param& a_value )
859  {
860  f_contents.push_back( a_value.clone() );
861  return;
862  }
863  inline void param_array::push_back( param* a_value_ptr )
864  {
865  f_contents.push_back( a_value_ptr );
866  return;
867  }
868 
869  inline void param_array::push_front( const param& a_value )
870  {
871  f_contents.push_front( a_value.clone() );
872  return;
873  }
874  inline void param_array::push_front( param* a_value_ptr )
875  {
876  f_contents.push_front( a_value_ptr );
877  return;
878  }
879 
880  inline void param_array::append( const param_array& an_array )
881  {
882  for( param_array::const_iterator it = an_array.begin(); it != an_array.end(); ++it )
883  {
884  push_back( *(*it) );
885  }
886  return;
887  }
888 
889  inline void param_array::erase( unsigned a_index )
890  {
891  delete f_contents[ a_index ];
892  f_contents[ a_index ] = NULL;
893  return;
894  }
895  inline param* param_array::remove( unsigned a_index )
896  {
897  param* t_current = f_contents[ a_index ];
898  f_contents[ a_index ] = NULL;
899  return t_current;
900  }
901  inline void param_array::clear()
902  {
903  for( unsigned ind = 0; ind < f_contents.size(); ++ind )
904  {
905  delete f_contents[ ind ];
906  }
907  f_contents.clear();
908  return;
909  }
910 
912  {
913  return f_contents.begin();
914  }
916  {
917  return f_contents.begin();
918  }
919 
921  {
922  return f_contents.end();
923  }
925  {
926  return f_contents.end();
927  }
928 
930  {
931  return f_contents.rbegin();
932  }
934  {
935  return f_contents.rbegin();
936  }
937 
939  {
940  return f_contents.rend();
941  }
943  {
944  return f_contents.rend();
945  }
946 
947 
948 
949  //************************************
950  //*********** NODE *****************
951  //************************************
952 
953 
954  template< typename XValType >
955  inline XValType param_node::get_value( const std::string& a_name ) const
956  {
957  const param_value* value = value_at( a_name );
958  if( value == NULL ) throw error() << "No value with name <" << a_name << "> is present at this node";
959  return value->get< XValType >();
960  }
961 
962  template< typename XValType >
963  inline XValType param_node::get_value( const std::string& a_name, XValType a_default ) const
964  {
965  const param_value* value = value_at( a_name );
966  if( value == NULL ) return a_default;
967  return value->get< XValType >();
968  }
969 
970  inline param* param_node::clone() const
971  {
972  //std::cout << "param_node::clone" << std::endl;
973  return new param_node( *this );
974  }
975 
976  inline bool param_node::is_null() const
977  {
978  return false;
979  }
980 
981  inline bool param_node::is_node() const
982  {
983  return true;
984  }
985 
986  inline unsigned param_node::size() const
987  {
988  return f_contents.size();
989  }
990  inline bool param_node::empty() const
991  {
992  return f_contents.empty();
993  }
994 
995 
996  inline bool param_node::has( const std::string& a_name ) const
997  {
998  return f_contents.count( a_name ) > 0;
999  }
1000 
1001  inline unsigned param_node::count( const std::string& a_name ) const
1002  {
1003  return f_contents.count( a_name );
1004  }
1005 
1006  inline std::string param_node::get_value( const std::string& a_name ) const
1007  {
1008  const param_value* value = value_at( a_name );
1009  if( value == NULL ) throw error() << "No value with name <" << a_name << "> is present at this node:\n" << *this;
1010  return value->to_string();
1011  }
1012 
1013  inline std::string param_node::get_value( const std::string& a_name, const std::string& a_default ) const
1014  {
1015  const param_value* value = value_at( a_name );
1016  if( value == NULL ) return a_default;
1017  return value->to_string();
1018  }
1019 
1020  inline std::string param_node::get_value( const std::string& a_name, const char* a_default ) const
1021  {
1022  return get_value( a_name, std::string( a_default ) );
1023  }
1024 
1025  inline const param* param_node::at( const std::string& a_name ) const
1026  {
1027  const_iterator it = f_contents.find( a_name );
1028  if( it == f_contents.end() )
1029  {
1030  return NULL;
1031  }
1032  return it->second;
1033  }
1034 
1035  inline param* param_node::at( const std::string& a_name )
1036  {
1037  iterator it = f_contents.find( a_name );
1038  if( it == f_contents.end() )
1039  {
1040  return NULL;
1041  }
1042  return it->second;
1043  }
1044 
1045  inline const param_value* param_node::value_at( const std::string& a_name ) const
1046  {
1047  const_iterator it = f_contents.find( a_name );
1048  if( it == f_contents.end() )
1049  {
1050  return NULL;
1051  }
1052  return &it->second->as_value();
1053  }
1054 
1055  inline param_value* param_node::value_at( const std::string& a_name )
1056  {
1057  iterator it = f_contents.find( a_name );
1058  if( it == f_contents.end() )
1059  {
1060  return NULL;
1061  }
1062  return &it->second->as_value();
1063  }
1064 
1065  inline const param_array* param_node::array_at( const std::string& a_name ) const
1066  {
1067  const_iterator it = f_contents.find( a_name );
1068  if( it == f_contents.end() )
1069  {
1070  return NULL;
1071  }
1072  return &it->second->as_array();
1073  }
1074 
1075  inline param_array* param_node::array_at( const std::string& a_name )
1076  {
1077  iterator it = f_contents.find( a_name );
1078  if( it == f_contents.end() )
1079  {
1080  return NULL;
1081  }
1082  return &it->second->as_array();
1083  }
1084 
1085  inline const param_node* param_node::node_at( const std::string& a_name ) const
1086  {
1087  const_iterator it = f_contents.find( a_name );
1088  if( it == f_contents.end() )
1089  {
1090  return NULL;
1091  }
1092  return &it->second->as_node();
1093  }
1094 
1095  inline param_node* param_node::node_at( const std::string& a_name )
1096  {
1097  iterator it = f_contents.find( a_name );
1098  if( it == f_contents.end() )
1099  {
1100  return NULL;
1101  }
1102  return &it->second->as_node();
1103  }
1104 
1105  inline const param& param_node::operator[]( const std::string& a_name ) const
1106  {
1107  const_iterator it = f_contents.find( a_name );
1108  if( it == f_contents.end() )
1109  {
1110  throw error() << "No value present corresponding to name <" << a_name << ">\n";
1111  }
1112  return *(it->second);
1113  }
1114 
1115  inline param& param_node::operator[]( const std::string& a_name )
1116  {
1117  return *f_contents[ a_name ];
1118  }
1119 
1120  inline bool param_node::add( const std::string& a_name, const param& a_value )
1121  {
1122  iterator it = f_contents.find( a_name );
1123  if( it == f_contents.end() )
1124  {
1125  f_contents.insert( contents_type( a_name, a_value.clone() ) );
1126  return true;
1127  }
1128  return false;
1129  }
1130 
1131  inline bool param_node::add( const std::string& a_name, param* a_value )
1132  {
1133  iterator it = f_contents.find( a_name );
1134  if( it == f_contents.end() )
1135  {
1136  f_contents.insert( contents_type( a_name, a_value ) );
1137  return true;
1138  }
1139  return false;
1140  }
1141 
1142  inline void param_node::replace( const std::string& a_name, const param& a_value )
1143  {
1144  erase( a_name );
1145  f_contents[ a_name ] = a_value.clone();
1146  return;
1147  }
1148 
1149  inline void param_node::replace( const std::string& a_name, param* a_value )
1150  {
1151  erase( a_name );
1152  f_contents[ a_name ] = a_value;
1153  return;
1154  }
1155 
1156  inline void param_node::erase( const std::string& a_name )
1157  {
1158  iterator it = f_contents.find( a_name );
1159  if( it != f_contents.end() )
1160  {
1161  delete it->second;
1162  f_contents.erase( it );
1163  }
1164  return;
1165  }
1166 
1167  inline param* param_node::remove( const std::string& a_name )
1168  {
1169  iterator it = f_contents.find( a_name );
1170  if( it != f_contents.end() )
1171  {
1172  param* removed = it->second;
1173  f_contents.erase( it );
1174  return removed;
1175  }
1176  return NULL;
1177  }
1178 
1179  inline void param_node::clear()
1180  {
1181  for( iterator it = f_contents.begin(); it != f_contents.end(); ++it )
1182  {
1183  delete it->second;
1184  }
1185  f_contents.clear();
1186  return;
1187  }
1188 
1190  {
1191  return f_contents.begin();
1192  }
1193 
1195  {
1196  return f_contents.begin();
1197  }
1198 
1200  {
1201  return f_contents.end();
1202  }
1203 
1205  {
1206  return f_contents.end();
1207  }
1208 
1209 
1210 
1211  SCARAB_API std::ostream& operator<<(std::ostream& out, const param& value);
1212  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_value& value);
1213  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_array& value);
1214  SCARAB_API std::ostream& operator<<(std::ostream& out, const param_node& value);
1215 
1216 
1217 } /* namespace scarab */
1218 
1219 #endif /* SCARAB_PARAM_HH_ */
virtual bool is_node() const
Definition: param.hh:420
const param * back() const
Definition: param.hh:834
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param.hh:1142
const param_node * node_at(unsigned a_index) const
Definition: param.hh:805
std::string * f_string
Definition: param.hh:157
fs::path path
Definition: path.hh:25
unsigned count(const std::string &a_name) const
Definition: param.hh:1001
void erase(const std::string &a_name)
Definition: param.hh:1156
virtual std::string to_string() const
Definition: param.hh:702
const param_node * node_at(const std::string &a_name) const
Definition: param.hh:1085
contents f_contents
Definition: param.hh:295
iterator begin()
Definition: param.hh:1189
void push_front(const param &a_value)
Definition: param.hh:869
virtual bool is_value() const
Definition: param.hh:410
std::deque< param * > contents
Definition: param.hh:177
#define SCARAB_API
Definition: scarab_api.hh:24
std::map< std::string, param * > contents
Definition: param.hh:303
virtual bool is_null() const
Definition: param.hh:976
virtual std::string to_string() const
Definition: param.hh:491
SCARAB_API std::ostream & operator<<(std::ostream &out, const param &a_value)
Definition: param.cc:540
virtual param * clone() const
Definition: param.hh:729
bool is_uint() const
Definition: param.hh:578
virtual bool is_array() const
Definition: param.hh:739
std::string type(const x_type &a_param)
Definition: typename.hh:22
static unsigned s_indent_level
Definition: param.hh:75
virtual param * clone() const
Definition: param.hh:970
contents::value_type contents_type
Definition: param.hh:306
std::string get_value(unsigned a_index) const
Definition: param.hh:753
bool is_bool() const
Definition: param.hh:573
std::string f_buffer
Definition: param.hh:170
const param_value * value_at(const std::string &a_name) const
Definition: param.hh:1045
virtual bool is_null() const
Definition: param.hh:734
unsigned size() const
Definition: param.hh:986
XValType get() const
Definition: param.hh:539
const param_value & operator()() const
Assumes that the parameter is a value, and returns a reference to itself.
Definition: param.hh:461
virtual bool is_value() const
Definition: param.hh:568
bool is_int() const
Definition: param.hh:583
virtual bool is_node() const
Definition: param.hh:981
std::string param_value::get< std::string >() const
Definition: param.hh:527
iterator end()
Definition: param.hh:920
const param_array * array_at(unsigned a_index) const
Definition: param.hh:794
contents::const_reverse_iterator const_reverse_iterator
Definition: param.hh:181
param_value & as_value()
Definition: param.hh:425
std::string get_value(const std::string &a_name) const
Definition: param.hh:1006
unsigned size() const
Definition: param.hh:744
bool has(const std::string &a_name) const
Definition: param.hh:996
contents::const_iterator const_iterator
Definition: param.hh:305
const param & operator[](unsigned a_index) const
Definition: param.hh:471
void append(const param_array &an_array)
Definition: param.hh:880
const param_value * value_at(unsigned a_index) const
Definition: param.hh:783
iterator end()
Definition: param.hh:1199
const param & operator[](const std::string &a_name) const
Definition: param.hh:1105
param * remove(unsigned a_index)
Definition: param.hh:895
iterator begin()
Definition: param.hh:911
const param * at(unsigned a_index) const
Definition: param.hh:772
bool empty() const
Definition: param.hh:748
virtual bool is_null() const
Definition: param.hh:405
const param * front() const
Definition: param.hh:825
param * remove(const std::string &a_name)
Definition: param.hh:1167
reverse_iterator rend()
Definition: param.hh:938
bool empty() const
Definition: param.hh:990
reverse_iterator rbegin()
Definition: param.hh:929
const param & operator[](unsigned a_index) const
Definition: param.hh:816
bool is_double() const
Definition: param.hh:588
const param * at(const std::string &a_name) const
Definition: param.hh:1025
scarab::path param_value::get< scarab::path >() const
Definition: param.hh:533
param_node & as_node()
Definition: param.hh:437
bool is_string() const
Definition: param.hh:593
void set(bool a_value)
Definition: param.hh:598
virtual bool is_null() const
Definition: param.hh:563
const param_array * array_at(const std::string &a_name) const
Definition: param.hh:1065
void erase(unsigned a_index)
Definition: param.hh:889
void assign(unsigned a_index, const param &a_value)
Definition: param.hh:844
contents::reverse_iterator reverse_iterator
Definition: param.hh:180
contents::iterator iterator
Definition: param.hh:304
param_array & as_array()
Definition: param.hh:431
virtual param * clone() const
Definition: param.hh:557
contents::const_iterator const_iterator
Definition: param.hh:179
contents f_contents
Definition: param.hh:395
void push_back(const param &a_value)
Definition: param.hh:858
bool add(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param.hh:1120
contents::value_type contents_type
Definition: param.hh:182
contents::iterator iterator
Definition: param.hh:178
virtual bool is_array() const
Definition: param.hh:415
virtual param * clone() const
Definition: param.hh:399