How To Use
 All Modules Pages
C++ code: Definition of base class SimulatedObjectBase
// ----- SimulatedObjectBase - Base class for simulated objects (fake objects) -----
/// \brief This base class supports some features useful for simulated objects:
///
/// - setting/unsetting/checking of options
/// - tracing
/// - generating sync events
/// - passing of values to specific method implementations
///
/// Remarks:
/// The derived class may have const methods which nevertheless
/// want to make use of base class functionality. To avoid casting to non const
/// this pointer, base class methods are set const, members are set mutable.
/// Base class for simulated objects (fake objects)
class SimulatedObjectBase
{
public:
/// Name of the (derived) class the object belongs to.
std::string m_className;
public:
/// Constructor. Passing TTB_CLASS_NAME as second param will automatically set the name of the derived class.
explicit SimulatedObjectBase(std::string const & in_objectName, std::string const & in_className = "SimulatedObjectBase");
virtual ~SimulatedObjectBase();
virtual void SetObjectName (std::string const& in_objectName)
{
m_objectName = in_objectName;
}
const std::string& GetClassName() const {return m_className;}
const std::string& GetObjectName() const {return m_objectName;}
const std::string& GetTraceName() const {return m_objectName;}
virtual void _Dump(EventContext in_context = CTX_INFO) const;
// ----- Setting and checking options and values -----
void _SetOption (
std::string const & in_option,
std::string const & in_scope = Option::ALL,
OptionType::Enum in_optionType = OptionType::PERMANENTLY_ACTIVE,
int in_numRemainingCalls = 0) const;
void _SetOptionValue(
std::string const & in_name,
std::string const & in_value,
std::string const & in_scope = Option::ALL) const;
void _ResetOption(
std::string const & in_option,
std::string const & in_scope = Option::ALL) const;
void _ResetOptions (void) const;
bool _IsOptionSet (
std::string const & in_option,
std::string const & in_scope = Option::ALL) const;
bool _WasOptionSet(
std::string const & in_option,
std::string const & in_scope = Option::ALL) const;
/// Get the given option in string format
std::string _GetOptionValueStr(std::string const & in_name, std::string const & in_scope = Option::ALL) const;
/// Get the given option in numeric format (type must be streamable)
template<class T> T _GetOptionValue(const std::string& in_nameOption, std::string const & in_scope = Option::ALL) const
{
std::string optionValAsString = _GetOptionValueStr(in_nameOption, in_scope);
std::istringstream iss(optionValAsString);
T result = T();
if ((iss >> result).fail())
{
result = T(); // reset to default value
std::ostringstream oss;
oss << "Could not convert option value >" << in_nameOption
<< "< with value >" << optionValAsString << "<"
<< " to type >" << typeid(T).name() << "<";
TRCF("SimulatedObjectBase::_GetOptionValue");
TRC(TL_ERROR, "WARNING: %s", oss.str().c_str());
}
return result;
}
bool _IsSilent (std::string const & in_scope = Option::ALL) const;
bool _IsError(std::string const & in_scope = Option::ALL) const;
bool _WasError(std::string const & in_scope = Option::ALL) const;
Options& _GetOptions(void) const;
/// Continue the execution of a method call which has been blocked
void _ContinueExecution (std::string const & in_methodName) const;
// General purpose method to trigger something. Base implementation does nothing.
virtual void _Trigger(int in_hint = 0) const;
// Derived classes may use this call e.g. to recheck all set options and
// prepare some internal data used for inter working within test.
// The call may also cause a direct test specific interaction
// with the test environment or the test object.
protected:
/// Generic method execution with support for writing to TestEvents, generating sync events and blocking.
void _MethodCall(std::string const & in_methodName, std::string const & in_callInfo = "", StringFunc in_func = StringFuncEmpty) const;
/// Same as _MethodCall with additional support for returning an (error) return value of any type.
template <typename RT> RT _MethodCallReturn(std::string const & in_methodName, std::string const & in_callInfo = "", StringFunc in_func = StringFuncEmpty) const
{
typename RT retVal = RT();
// in_methodName may come from __FUNCTION__ macro and may therefore
// contain unwanted additional info about namespace and class name
std::string methodName = ExtractFunctionName(in_methodName);
_MethodCall(methodName,
// Log input params
in_callInfo,
// Delayed processing of output/return values
([&](void) -> std::string
{
bool simulateFailure = _IsError(methodName);
// Call user supplied function to generate return value.
// Context info about error flag, method name and called object
// is passed to the function.
std::string info = TestToolBox::MakeReturnValue(retVal,
simulateFailure, methodName, this);
if (info.empty() && simulateFailure)
{
info = "return error";
}
// Call user defined postprocessing functor
std::string infoFromFuncCall = in_func();
if (!infoFromFuncCall.empty() && !info.empty())
{
infoFromFuncCall += " ";
}
// info for output
return infoFromFuncCall + info;
}));
return retVal;
};
/// Assumptions:
/// - return type is default constructable (signalling "no error")
/// - there exists a function overload TestToolBox::MakeReturnValue(..)
/// for the specific return type
void _EnsureWaitableConditionExists(std::string const & methodName) const;
Option* _FindOption(std::string const & in_option, std::string const & in_scope) const;
private:
/// Object name used to identify an instance of simulated object
std::string m_objectName;
/// Object specific set of options
Options mutable m_options;
/// map with conditions to wait for; used for realizing blocking method calls which will return when they receive an explicit trigger from test environment
WaitableConditions mutable m_waitableConditions;
boost::mutex mutable m_mutex;
};