Custom TypeConverter for Wrapper to String
Posted: 15 Aug 2024, 16:55
NOTE: A few posts down I show a working example of a TypeConverter, for those of you in the future looking for one. :)
Greetings there, I'm attempting to write a TypeConverter in C++ that will allow me to bind a particular wrapper into Strings. There's a bit of sanity there, I promise.
Below is a sanitized/simplified form of the wrapper. In essence, life is made much easier if we have a singular wrapper which, if given a LocalizationKey stores the localized text generated from that key, but also it has the ability to have text applied that isn't supposed to be localized (ex: a user typed in a name for something). The actual functionality of this wrapper is much more than this, but this is the pertinent aspects of the wrapper for this usecase. I should further clarify, while yes, I CAN just always bind through the Wrapper into OutputText, the design requirements from the rest of my team prohibit this. (In short they want to do {Binding Group.Name} and not {Binding Group.Name.OutputText}.)
Note 1: This part works fine inside of my own code, where I may well be trying to do something like "Noesis::String Tester = LoclizationWrapper("TestText");". The problem is that there are various automated bits within Noesis itself that basically says "The left side is type 'Noesis::String' and the right side is type 'LocalizationWrapper', I'm not going to try this without a TypeConverter. Binding failed.".
Below is the simplified example of the TypeConverter inside the header.
Note 2: This line fails to compile with a message of "Class 'Noesiss::TypeMetaData' may not have a template argument list.'.
When I expand out the macro, the error arises from the following line:
The compiler REALLY does not want "SelfClass" to exist there. But if I keep the line and remove that, then it seems happy (if theoretically wrong?). But then, with some further mucking about with the code in the .CPP file once I expand out the NS_IMPLEMENT_REFLECTION() macro, I can get the .h/.cpp file to compile in isolation, but when trying to compile the project as a whole, the complaint that I get is that "StaticFillClassType does not exist in global namespace", at the location in TypeClassCreator.inl where it's trying to execute the templated "void TypeClassCreator::Fill(Type* type)", specifically "ClassT::StaticFillClassType(helper);" line.
So any help that can be provided on this issue would be amazing!
As a further related topic, my understanding is that there's no actual way to declare within LocalizationWrapper that every instance of it ever created has the TypeConverter, but instead the requirement is that in other classes/wrappers that want to use it, the following is done:
Meaning, that if I have SomeWrapper, ExtraWrapper, MoreWrapper, and they all utilize the LocalizationWrapper, I need to remember to add that ".Meta<...>(...);" bit at the end each time. Is that correct?
Thanks again to any who respond!
Greetings there, I'm attempting to write a TypeConverter in C++ that will allow me to bind a particular wrapper into Strings. There's a bit of sanity there, I promise.
Below is a sanitized/simplified form of the wrapper. In essence, life is made much easier if we have a singular wrapper which, if given a LocalizationKey stores the localized text generated from that key, but also it has the ability to have text applied that isn't supposed to be localized (ex: a user typed in a name for something). The actual functionality of this wrapper is much more than this, but this is the pertinent aspects of the wrapper for this usecase. I should further clarify, while yes, I CAN just always bind through the Wrapper into OutputText, the design requirements from the rest of my team prohibit this. (In short they want to do {Binding Group.Name} and not {Binding Group.Name.OutputText}.)
Code: Select all
class LocalizationWrapper final : public CNotifyPropertyChangedBase
{
void SetLocalizationKey( SomeKey ) { OutputText = LocalizationMagic(SomeKey); }
void SetUnlocalizedText( SomeText ) { OutputText = SomeText; }
explicit operator Noesis::String() const { return _OutputText; } // See Note 1.
Noesis::String LocalizationKey;
Noesis::String OutputText;
}
Below is the simplified example of the TypeConverter inside the header.
Code: Select all
class LocalizationWrapperTypeConverter : public Noesis::TypeConverter
{
bool CanConvertFrom( const Noesis::Type* type ) const override;
bool CanConvertTo...
bool TryConvertFrom...
bool TryConvertFromString...
bool TryConvertTo...
bool TryConvertToString...
NS_DECLARE_REFLECTION( Noesis::TypeMetaData<Noesis::TypeConverterMetaData>( "LocalizationWrapperTypeConverter", Noesis::TypeConverter ) // See Note 2.
}
When I expand out the macro, the error arises from the following line:
Code: Select all
typedef Noesis::TypeMetaData<Noesis::TypeConverterMetaData>( "LocalizationWrapperTypeConverter" ) SelfClass;
So any help that can be provided on this issue would be amazing!
As a further related topic, my understanding is that there's no actual way to declare within LocalizationWrapper that every instance of it ever created has the TypeConverter, but instead the requirement is that in other classes/wrappers that want to use it, the following is done:
Code: Select all
void OtherWrapper::AdditionalReflection( Noesis::TypeClassCreator& Helper )
{
Helper.Prop( "TextVariable", &OtherWrapper::GetTextVariable ).Meta<Noesis::TypeConverterMetaData>( "LocalizationWrapperTypeConverter" );
}
Thanks again to any who respond!