Modern C++ Visual Assist Script for Encapsulating object with fail safe mechanism

This is a small snippet for Visual Assist to encapsulate member objects and making sure that you can not use those objects unless they are instantiated or assigned. Also making sure that assignment operation is type safe.:

$end$$SymbolType$ $GeneratedPropertyName$() const {
if (this->$SymbolName$ == nullptr)
throw std::bad_function_call(“The object you are trying to use is empty. This functionality is not avaible. Please pass an object before usage.”);

return $SymbolName$;
}
template<typename T>
void $GeneratedPropertyName$(T &&value) {

static_assert(!is_same<objectPhysics, T>::value, Aurora::Errors::ErrorMessages::TypeMismatch.c_str());

$SymbolName$ = std::forward<T>(value); }

 

Or with assertion on both operations(for minimal overhead for release version):

$end$$SymbolType$ $GeneratedPropertyName$() const {
assert($SymbolName$ == nullptr && “The object you are trying to use is empty. This functionality is not available. Please pass an object before usage.”);

return $SymbolName$;
}
template<typename T>
void $GeneratedPropertyName$(T &&value) {
static_assert(!is_same<objectPhysics, T>::value, Aurora::Errors::ErrorMessages::TypeMismatch.c_str());

$SymbolName$ = std::forward<T>(value);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s