本文整理汇总了C++中Method::isConst方法的典型用法代码示例。如果您正苦于以下问题:C++ Method::isConst方法的具体用法?C++ Method::isConst怎么用?C++ Method::isConst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Method
的用法示例。
在下文中一共展示了Method::isConst方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testMethodSearch
void ClassTestSuite::testMethodSearch()
{
Class test = Class::lookup("ClassTest::Test1");
auto retDouble = test.findAllMethods([&](const Method& m) {
return m.returnSpelling() == "double";
});
TS_ASSERT_EQUALS(retDouble.size(), 3);
auto retDoubleStrict = test.findAllMethods([&](const Method& m) {
return m.returnSpelling() == "double" && m.getClass() == test; //filter out inherited methods
});
TS_ASSERT_EQUALS(retDoubleStrict.size(), 2);
auto constMethods = test.findAllMethods([&](const Method& m) {
return m.isConst();
});
TS_ASSERT_EQUALS(constMethods.size(), 2);
Method m = test.findMethod([&](const Method& m) {
return m.name() == "method1";
});
TS_ASSERT(m.isValid());
TS_ASSERT(m.isConst());
TS_ASSERT_EQUALS(m.returnSpelling(), "std::string");
Method m30 = test.findMethod([&](const Method& m) {
return m.name() == "method30";
});
TS_ASSERT(!m30.isValid());
TS_ASSERT_THROWS_ANYTHING(m30.isConst());
}
示例2: generateMethod
void SmokeClassFiles::generateMethod(QTextStream& out, const QString& className, const QString& smokeClassName,
const Method& meth, int index, QSet<QString>& includes)
{
out << " ";
if ((meth.flags() & Method::Static) || meth.isConstructor())
out << "static ";
out << QString("void x_%1(Smoke::Stack x) {\n").arg(index);
out << " // " << meth.toString() << "\n";
out << " ";
if (meth.isConstructor()) {
out << smokeClassName << "* xret = new " << smokeClassName << "(";
} else {
const Function* func = Util::globalFunctionMap[&meth];
if (func)
includes.insert(func->fileName());
if (meth.type()->getClass())
includes.insert(meth.type()->getClass()->fileName());
if (meth.type()->isFunctionPointer() || meth.type()->isArray())
out << meth.type()->toString("xret") << " = ";
else if (meth.type() != Type::Void)
out << meth.type()->toString() << " xret = ";
if (!(meth.flags() & Method::Static)) {
if (meth.isConst()) {
out << "((const " << smokeClassName << "*)this)->";
} else {
out << "this->";
}
}
if (!(meth.flags() & Method::PureVirtual) && !func) {
// dynamic dispatch for virtuals
out << className << "::";
} else if (func) {
if (!func->nameSpace().isEmpty())
out << func->nameSpace() << "::";
}
out << meth.name() << "(";
}
for (int j = 0; j < meth.parameters().count(); j++) {
const Parameter& param = meth.parameters()[j];
if (param.type()->getClass())
includes.insert(param.type()->getClass()->fileName());
if (j > 0) out << ",";
QString field = Util::stackItemField(param.type());
QString typeName = param.type()->toString();
if (param.type()->isArray()) {
Type t = *param.type();
t.setPointerDepth(t.pointerDepth() + 1);
t.setIsRef(false);
typeName = t.toString();
out << '*';
} else if (field == "s_class" && (param.type()->pointerDepth() == 0 || param.type()->isRef()) && !param.type()->isFunctionPointer()) {
// references and classes are passed in s_class
typeName.append('*');
out << '*';
}
// casting to a reference doesn't make sense in this case
if (param.type()->isRef() && !param.type()->isFunctionPointer()) typeName.replace('&', "");
out << "(" << typeName << ")" << "x[" << j + 1 << "]." << field;
}
// if the method has any other default parameters, append them here as values, so
if (!meth.remainingDefaultValues().isEmpty()) {
const QStringList& defaultParams = meth.remainingDefaultValues();
if (meth.parameters().count() > 0)
out << "," ;
out << defaultParams.join(",");
}
out << ");\n";
if (meth.type() != Type::Void) {
out << " x[0]." << Util::stackItemField(meth.type()) << " = " << Util::assignmentString(meth.type(), "xret") << ";\n";
} else {
out << " (void)x; // noop (for compiler warning)\n";
}
out << " }\n";
// If the constructor was generated from another one with default parameteres, we don't need to explicitly create
// it here again. The x_* call will append the default parameters at the end and thus choose the right constructor.
if (meth.isConstructor() && meth.remainingDefaultValues().isEmpty()) {
out << " explicit " << smokeClassName << '(';
QStringList x_list;
for (int i = 0; i < meth.parameters().count(); i++) {
if (i > 0) out << ", ";
out << meth.parameters()[i].type()->toString() << " x" << QString::number(i + 1);
x_list << "x" + QString::number(i + 1);
}
out << ") : " << meth.getClass()->name() << '(' << x_list.join(", ") << ") {}\n";
}
}
示例3: generateVirtualMethod
void SmokeClassFiles::generateVirtualMethod(QTextStream& out, const Method& meth, QSet<QString>& includes)
{
QString x_params, x_list;
QString type = meth.type()->toString();
if (meth.type()->getClass())
includes.insert(meth.type()->getClass()->fileName());
out << " virtual " << type << " " << meth.name() << "(";
for (int i = 0; i < meth.parameters().count(); i++) {
if (i > 0) { out << ", "; x_list.append(", "); }
const Parameter& param = meth.parameters()[i];
if (param.type()->getClass())
includes.insert(param.type()->getClass()->fileName());
out << param.type()->toString() << " x" << i + 1;
x_params += QString(" x[%1].%2 = %3;\n")
.arg(QString::number(i + 1)).arg(Util::stackItemField(param.type()))
.arg(Util::assignmentString(param.type(), "x" + QString::number(i + 1)));
x_list += "x" + QString::number(i + 1);
}
out << ") ";
if (meth.isConst())
out << "const ";
if (meth.hasExceptionSpec()) {
out << "throw(";
for (int i = 0; i < meth.exceptionTypes().count(); i++) {
if (i > 0) out << ", ";
out << meth.exceptionTypes()[i].toString();
}
out << ") ";
}
out << "{\n";
out << QString(" Smoke::StackItem x[%1];\n").arg(meth.parameters().count() + 1);
out << x_params;
if (meth.flags() & Method::PureVirtual) {
out << QString(" this->_binding->callMethod(%1, (void*)this, x, true /*pure virtual*/);\n").arg(m_smokeData->methodIdx[&meth]);
if (meth.type() != Type::Void) {
QString field = Util::stackItemField(meth.type());
if (meth.type()->pointerDepth() == 0 && field == "s_class") {
QString tmpType = type;
if (meth.type()->isRef()) tmpType.replace('&', "");
tmpType.append('*');
out << " " << tmpType << " xptr = (" << tmpType << ")x[0].s_class;\n";
out << " " << type << " xret(*xptr);\n";
out << " delete xptr;\n";
out << " return xret;\n";
} else {
out << QString(" return (%1)x[0].%2;\n").arg(type, Util::stackItemField(meth.type()));
}
}
} else {
out << QString(" if (this->_binding->callMethod(%1, (void*)this, x)) ").arg(m_smokeData->methodIdx[&meth]);
if (meth.type() == Type::Void) {
out << "return;\n";
} else {
QString field = Util::stackItemField(meth.type());
if (meth.type()->pointerDepth() == 0 && field == "s_class") {
QString tmpType = type;
if (meth.type()->isRef()) tmpType.replace('&', "");
tmpType.append('*');
out << "{\n";
out << " " << tmpType << " xptr = (" << tmpType << ")x[0].s_class;\n";
out << " " << type << " xret(*xptr);\n";
out << " delete xptr;\n";
out << " return xret;\n";
out << " }\n";
} else {
out << QString("return (%1)x[0].%2;\n").arg(type, Util::stackItemField(meth.type()));
}
}
out << " ";
if (meth.type() != Type::Void)
out << "return ";
out << QString("this->%1::%2(%3);\n").arg(meth.getClass()->toString()).arg(meth.name()).arg(x_list);
}
out << " }\n";
}
示例4: generateMethodBody
QString SmokeClassFiles::generateMethodBody(const QString& indent, const QString& className, const QString& smokeClassName, const Method& meth,
int index, bool dynamicDispatch, QSet<QString>& includes)
{
QString methodBody;
QTextStream out(&methodBody);
out << indent;
if (meth.isConstructor()) {
out << smokeClassName << "* xret = new " << smokeClassName << "(";
} else {
const Function* func = Util::globalFunctionMap[&meth];
if (func)
includes.insert(func->fileName());
if (meth.type()->getClass())
includes.insert(meth.type()->getClass()->fileName());
if (meth.type()->isFunctionPointer() || meth.type()->isArray())
out << meth.type()->toString("xret") << " = ";
else if (meth.type() != Type::Void)
out << meth.type()->toString() << " xret = ";
if (!(meth.flags() & Method::Static)) {
if (meth.isConst()) {
out << "((const " << smokeClassName << "*)this)->";
} else {
out << "this->";
}
}
if (!dynamicDispatch && !func) {
// dynamic dispatch not wanted, call with 'this->Foo::method()'
out << className << "::";
} else if (func) {
if (!func->nameSpace().isEmpty())
out << func->nameSpace() << "::";
}
out << meth.name() << "(";
}
for (int j = 0; j < meth.parameters().count(); j++) {
const Parameter& param = meth.parameters()[j];
if (param.type()->getClass())
includes.insert(param.type()->getClass()->fileName());
if (j > 0) out << ",";
QString field = Util::stackItemField(param.type());
QString typeName = param.type()->toString();
if (param.type()->isArray()) {
Type t = *param.type();
t.setPointerDepth(t.pointerDepth() + 1);
t.setIsRef(false);
typeName = t.toString();
out << '*';
} else if (field == "s_class" && (param.type()->pointerDepth() == 0 || param.type()->isRef()) && !param.type()->isFunctionPointer()) {
// references and classes are passed in s_class
typeName.append('*');
out << '*';
}
// casting to a reference doesn't make sense in this case
if (param.type()->isRef() && !param.type()->isFunctionPointer()) typeName.replace('&', "");
out << "(" << typeName << ")" << "x[" << j + 1 << "]." << field;
}
// if the method has any other default parameters, append them here as values
if (!meth.remainingDefaultValues().isEmpty()) {
const QStringList& defaultParams = meth.remainingDefaultValues();
if (meth.parameters().count() > 0)
out << "," ;
out << defaultParams.join(",");
}
out << ");\n";
if (meth.type() != Type::Void) {
out << indent << "x[0]." << Util::stackItemField(meth.type()) << " = " << Util::assignmentString(meth.type(), "xret") << ";\n";
} else {
out << indent << "(void)x; // noop (for compiler warning)\n";
}
return methodBody;
}
示例5: testClass
void ClassTestSuite::testClass()
{
//Class test = ClassOf<Test1>();
Class test = Class::lookup("ClassTest::Test1");
TS_ASSERT_EQUALS(test.simpleName(), "Test1");
#ifndef NO_RTTI
TS_ASSERT(test.describes<ClassTest::Test1>())
Class test2 = Class::lookup(typeid(ClassTest::Test1));
TS_ASSERT_EQUALS(test2.simpleName(), "Test1");
TS_ASSERT_EQUALS(test, test2);
#endif
Class::ClassList superClasses = test.superclasses();
TS_ASSERT_EQUALS(superClasses.size(), 2);
Class base1;
Class base2;
TS_ASSERT_THROWS(base1.simpleName(), std::runtime_error); // uninitialized handle
for(const Class& c: superClasses) {
if (c.fullyQualifiedName() == "ClassTest::TestBase1") {
base1 = c;
} else if (c.fullyQualifiedName() == "ClassTest::TestBase2") {
base2 = c;
}
}
TS_ASSERT_EQUALS(base1.fullyQualifiedName(), "ClassTest::TestBase1");
TS_ASSERT_EQUALS(base1.simpleName(), "TestBase1");
TS_ASSERT_EQUALS(base2.fullyQualifiedName(), "ClassTest::TestBase2");
TS_ASSERT_EQUALS(base2.simpleName(), "TestBase2");
TS_ASSERT(inheritanceRelation(test, base1));
TS_ASSERT(inherits(test, base1));
TS_ASSERT(inheritedBy(base1, test));
TS_ASSERT(inheritanceRelation(test, base2));
TS_ASSERT(inherits(test, base2));
TS_ASSERT(inheritedBy(base2, test));
TS_ASSERT(!inheritanceRelation(base1, base2));
Class::ConstructorList base1Constructors = base1.constructors();
TS_ASSERT_EQUALS(base1.constructors().size(), 1);
Constructor base1Constructor = base1Constructors.front();
TS_ASSERT_EQUALS(base1Constructor.numberOfArguments(), 0);
TS_ASSERT_THROWS(base1Constructor.call(), std::runtime_error); // abstract class
Class::ConstructorList base2Constructors = base2.constructors();
TS_ASSERT_EQUALS(base2.constructors().size(), 1);
Constructor base2Constructor = base2Constructors.front();
TS_ASSERT_EQUALS(base2Constructor.numberOfArguments(), 0);
VariantValue b2Inst = base2Constructor.call();
TS_ASSERT(b2Inst.isValid());
TS_ASSERT_EQUALS(base2.attributes().size(), 0);
Class::MethodList base2Methods = base2.methods();
TS_ASSERT_EQUALS(base2Methods.size(), 1);
Method base2Method1 = base2Methods.front();
TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1");
TS_ASSERT(base2Method1.isConst());
TS_ASSERT(!base2Method1.isVolatile());
TS_ASSERT(!base2Method1.isStatic());
TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0);
TS_ASSERT_EQUALS(base2Method1.call(b2Inst).value<int>(), 6);
Class::ConstructorList testConstructors = test.constructors();
Class::AttributeList attributes = test.attributes();
TS_ASSERT_EQUALS(attributes.size(), 2);
Attribute attr = attributes.front();
WITH_RTTI(TS_ASSERT(attr.type() == typeid(int)));
TS_ASSERT_EQUALS(testConstructors.size(), 3);
Constructor defaultConstr;
Constructor intConstr;
TS_ASSERT_THROWS(defaultConstr.argumentSpellings(), std::runtime_error);
for (const Constructor& c: testConstructors) {
if (c.numberOfArguments() == 0) {
defaultConstr = c;
} else if (c.numberOfArguments() == 1 && c.argumentSpellings()[0] == "int"){
//.........这里部分代码省略.........
示例6: testClass
void ClassTestSuite::testClass()
{
//Class test = ClassOf<Test1>();
Class test = Class::lookup("ClassTest::Test1");
TS_ASSERT_EQUALS(test.simpleName(), "Test1");
Class::ClassList superClasses = test.superclasses();
TS_ASSERT_EQUALS(superClasses.size(), 2);
Class base1;
Class base2;
TS_ASSERT_THROWS(base1.simpleName(), std::runtime_error); // uninitialized handle
for(const Class& c: superClasses) {
if (c.fullyQualifiedName() == "ClassTest::TestBase1") {
base1 = c;
} else if (c.fullyQualifiedName() == "ClassTest::TestBase2") {
base2 = c;
}
}
TS_ASSERT_EQUALS(base1.fullyQualifiedName(), "ClassTest::TestBase1");
TS_ASSERT_EQUALS(base1.simpleName(), "TestBase1");
TS_ASSERT_EQUALS(base2.fullyQualifiedName(), "ClassTest::TestBase2");
TS_ASSERT_EQUALS(base2.simpleName(), "TestBase2");
Class::ConstructorList base1Constructors = base1.constructors();
TS_ASSERT_EQUALS(base1.constructors().size(), 1);
Constructor base1Constructor = base1Constructors.front();
TS_ASSERT_EQUALS(base1Constructor.numberOfArguments(), 0);
TS_ASSERT_THROWS(base1Constructor.call(), std::runtime_error); // abstract class
Class::ConstructorList base2Constructors = base2.constructors();
TS_ASSERT_EQUALS(base2.constructors().size(), 1);
Constructor base2Constructor = base2Constructors.front();
TS_ASSERT_EQUALS(base2Constructor.numberOfArguments(), 0);
VariantValue b2Inst = base2Constructor.call();
TS_ASSERT(b2Inst.isValid());
TS_ASSERT_EQUALS(base2.attributes().size(), 0);
Class::MethodList base2Methods = base2.methods();
TS_ASSERT_EQUALS(base2Methods.size(), 1);
Method base2Method1 = base2Methods.front();
TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1");
TS_ASSERT(base2Method1.isConst());
TS_ASSERT(!base2Method1.isVolatile());
TS_ASSERT(!base2Method1.isStatic());
TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0);
TS_ASSERT_EQUALS(base2Method1.call(b2Inst).value<int>(), 6);
Class::ConstructorList testConstructors = test.constructors();
Class::AttributeList attributes = test.attributes();
TS_ASSERT_EQUALS(attributes.size(), 2);
Attribute attr = attributes.front();
WITH_RTTI(TS_ASSERT(attr.type() == typeid(int)));
TS_ASSERT_EQUALS(testConstructors.size(), 3);
Constructor defaultConstr;
Constructor intConstr;
TS_ASSERT_THROWS(defaultConstr.argumentSpellings(), std::runtime_error);
for (const Constructor& c: testConstructors) {
if (c.numberOfArguments() == 0) {
defaultConstr = c;
} else if (c.numberOfArguments() == 1 && c.argumentSpellings()[0] == "int") {
intConstr = c;
}
}
TS_ASSERT_EQUALS(defaultConstr.numberOfArguments(), 0);
TS_ASSERT_EQUALS(intConstr.numberOfArguments(), 1);
VariantValue testInst1 = defaultConstr.call();
TS_ASSERT(testInst1.isA<Test1>());
TS_ASSERT_EQUALS(attr.get(testInst1).value<int>(), 3);
VariantValue testInst2 = intConstr.call(77);
TS_ASSERT(testInst2.isA<Test1>());
//.........这里部分代码省略.........