1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
//osgIntrospection - Copyright (C) 2005 Marco Jez
#ifndef OSGINTROSPECTION_REFLECTOR_
#define OSGINTROSPECTION_REFLECTOR_
#include <osgIntrospection/Reflection>
#include <osgIntrospection/Type>
#include <osgIntrospection/Value>
#include <osgIntrospection/PropertyInfo>
#include <osgIntrospection/MethodInfo>
#include <osgIntrospection/ConstructorInfo>
#include <osgIntrospection/InstanceCreator>
#include <osgIntrospection/ReaderWriter>
#include <osgIntrospection/TypedConstructorInfo>
#include <osgIntrospection/Comparator>
#include <osgIntrospection/ConverterProxy>
#include <osgIntrospection/Converter>
#include <osgIntrospection/Utility>
#include <string>
#include <sstream>
#include <iterator>
namespace osgIntrospection
{
class CustomAttribute;
class ReaderWriter;
/// A Reflector is a proxy class that is used to create a new description
/// of a given type. If the type to be described is simple and doesn't
/// require additional details such as properties and methods, it can be
/// reflected by simply creating a global instance of one of the classes
/// derived from Reflector, for example ValueReflector. Other types may
/// need further information and therefore it could be necessary to create
/// a new subclass of Reflector or extend one of the existing subclasses.
/// The reflected type can be set by calling Reflector's protected
/// methods.
///
/// NOTE: when you create a Reflector for type T, it will automatically
/// create descriptions for types T*, const T*, T&, and const T&. You
/// should NEVER create reflectors for pointer or reference types
/// explicitly.
///
template<typename T>
class Reflector
{
public:
typedef T reflected_type;
typedef Reflector<T> inherited;
/// Virtual destructor.
virtual ~Reflector() {}
protected:
/// Direct initialization constructor. Parameter 'name' is the name
/// of the type being reflected and 'ns' is its namespace.
Reflector(const std::string& name, const std::string& ns, bool abstract = false);
/// Direct initialization constructor. Parameter 'qname' is the
/// fully-qualified name of the type being reflected, i.e. containing
/// both the namespace and the name (separated by "::").
Reflector(const std::string& qname, bool abstract = false);
protected:
/// Returns the Type object being described.
Type* getType() { return _type; }
/// Declares a new base type for the current type.
void addBaseType(const Type& type);
/// Sets the comparator object for the current type.
void setComparator(const Comparator* cmp);
/// Adds a property description to the current type.
PropertyInfo* addProperty(PropertyInfo* pi);
/// Adds a method description to the current type.
MethodInfo* addMethod(MethodInfo* mi);
/// Adds a protected method description to the current type.
MethodInfo* addProtectedMethod(MethodInfo* mi);
/// Adds an enumeration label to the current type.
void addEnumLabel(int v, const std::string& label, bool strip_namespace = true);
/// Adds a constructor description to the current type.
/// As soon as a constructor is added through this method,
/// the automatically-generated default constructor is
/// removed.
ConstructorInfo* addConstructor(ConstructorInfo* ci);
/// Adds a protected constructor description to the current
/// type.
ConstructorInfo* addProtectedConstructor(ConstructorInfo* ci);
/// Returns a string containing the qualified version of 'name'.
std::string qualifyName(const std::string& name) const;
/// Adds a custom attribute to the type being described.
CustomAttributeProvider *addAttribute(const CustomAttribute* attrib);
/// Sets the current type's ReaderWriter object.
void setReaderWriter(const ReaderWriter* rw);
/// Sets the current type's declaring file.
void setDeclaringFile(const std::string& file) const;
private:
struct PtrConstructor: ConstructorInfo
{
PtrConstructor(const Type* pt)
: ConstructorInfo(*pt, ParameterInfoList())
{
}
Value createInstance(ValueList& ) const { T* x = 0; return x; }
};
struct ConstPtrConstructor: ConstructorInfo
{
ConstPtrConstructor(const Type* pt)
: ConstructorInfo(*pt, ParameterInfoList())
{
}
Value createInstance(ValueList& ) const { const T *x = 0; return x; }
};
void init();
void init_reference_types();
void init_void_converter();
static std::string purify(const std::string& s);
static void split_qualified_name(const std::string& q, std::string& n, std::string& ns);
typedef std::vector<MethodInfo* > TempMethodList;
TempMethodList _temp_methods;
TempMethodList _temp_protected_methods;
Type* _type;
};
/// This reflector ought to be used to describe types that can be
/// created on the stack. Such types are for example int, double,
/// std::string, or other (possibly small) user-defined structs or
/// classes. The instance creator associated to types created through
/// this reflector will create Value objects whose internal type is T.
template<typename T>
struct ValueReflector: Reflector<T>
{
typedef ValueReflector<T> inherited;
typedef ValueInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
typedef ProtectedConstructorInstanceCreator<typename Reflector<T>::reflected_type> invalid_instance_creator_type;
ValueReflector(const std::string& name, const std::string& ns)
: Reflector<T>(name, ns, false)
{
}
ValueReflector(const std::string& qname)
: Reflector<T>(qname, false)
{
}
};
/// This reflector is to be used to describe abstract types that can't
/// be instantiated. For this reason a DummyInstanceCreator is used in
/// order to avoid compiler errors.
template<typename T>
struct AbstractObjectReflector: Reflector<T>
{
typedef AbstractObjectReflector<T> inherited;
typedef DummyInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
typedef ProtectedConstructorInstanceCreator<typename Reflector<T>::reflected_type> invalid_instance_creator_type;
AbstractObjectReflector(const std::string& name, const std::string& ns)
: Reflector<T>(name, ns, true)
{
}
AbstractObjectReflector(const std::string& qname)
: Reflector<T>(qname, true)
{
}
};
/// This reflector is to be used to describe types that ought to be
/// created on the heap. Such types are for example all classes derived
/// from osg::Referenced. The instance creator associated to types
/// created through this reflector will create Value objects whose
/// internal type is T*.
template<typename T>
struct ObjectReflector: Reflector<T>
{
typedef ObjectReflector<T> inherited;
typedef ObjectInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
typedef ProtectedConstructorInstanceCreator<typename Reflector<T>::reflected_type> invalid_instance_creator_type;
ObjectReflector(const std::string& name, const std::string& ns)
: Reflector<T>(name, ns, false)
{
}
ObjectReflector(const std::string& qname)
: Reflector<T>(qname, false)
{
}
};
/// This reflector is a ValueReflector that should be used to define
/// types that can be read and written from/to streams using the <<
/// and >> operators. A StdReaderWriter is assigned by default.
template<typename T>
struct AtomicValueReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
AtomicValueReflector(const std::string& name, const std::string& ns)
: ValueReflector<T>(name, ns)
{
setReaderWriter(new StdReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
AtomicValueReflector(const std::string& qname)
: ValueReflector<T>(qname)
{
setReaderWriter(new StdReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
};
template<typename T>
struct WAtomicValueReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
WAtomicValueReflector(const std::string& name, const std::string& ns)
: ValueReflector<T>(name, ns)
{
setReaderWriter(new StdWReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
WAtomicValueReflector(const std::string& qname)
: ValueReflector<T>(qname)
{
setReaderWriter(new StdWReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
};
/// This reflector is a ValueReflector that should be used to define
/// enumerations. It assigns an EnumReaderWriter by default.
template<typename T>
struct EnumReflector: ValueReflector<T>
{
typedef EnumReflector<T> inherited;
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
EnumReflector(const std::string& name, const std::string& ns)
: ValueReflector<T>(name, ns)
{
setReaderWriter(new EnumReaderWriter<T>);
setComparator(new TotalOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
EnumReflector(const std::string& qname)
: ValueReflector<T>(qname)
{
setReaderWriter(new EnumReaderWriter<T>);
setComparator(new TotalOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
};
/// This class allows to define the means for reflecting STL containers
/// such as std::deque and std::vector.
template<typename T, typename VT>
struct StdVectorReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value& instance, int i) const
{
return getInstance<T>(instance).at(i);
}
virtual Value get(const Value& instance, int i) const
{
return getInstance<T>(instance).at(i);
}
};
struct Setter: PropertySetter
{
virtual void set(Value& instance, int i, const Value& v) const
{
getInstance<T>(instance).at(i) = variant_cast<const typename T::value_type& >(v);
}
};
struct Counter: PropertyCounter
{
virtual int count(const Value& instance) const
{
return static_cast<int>(getInstance<T>(instance).size());
}
};
struct Adder: PropertyAdder
{
virtual void add(Value& instance, const Value& v) const
{
getInstance<T>(instance).push_back(variant_cast<const typename T::value_type& >(v));
}
};
struct Remover: PropertyRemover
{
virtual void remove(Value& instance, int i) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
struct Inserter: PropertyInserter
{
virtual void insert(Value& instance, int i, const Value& v) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.insert(j, variant_cast<const typename T::value_type& >(v));
}
};
StdVectorReflector(const std::string& name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo* pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Item", 0, 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
pi->addAttribute(new CustomPropertyInsertAttribute(new Inserter));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
/// This class allows to define the means for reflecting STL containers
/// such as std::set and std::multiset.
template<typename T, typename VT>
struct StdSetReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value& instance, int i) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
virtual Value get(const Value& instance, int i) const
{
const T& ctr = getInstance<T>(instance);
typename T::const_iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
};
struct Counter: PropertyCounter
{
virtual int count(const Value& instance) const
{
return static_cast<int>(getInstance<T>(instance).size());
}
};
struct Adder: PropertyAdder
{
virtual void add(Value& instance, const Value& v) const
{
getInstance<T>(instance).insert(variant_cast<const typename T::value_type& >(v));
}
};
struct Remover: PropertyRemover
{
virtual void remove(Value& instance, int i) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
StdSetReflector(const std::string& name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo* pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Item", 0, 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
/// This class allows to define the means for reflecting STL containers
/// that cannot be indexed directly, such as std::list.
template<typename T, typename VT>
struct StdListReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value& instance, int i) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
virtual Value get(const Value& instance, int i) const
{
const T& ctr = getInstance<T>(instance);
typename T::const_iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
};
struct Setter: PropertySetter
{
virtual void set(Value& instance, int i, const Value& v) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
*j = variant_cast<const typename T::value_type& >(v);
}
};
struct Counter: PropertyCounter
{
virtual int count(const Value& instance) const
{
return static_cast<int>(getInstance<T>(instance).size());
}
};
struct Adder: PropertyAdder
{
virtual void add(Value& instance, const Value& v) const
{
getInstance<T>(instance).push_back(variant_cast<const typename T::value_type& >(v));
}
};
struct Remover: PropertyRemover
{
virtual void remove(Value& instance, int i) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
struct Inserter: PropertyInserter
{
virtual void insert(Value& instance, int i, const Value& v) const
{
T& ctr = getInstance<T>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.insert(j, variant_cast<const typename T::value_type& >(v));
}
};
StdListReflector(const std::string& name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo* pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Item", 0, 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
pi->addAttribute(new CustomPropertyInsertAttribute(new Inserter));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
/// This class allows to define the means for reflecting STL associative
/// containers which hold pairs of key+value, such as std::map.
template<typename T, typename IT, typename VT>
struct StdMapReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
typedef typename T::iterator iterator;
typedef typename T::const_iterator const_iterator;
typedef typename T::key_type key_type;
typedef typename T::mapped_type mapped_type;
struct Getter: PropertyGetter
{
virtual Value get(Value& instance, const ValueList& indices) const
{
T& ctr = getInstance<T>(instance);
const key_type& key = variant_cast<const key_type& >(indices.front());
iterator i = ctr.find(key);
if (i == ctr.end()) return Value();
return i->second;
}
virtual Value get(const Value& instance, const ValueList& indices) const
{
const T& ctr = getInstance<T>(instance);
const key_type& key = variant_cast<const key_type& >(indices.front());
const_iterator i = ctr.find(key);
if (i == ctr.end()) return Value();
return i->second;
}
};
struct Setter: PropertySetter
{
virtual void set(Value& instance, const ValueList& indices, const Value& v) const
{
getInstance<T>(instance).insert(std::make_pair(variant_cast<const key_type& >(indices.front()),
variant_cast<const mapped_type& >(v)));
}
};
struct Indexer: IndexInfo
{
ParameterInfoList _params;
const Type& _itype;
Indexer()
: _itype(typeof(IT))
{
_params.push_back(new ParameterInfo("key", typeof(key_type), 0, ParameterInfo::IN));
}
virtual ~Indexer()
{
delete _params.front();
}
virtual const ParameterInfoList& getIndexParameters() const
{
return _params;
}
virtual void getIndexValueSet(int /*whichindex*/, const Value& instance, ValueList& values) const
{
const T& ctr = getInstance<T>(instance);
for (const_iterator i=ctr.begin();
i!=ctr.end();
++i)
{
values.push_back(Value(i->first).convertTo(_itype));
}
}
protected:
Indexer& operator = (const Indexer&) { return *this; }
};
struct Remover: PropertyRemover
{
virtual void remove(Value& instance, ValueList& values) const
{
getInstance<T>(instance).erase(getInstance<key_type>(values.front()));
}
};
StdMapReflector(const std::string& name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo* pi = new PropertyInfo(typeof(T), typeof(typename T::mapped_type), "Item", 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomIndexAttribute(new Indexer));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
if (typeid(VT).before(typeid(typename T::mapped_type)) ||
typeid(typename T::mapped_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
template<typename T, typename PT1, typename PT2>
struct StdPairReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Accessor: PropertyGetter, PropertySetter
{
Accessor(int i): _i(i) {}
virtual Value get(const Value& instance) const
{
switch (_i)
{
case 0: return getInstance<T>(instance).first;
case 1: return getInstance<T>(instance).second;
default: return Value();
}
}
virtual Value get(Value& instance) const
{
switch (_i)
{
case 0: return getInstance<T>(instance).first;
case 1: return getInstance<T>(instance).second;
default: return Value();
}
}
virtual void set(Value& instance, const Value& v) const
{
T& ctr = getInstance<T>(instance);
switch (_i)
{
case 0: ctr.first = variant_cast<const typename T::first_type& >(v); break;
case 1: ctr.second = variant_cast<const typename T::second_type& >(v); break;
}
}
int _i;
};
StdPairReflector(const std::string& name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo* pi1 = new PropertyInfo(typeof(T), typeof(typename T::first_type), "first", 0, 0);
pi1->addAttribute(new CustomPropertyGetAttribute(new Accessor(0)));
pi1->addAttribute(new CustomPropertySetAttribute(new Accessor(0)));
if (typeid(PT1).before(typeid(typename T::first_type)) ||
typeid(typename T::first_type).before(typeid(PT1)))
pi1->addAttribute(new PropertyTypeAttribute(typeof(PT1)));
this->addProperty(pi1);
PropertyInfo* pi2 = new PropertyInfo(typeof(T), typeof(typename T::second_type), "second", 0, 0);
pi2->addAttribute(new CustomPropertyGetAttribute(new Accessor(1)));
pi2->addAttribute(new CustomPropertySetAttribute(new Accessor(1)));
if (typeid(PT2).before(typeid(typename T::second_type)) ||
typeid(typename T::second_type).before(typeid(PT2)))
pi2->addAttribute(new PropertyTypeAttribute(typeof(PT2)));
this->addProperty(pi2);
}
};
// TEMPLATE METHODS
template<typename T>
Reflector<T>::Reflector(const std::string& name, const std::string& ns, bool abstract)
: _type(Reflection::getOrRegisterType(extended_typeid<T>(), true))
{
if (!_type->_name.empty())
_type->_aliases.push_back(ns.empty()? purify(name): purify(ns+"::"+name));
else
{
_type->_name = purify(name);
_type->_namespace = purify(ns);
}
_type->_is_abstract = abstract;
init();
}
template<typename T>
Reflector<T>::Reflector(const std::string& qname, bool abstract)
: _type(Reflection::getOrRegisterType(extended_typeid<T>(), true))
{
if (!_type->_name.empty())
_type->_aliases.push_back(purify(qname));
else
{
split_qualified_name(purify(qname), _type->_name, _type->_namespace);
}
_type->_is_abstract = abstract;
init();
}
template<typename T>
void Reflector<T>::init()
{
// pointer type
if (!_type->_pointed_type)
{
Type* ptype = Reflection::getOrRegisterType(extended_typeid<T*>(), true);
ptype->_name = _type->_name;
ptype->_namespace = _type->_namespace;
ptype->_pointed_type = _type;
ptype->_is_defined = true;
ptype->_cons.push_back(new PtrConstructor(ptype));
ptype->_rw = new PtrReaderWriter<T*>();
ptype->_cmp = new TotalOrderComparator<T*>();
}
// const pointer type
if (!_type->_pointed_type || !_type->_is_const)
{
Type* cptype = Reflection::getOrRegisterType(extended_typeid<const T*>(), true);
cptype->_name = _type->_name;
cptype->_namespace = _type->_namespace;
cptype->_is_const = true;
cptype->_pointed_type = _type;
cptype->_is_defined = true;
cptype->_cons.push_back(new ConstPtrConstructor(cptype));
cptype->_rw = new PtrReaderWriter<const T*>();
cptype->_cmp = new TotalOrderComparator<const T*>();
}
init_reference_types();
init_void_converter();
_type->_is_defined = true;
}
template<typename T>
void Reflector<T>::init_reference_types()
{
// reference type
if (!_type->_referenced_type)
{
Type* ptype = Reflection::getOrRegisterType(extended_typeid<T&>(), true);
ptype->_name = _type->_name;
ptype->_namespace = _type->_namespace;
ptype->_referenced_type = _type;
ptype->_is_defined = true;
}
// const reference type
if (!_type->_referenced_type || !_type->_is_const)
{
Type* cptype = Reflection::getOrRegisterType(extended_typeid<const T&>(), true);
cptype->_name = _type->_name;
cptype->_namespace = _type->_namespace;
cptype->_is_const = true;
cptype->_referenced_type = _type;
cptype->_is_defined = true;
}
}
template<>
void Reflector<void>::init_reference_types();
template<typename T>
void Reflector<T>::init_void_converter()
{
const osgIntrospection::Type& st = typeof(T*);
const osgIntrospection::Type& cst = typeof(const T*);
const osgIntrospection::Type& dt = typeof(void*);
const osgIntrospection::Type& cdt = typeof(const void*);
osgIntrospection::ConverterProxy cp1(st, dt, new osgIntrospection::ReinterpretConverter<T* , void*>);
osgIntrospection::ConverterProxy cp2(cst, cdt, new osgIntrospection::ReinterpretConverter<const T* , const void*>);
osgIntrospection::ConverterProxy cp1c(st, cdt, new osgIntrospection::ReinterpretConverter<T* , const void*>);
osgIntrospection::ConverterProxy cp3(dt, st, new osgIntrospection::ReinterpretConverter<void*, T* >);
osgIntrospection::ConverterProxy cp4(cdt, cst, new osgIntrospection::ReinterpretConverter<const void*, const T* >);
osgIntrospection::ConverterProxy cp3c(dt, cst, new osgIntrospection::ReinterpretConverter<void*, const T* >);
}
template<>
void Reflector<void>::init_void_converter();
template<typename T>
std::string Reflector<T>::purify(const std::string& s)
{
std::string r(s);
while (true)
{
std::string::size_type p = r.find(" COMMA ");
if (p == std::string::npos) break;
r.replace(p, 7, ", ");
};
return r;
}
template<typename T>
void Reflector<T>::split_qualified_name(const std::string& q, std::string& n, std::string& ns)
{
int templ = 0;
std::string::size_type split_point = std::string::npos;
std::string::size_type j = 0;
for (std::string::const_iterator i=q.begin(); i!=q.end(); ++i, ++j)
{
if (*i == '<') ++templ;
if (*i == '>') --templ;
if (templ == 0)
{
if (*i == ':' && (i+1)!=q.end() && *(i+1) == ':')
split_point = j;
}
}
if (split_point == std::string::npos)
{
ns.clear();
n = q;
}
else
{
n = q.substr(split_point+2);
ns = q.substr(0, split_point);
}
}
template<typename T>
void Reflector<T>::addBaseType(const Type& type)
{
_type->_base.push_back(&type);
}
template<typename T>
PropertyInfo* Reflector<T>::addProperty(PropertyInfo* pi)
{
_type->_props.push_back(pi);
return pi;
}
template<typename T>
MethodInfo* Reflector<T>::addMethod(MethodInfo* mi)
{
for (TempMethodList::iterator i=_temp_methods.begin(); i!=_temp_methods.end(); ++i)
{
if (mi->overrides(*i))
return *i;
}
_temp_methods.push_back(mi);
_type->_methods.push_back(mi);
return mi;
}
template<typename T>
MethodInfo* Reflector<T>::addProtectedMethod(MethodInfo* mi)
{
for (TempMethodList::iterator i=_temp_protected_methods.begin(); i!=_temp_protected_methods.end(); ++i)
{
if (mi->overrides(*i))
return *i;
}
_temp_protected_methods.push_back(mi);
_type->_protected_methods.push_back(mi);
return mi;
}
template<typename T>
void Reflector<T>::addEnumLabel(int v, const std::string& label, bool strip_namespace)
{
if (strip_namespace)
{
std::string::size_type p = label.rfind("::");
if (p != std::string::npos)
{
_type->_labels.insert(std::make_pair(v, label.substr(p+2)));
return;
}
}
_type->_labels.insert(std::make_pair(v, label));
}
template<typename T>
ConstructorInfo* Reflector<T>::addConstructor(ConstructorInfo* ci)
{
_type->_cons.push_back(ci);
return ci;
}
template<typename T>
ConstructorInfo* Reflector<T>::addProtectedConstructor(ConstructorInfo* ci)
{
_type->_protected_cons.push_back(ci);
return ci;
}
template<typename T>
std::string Reflector<T>::qualifyName(const std::string& name) const
{
std::string s;
if (!_type->_namespace.empty())
{
s.append(_type->_namespace);
s.append("::");
}
if (!_type->_name.empty())
{
s.append(_type->_name);
s.append("::");
}
s.append(name);
return s;
}
template<typename T>
CustomAttributeProvider *Reflector<T>::addAttribute(const CustomAttribute* attrib)
{
return _type->addAttribute(attrib);
}
template<typename T>
void Reflector<T>::setReaderWriter(const ReaderWriter* rw)
{
_type->_rw = rw;
}
template<typename T>
void Reflector<T>::setComparator(const Comparator* cmp)
{
_type->_cmp = cmp;
}
template<typename T>
void Reflector<T>::setDeclaringFile(const std::string& file) const
{
_type->_declaringFile = file;
}
}
#endif
|