28 #ifndef util_NamedValue_hpp
29 #define util_NamedValue_hpp
37 #include <util/TypeName.hpp>
47 std::istream & operator >> ( std::istream & s , NamedValueSet & v );
48 std::ostream & operator << ( std::ostream & s ,
const NamedValueSet & v );
58 const std::vector< NamedValue<void> * >
get()
const {
return m_members ; }
61 find(
const std::string & s ,
const char sep =
'.' )
const ;
63 NamedValue<void> * insert( NamedValue<void> * );
65 void remove( NamedValue<void> * );
71 NamedValueSet(
const NamedValueSet & );
72 NamedValueSet & operator = (
const NamedValueSet & );
73 std::vector< NamedValue<void> * > m_members ;
83 const std::string name ;
84 const std::type_info & type ;
88 virtual unsigned get_max()
const = 0 ;
89 virtual unsigned put_max()
const = 0 ;
91 virtual void * put_void(
unsigned ) = 0 ;
92 virtual const void * get_void(
unsigned )
const = 0 ;
94 virtual void tell( std::ostream & )
const = 0 ;
95 virtual void write( std::ostream & )
const = 0 ;
96 virtual unsigned read( std::istream & ) = 0 ;
103 const T &
get(
unsigned i = 0 )
const
105 const std::type_info & t =
typeid(T);
106 const void *
const p = get_void(i);
107 if ( t != type || NULL == p ) { get_throw(t,i); }
108 return * ( (
const T *) p );
112 T & put(
unsigned i = 0 )
114 const std::type_info & t =
typeid(T);
115 void *
const p = put_void(i);
116 if ( t != type || NULL == p ) { put_throw(t,i); }
117 return * ( (T *) p );
123 virtual unsigned pack(
void * )
const = 0 ;
126 virtual unsigned unpack(
void * ) = 0 ;
130 const std::vector< NamedValueSet * > references()
const ;
134 NamedValue(
const std::string & n ,
const std::type_info & t )
135 : name(n), type(t) {}
139 friend class NamedValueSet ;
141 std::vector< NamedValueSet * > m_holders ;
143 void get_throw(
const std::type_info & ,
unsigned )
const ;
144 void put_throw(
const std::type_info & ,
unsigned )
const ;
155 template<
typename T >
156 unsigned read_array( std::istream & s , T *
const v ,
const unsigned n )
159 while ( i < n && ( s >> v[i] ) ) { ++i ; }
160 s.clear( s.rdstate() & ~std::ios::failbit );
164 template<
typename T >
165 unsigned read_vector( std::istream & s , std::vector<T> & v )
167 const unsigned n = v.size();
169 for ( T tmp ; s >> tmp ; ++i ) {
170 if ( i < n ) { v[i] = tmp ; }
171 else { v.push_back( tmp ); }
173 s.clear( s.rdstate() & ~std::ios::failbit );
177 template<
typename T >
178 void write_array( std::ostream & s ,
const T *
const v ,
const unsigned n )
180 for (
unsigned i = 0 ; i < n ; ++i ) {
181 if ( i ) { s <<
" " ; }
186 template<
typename T >
187 unsigned pack_array(
void * b ,
const T *
const p ,
unsigned n )
189 n = ((
const unsigned char *)(p+n)) - ((
const unsigned char *)(p));
190 if ( b ) { memcpy( b , p , n ); }
194 template<
typename T >
195 unsigned unpack_array(
const void * b , T *
const p ,
unsigned n )
198 n = ((
unsigned char *)(p+n)) - ((
unsigned char *)(p));
207 template<
typename T >
208 unsigned pack_vector(
void * b ,
const std::vector<T> & v )
210 const unsigned n = v.size();
212 memcpy( b , & n ,
sizeof(
unsigned) );
213 b = ((
unsigned char *)b) +
sizeof(unsigned);
215 return sizeof(unsigned) + pack_array<T>( b , & v[0] , n );
218 template<
typename T >
219 unsigned unpack_vector(
void * b , std::vector<T> & v )
223 memcpy( & n , b ,
sizeof(
unsigned) );
224 b = ((
unsigned char *)b) +
sizeof(unsigned);
225 if ( v.size() < n ) { v.resize(n); }
226 n =
sizeof(unsigned) + unpack_array<T>( b , & v[0] , n );
231 unsigned pack_value(
void * b ,
const std::string & s )
233 const unsigned n = s.size() + 1 ;
234 if ( b ) { memcpy( b , s.c_str() , n ); }
238 unsigned unpack_value(
void * b , std::string & s )
241 if ( b ) { s.assign( (
char *) b ); n = s.size() + 1 ; }
245 template<
typename T >
246 unsigned pack_value(
void * b ,
const T & v )
248 const unsigned n =
sizeof(T);
249 if ( b ) { memcpy( b , & v , n ); }
253 template<
typename T >
254 unsigned unpack_value(
void * b , T & v )
257 if ( b ) { memcpy( & v , b , n =
sizeof(T) ); }
267 template<
typename T >
268 class NamedValue :
public NamedValue<void> {
277 NamedValue(
const std::string & n ,
const T & v )
278 :
NamedValue<void>( n , typeid(T) ), value(v) {}
281 void tell( std::ostream & s )
const
282 { s << name <<
" = " << TypeName<T>::value(); }
285 void write( std::ostream & s )
const { s << value ; }
288 unsigned read( std::istream & s ) {
return s >> value ? 1 : 0 ; }
290 unsigned pack(
void * b )
const {
return pack_value( b , value ); }
291 unsigned unpack(
void * b ) {
return unpack_value( b , value ); }
295 unsigned get_max()
const {
return 1 ; }
296 unsigned put_max()
const {
return 1 ; }
297 void * put_void(
unsigned i ) {
return i ? NULL : & value ; }
298 const void * get_void(
unsigned i )
const {
return i ? NULL : & value ; }
302 NamedValue & operator = (
const NamedValue & );
309 template<
typename T >
319 :
NamedValue<void>( n , typeid(T) ), ref(v) {}
322 void tell( std::ostream & s )
const
323 { s << name <<
" = " << TypeName<T &>::value(); }
326 void write( std::ostream & s )
const { s << ref ; }
329 unsigned read( std::istream & s ) {
return s >> ref ? 1 : 0 ; }
331 unsigned pack(
void * b )
const {
return pack_value( b , ref ); }
332 unsigned unpack(
void * b ) {
return unpack_value( b , ref ); }
336 unsigned get_max()
const {
return 1 ; }
337 unsigned put_max()
const {
return 1 ; }
338 void * put_void(
unsigned i ) {
return i ? NULL : & ref ; }
339 const void * get_void(
unsigned i )
const {
return i ? NULL : & ref ; }
342 NamedValue(
const NamedValue & );
343 NamedValue & operator = (
const NamedValue & );
350 template<
typename T >
355 NamedValue(
const std::string & n ,
const T & v )
360 void write( std::ostream & s )
const { s << ref ; }
362 unsigned read( std::istream & ) {
return 0 ; }
364 void tell( std::ostream & s )
const
365 { s << name <<
" = " << TypeName<const T &>::value(); }
367 unsigned pack(
void * b )
const {
return pack_value( b , ref ); }
369 unsigned unpack(
void * b ) {
return 0 ; }
373 unsigned get_max()
const {
return 1 ; }
374 unsigned put_max()
const {
return 0 ; }
375 void * put_void(
unsigned i ) {
return NULL ; }
376 const void * get_void(
unsigned i )
const {
return i ? NULL : & ref ; }
379 NamedValue(
const NamedValue & );
380 NamedValue & operator = (
const NamedValue & );
388 template<
typename T ,
unsigned N >
397 void write( std::ostream & s )
const { write_array<T>(s,value,N); }
399 unsigned read( std::istream & s ) {
return read_array<T>(s,value,N); }
401 void tell( std::ostream & s )
const
402 { s << name <<
" = " << TypeName<T[N]>::value(); }
404 unsigned pack(
void * b )
const {
return pack_array<T>( b , value, N); }
405 unsigned unpack(
void * b ) {
return unpack_array<T>(b, value, N); }
409 unsigned get_max()
const {
return N ; }
410 unsigned put_max()
const {
return N ; }
411 void * put_void(
unsigned i ) {
return i ? NULL : value + i ; }
412 const void * get_void(
unsigned i )
const {
return i ? NULL : value + i ; }
416 NamedValue & operator = (
const NamedValue & );
423 template<
typename T >
427 const unsigned size ;
429 NamedValue(
const std::string & n , T * v ,
unsigned s )
434 void write( std::ostream & s )
const { write_array<T>(s,ref,size); }
436 unsigned read( std::istream & s )
437 {
return read_array<T>(s,ref,size); }
439 void tell( std::ostream & s )
const
440 { s << name <<
" = " << type_name_array<T>(size) <<
" *" ; }
442 unsigned pack(
void * b )
const {
return pack_array<T>( b,ref,size); }
443 unsigned unpack(
void * b ) {
return unpack_array<T>(b,ref,size); }
447 unsigned get_max()
const {
return size ; }
448 unsigned put_max()
const {
return size ; }
449 void * put_void(
unsigned i) {
return i < size ? ref+i : NULL;}
450 const void * get_void(
unsigned i)
const {
return i < size ? ref+i : NULL;}
453 NamedValue(
const NamedValue & );
454 NamedValue & operator = (
const NamedValue & );
461 template<
typename T >
464 const T *
const ref ;
465 const unsigned size ;
467 NamedValue(
const std::string & n ,
const T * v ,
unsigned s )
472 void write( std::ostream & s )
const { write_array<T>(s,ref,size); }
473 unsigned read( std::istream & ){
return 0 ; }
475 void tell( std::ostream & s )
const
476 { s << name <<
" = " << type_name_array<const T>(size) <<
" *" ; }
478 unsigned pack(
void * b )
const {
return pack_array<T>(b,ref,size); }
479 unsigned unpack(
void * b ) {
return 0 ; }
483 unsigned get_max()
const {
return size ; }
484 unsigned put_max()
const {
return 0 ; }
485 void * put_void(
unsigned ) {
return NULL ; }
486 const void * get_void(
unsigned i)
const {
return i < size ? ref+i : NULL;}
489 NamedValue(
const NamedValue & );
490 NamedValue & operator = (
const NamedValue & );
497 template<
typename T >
500 std::vector<T> value ;
504 NamedValue(
const std::string & n ,
const std::vector<T> & v )
509 void tell( std::ostream & s )
const
510 { s << name <<
" = " << type_name_vector<T>( value.size() ); }
512 void write( std::ostream & s )
const
513 { write_array<T>( s , & value[0] , value.size() ); }
515 unsigned read( std::istream & s )
516 {
return read_vector<T>( s , value ); }
518 unsigned pack(
void * b )
const {
return pack_vector<T>( b , value ); }
520 unsigned unpack(
void * b ) {
return unpack_vector<T>( b , value ); }
524 unsigned get_max()
const {
return value.size(); }
525 unsigned put_max()
const {
return std::numeric_limits<unsigned>::max(); }
527 void * put_void(
unsigned i )
529 if ( value.size() <= i ) { value.resize(i+1); }
533 const void * get_void(
unsigned i )
const
534 {
return i < value.size() ? & value[i] : NULL ; }
538 NamedValue & operator = (
const NamedValue & );
541 template<
typename T >
542 class NamedValue< std::vector<T> & > :
public NamedValue<void> {
544 std::vector<T> & ref ;
546 NamedValue(
const std::string & n , std::vector<T> & v )
547 :
NamedValue<void>( n , typeid(T) ), ref(v) {}
551 void tell( std::ostream & s )
const
552 { s << name <<
" = " << type_name_vector<T>( ref.size() ) <<
" &" ; }
554 void write( std::ostream & s )
const
555 { write_array<T>( s , & ref[0] , ref.size() ); }
557 unsigned read( std::istream & s )
558 {
return read_vector<T>( s , ref ); }
560 unsigned pack(
void * b )
const {
return pack_vector<T>( b , ref ); }
562 unsigned unpack(
void * b ) {
return unpack_vector<T>( b , ref ); }
566 unsigned get_max()
const {
return ref.size(); }
567 unsigned put_max()
const {
return std::numeric_limits<unsigned>::max(); }
569 void * put_void(
unsigned i )
571 if ( ref.size() <= i ) { ref.resize(i+1); }
575 const void * get_void(
unsigned i )
const
576 {
return i < ref.size() ? & ref[i] : NULL ; }
580 NamedValue & operator = (
const NamedValue & );
587 template<
typename T >
590 const std::vector<T> & ref ;
592 explicit NamedValue(
const std::string & n ,
const std::vector<T> & arg )
597 void tell( std::ostream & s )
const
598 { s << name <<
" = " << type_name_vector<const T>( ref.size() ) <<
" &" ; }
600 void write( std::ostream & s )
const
601 { write_array<T>( s , & ref[0] , ref.size() ); }
603 unsigned read( std::istream & ) {
return 0 ; }
605 unsigned pack(
void * b )
const {
return pack_vector<T>( b , ref ); }
607 unsigned unpack(
void * b ) {
return 0 ; }
611 unsigned get_max()
const {
return ref.size(); }
612 unsigned put_max()
const {
return 0 ; }
614 void * put_void(
unsigned ) {
return NULL ; }
616 const void * get_void(
unsigned i )
const
617 {
return i < ref.size() ? & ref[i] : NULL ; }
621 NamedValue & operator = (
const NamedValue & );
628 class NamedValue< NamedValueSet > :
public NamedValue<void> {
630 NamedValueSet value ;
635 :
NamedValue<void>( n , typeid(NamedValueSet) ) {}
637 void tell( std::ostream & )
const ;
639 void write( std::ostream & s )
const { s << value ; }
640 unsigned read( std::istream & s ) {
return s >> value ? 1 : 0 ; }
645 unsigned pack(
void * )
const ;
648 unsigned unpack(
void * );
652 unsigned get_max()
const {
return 1 ; }
653 unsigned put_max()
const {
return 1 ; }
654 void * put_void(
unsigned i ) {
return i ? NULL : & value ; }
655 const void * get_void(
unsigned i )
const {
return i ? NULL : & value ; }
660 NamedValue & operator = (
const NamedValue & );
unsigned pack(void *b) const
void write(std::ostream &s) const
Write value to stream.
unsigned pack(void *b) const
void tell(std::ostream &s) const
Tell the type to a stream.
unsigned pack(void *b) const
unsigned pack(void *b) const
unsigned read(std::istream &s)
Read value from stream.
NamedValue(const std::string &n)
Constructor.
unsigned read(std::istream &s)
Read value from stream.
unsigned pack(void *b) const
NamedValue to an ordinary value.
void tell(std::ostream &s) const
Tell the type to a stream.
void write(std::ostream &s) const
Write value to stream.
unsigned pack(void *b) const
NamedValue(const std::string &n, T &v)
Constructor.
Base class for references provides access to anonymous type.
unsigned pack(void *b) const