00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00046
#ifndef Option_INC
00047
#define Option_INC
00048
00049
#include "OptionBase.h"
00050
00051
namespace PLearn {
00052
using namespace std;
00053
00055
template<
class ObjectType,
class OptionType,
class ConstOptionType>
00056
class Option:
public OptionBase
00057 {
00058
protected:
00059 OptionType ObjectType::*ptr;
00060
00061
public:
00062
00065 Option(
const string& optionname, OptionType ObjectType::* member_ptr,
00066 flag_t flags,
const string& optiontype,
const string& defaultval,
00067
const string& description)
00068 :
OptionBase(optionname, flags, optiontype, defaultval, description),
00069 ptr(member_ptr) {}
00070 virtual void read(
Object* o,
PStream& in)
const
00071
{ in >> dynamic_cast<ObjectType*>(o)->*ptr; }
00072 virtual void read_and_discard(
PStream& in)
const
00073
{
00074
string dummy;
00075 in.
smartReadUntilNext(
";)", dummy);
00076 in.
unget();
00077
00078
00079 }
00080 virtual void write(
const Object* o,
PStream& out)
const
00081
{ out << static_cast<ConstOptionType>(dynamic_cast<const ObjectType *>(o)->*ptr); }
00082
00083 virtual Object* getAsObject(
Object* o)
const
00084
{
return toObjectPtr(dynamic_cast<ObjectType*>(o)->*ptr); }
00085
00086 virtual const Object* getAsObject(
const Object* o)
const
00087
{
return toObjectPtr(dynamic_cast<const ObjectType*>(o)->*ptr); }
00088
00089 virtual Object *getIndexedObject(
Object *o,
int i)
const
00090
{
return toIndexedObjectPtr(dynamic_cast<ObjectType*>(o)->*ptr, i); };
00091
00092 virtual const Object* getIndexedObject(
const Object *o,
int i)
const
00093
{
return toIndexedObjectPtr(dynamic_cast<const ObjectType*>(o)->*ptr, i); };
00094
00095 virtual string optionHolderClassName(
const Object* o)
const
00096
{
return dynamic_cast<const ObjectType*>(o)->ObjectType::classname(); }
00097 };
00098
00099
00104
00110
template<
class ObjectType,
class OptionType>
00111 inline void declareOption(
OptionList& ol,
00112
const string& optionname,
00113 OptionType ObjectType::* member_ptr,
00114
OptionBase::flag_t flags,
00115
const string& description,
00116
const string & defaultval=
"")
00117 { ol.push_back(
new Option<ObjectType, OptionType, const OptionType>(optionname, member_ptr, flags,
00118
TypeTraits<OptionType>::name(),
00119 defaultval, description)); }
00120
template<
class ObjectType,
class OptionType>
00121 inline void declareOption(
OptionList& ol,
const string& optionname, OptionType *ObjectType::* member_ptr, OptionBase::flag_t flags,
00122
const string& description,
const string & defaultval=
"")
00123 { ol.push_back(
new Option<ObjectType, OptionType *, const OptionType *>(optionname, member_ptr, flags,
TypeTraits<OptionType *>::name(),
00124 defaultval, description)); }
00125
00128
template<
class ObjectType,
class OptionType>
00129 inline void redeclareOption(
OptionList& ol,
00130
const string& optionname,
00131 OptionType ObjectType::* member_ptr,
00132 OptionBase::flag_t flags,
00133
const string& description,
00134
const string & defaultval=
"")
00135 {
00136
bool found =
false;
00137
for (OptionList::iterator it = ol.begin(); !found && it != ol.end(); it++) {
00138
if ((*it)->optionname() == optionname) {
00139
00140 found =
true;
00141 (*it) =
new Option<ObjectType, OptionType, const OptionType>
00142 (optionname, member_ptr, flags,
TypeTraits<OptionType>::name(), defaultval, description);
00143 }
00144 }
00145
if (!found) {
00146
00147
PLERROR(
"In Option::redeclareOption - The option you are trying to redeclare has not been declared yet.");
00148 }
00149 }
00150
00151
00152 }
00153
00154
#endif