Server-Side C++ Mapping for Operations
Default Mapping for Operations
As we saw in the Server-Side C++ Mapping for Interfaces, for each operation on an interface, the generated skeleton class contains a pure virtual function with the same name.
For example, let’s take the generated code from the greeter example:
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}
The skeleton class generated from the Greeter interface, after removing extra details, is as follows:
namespace VisitorCenter
{
class Greeter : public virtual Ice::Object
{
public:
virtual std::string greet(std::string name,
const Ice::Current& current) = 0;
};
}
The greet function takes a string name and a Current, then returns a value of type std::string. This function should be implemented in your derived servant class with something like:
class Chatbot : public VisitorCenter::Greeter
{
public:
std::string greet(std::string name, const Ice::Current&) override
{
ostringstream os;
os << "Hello, " << name << "!";
return os.str();
}
};
AMD Mapping for Operations
Each operation with the ["amd"] metadata is mapped to a pure virtual function with an Async suffix in the skeleton class. The AMD mapping replaces the default “sync” mapping for the operation. See Asynchronous Method Dispatch (AMD) in C++ for details.
Throwing Exceptions
To throw an exception from an operation implementation, you simply construct this exception and throw it. For example:
void
MFile::write(Filesystem::Lines text, const Ice::Current&)
{
// Try to write the file contents here...
// Assume we are out of space...
if (error)
{
throw Filesystem::WriteException{"file too large"};
}
}
If you throw an arbitrary C++ exception (such as a std::logic_error), the Ice runtime catches the exception and then returns an UnknownException to the client.
If you throw an Ice runtime exception, such as MarshalException, the client receives an UnknownLocalException.
The server-side Ice runtime does not validate user exceptions thrown by an operation implementation to ensure they are compatible with the operation's Slice definition. Rather, Ice returns the user exception to the client, where the client-side runtime will validate the exception as usual and throws UnknownUserException for an unexpected exception type.