disable copyctor

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8036 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-22 08:38:02 +00:00
commit 858453b1cf
5 changed files with 54 additions and 60 deletions

View file

@ -334,11 +334,11 @@ disable the implicit constructor/desctructor generation manually.
</p> </p>
<p> <p>
To do so, the <tt>%nodefaultctor</tt>, <tt>%nodefaultdtor</tt> and To do so, the <tt>%nodefaultctor</tt> and <tt>%nodefaultdtor</tt>
<tt>%nocopyctor</tt>directives can be used. Note that these directives directives can be used. Note that these directives only affects the
only affects the implicit generation, and they have no effect if implicit generation, and they have no effect if the default/copy
the default/copy constructors or destructor are explicitly declared in constructors or destructor are explicitly declared in the class
the class interface. interface.
</p> </p>
<p> <p>
@ -371,7 +371,7 @@ class Bar {
public: public:
Bar(); // The default constructor is generated, since is declared Bar(); // The default constructor is generated, since is declared
}; };
%defaultctor; // Enable the creation of default constructors again %clearnodefaultctor; // Enable the creation of default constructors again
</pre> </pre>
</div> </div>
@ -392,30 +392,6 @@ class Foo { // No destructor is generated, unless is declared
</pre> </pre>
</div> </div>
<p>
The <tt>%nocopyctor</tt> disable the automatic generation of the copy
constructor, but also has no effect over classes that explicitly
declare a public copy constructor:
</p>
<div class="code">
<pre>
%nocopytor Foo; // Disable the copy constructor for class Foo.
class Foo {
public:
Foo();
};
%nocopytor Bar; // Has no effect on Bar
class Bar {
public:
Bar(const Bar& b); // Copy constructor is generated
};
</pre>
</div>
<p>
</p>
<p> <p>
<b>Compatibility Note:</b> The generation of default <b>Compatibility Note:</b> The generation of default
constructors/implicit destructors was made the default behavior in SWIG constructors/implicit destructors was made the default behavior in SWIG

View file

@ -90,7 +90,6 @@ CPP_TEST_CASES += \
const_const_2 \ const_const_2 \
constant_pointers \ constant_pointers \
constover \ constover \
constructor_copy \
constructor_exception \ constructor_exception \
constructor_explicit \ constructor_explicit \
constructor_value \ constructor_value \

View file

@ -570,7 +570,8 @@ public:
/* If class is abstract. No default constructor. Sorry */ /* If class is abstract. No default constructor. Sorry */
if (Getattr(n,"abstract")) { if (Getattr(n,"abstract")) {
Delattr(n,"allocate:default_constructor"); Delattr(n,"allocate:default_constructor");
} else if (!Getattr(n,"allocate:default_constructor")) { }
if (!Getattr(n,"allocate:default_constructor")) {
/* Check base classes */ /* Check base classes */
List *bases = Getattr(n,"allbases"); List *bases = Getattr(n,"allbases");
int allows_default = 1; int allows_default = 1;
@ -588,7 +589,10 @@ public:
} }
} }
if (!Getattr(n,"allocate:has_copy_constructor")) { if (!Getattr(n,"allocate:has_copy_constructor")) {
if (!Getattr(n,"allocate:copy_constructor") && !Getattr(n,"abstract")) { if (Getattr(n,"abstract")) {
Delattr(n,"allocate:copy_constructor");
}
if (!Getattr(n,"allocate:copy_constructor")) {
/* Check base classes */ /* Check base classes */
List *bases = Getattr(n,"allbases"); List *bases = Getattr(n,"allbases");
int allows_copy = 1; int allows_copy = 1;

View file

@ -352,7 +352,7 @@ void swig_pragma(char *lang, char *name, char *value) {
GenerateDefault = 1; GenerateDefault = 1;
} else if ((strcmp(name,"no_default") == 0) || ((strcmp(name,"nodefault") == 0))) { } else if ((strcmp(name,"no_default") == 0) || ((strcmp(name,"nodefault") == 0))) {
Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1, Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1,
"dangerous, use %nodefaultctor, %nodefaultdtor or %nocopyctor instead.\n"); "dangerous, use %nodefaultctor, %nodefaultdtor instead.\n");
GenerateDefault = 0; GenerateDefault = 0;
} else if (strcmp(name,"attributefunction") == 0) { } else if (strcmp(name,"attributefunction") == 0) {
String *nvalue = NewString(value); String *nvalue = NewString(value);
@ -2050,7 +2050,7 @@ static Node *makeCopyConstructor(Node *n)
Swig_symbol_setscope(oldscope); Swig_symbol_setscope(oldscope);
return cn; return cn;
} }
/*
static Node *makeConstructor(Node *n) static Node *makeConstructor(Node *n)
{ {
Node *cn = NewHash(); Node *cn = NewHash();
@ -2067,6 +2067,30 @@ static Node *makeConstructor(Node *n)
Swig_symbol_setscope(oldscope); Swig_symbol_setscope(oldscope);
return cn; return cn;
} }
*/
static Node *makeConstructor(Node *n)
{
Node *cn = Copy(n);
String *name = Getattr(n,"name");
String *rname = 0;
String *dname = NewStringf("%s::",name);
if (SwigType_istemplate(name)) {
rname = SwigType_templateprefix(name);
name = rname;
}
String *lname = Swig_scopename_last(name);
Append(dname,lname);
SetFlag(cn,"feature:new");
Setattr(cn,"decl","f().");
Swig_features_get(Swig_cparse_features(), 0, dname, Getattr(cn,"decl"), cn);
Delete(rname);
Delete(dname);
Delete(lname);
return cn;
}
static Node *makeDestructor(Node *n) static Node *makeDestructor(Node *n)
{ {
@ -2095,28 +2119,6 @@ int Language::classHandler(Node *n) {
bool hasDirector = Swig_directorclass(n) ? true : false; bool hasDirector = Swig_directorclass(n) ? true : false;
int odefault = (GenerateDefault && !GetFlag(n,"feature:nodefault"));
if (!ImportMode && (!GetFlag(n,"feature:nocopyctor")) && odefault) {
if (!Getattr(n,"has_copy_constructor") && !Getattr(n,"allocate:has_copy_constructor")
&& (Getattr(n,"allocate:copy_constructor"))) {
if (!Abstract) {
Node *cn = makeCopyConstructor(CurrentClass);
appendChild(n,cn);
}
}
}
cplus_mode = PUBLIC;
if (!ImportMode && !GetFlag(n,"feature:nodefaultctor") && odefault) {
if (!Getattr(n,"has_constructor") && !Getattr(n,"allocate:has_constructor")
&& (Getattr(n,"allocate:default_constructor"))) {
/* Note: will need to change this to support different kinds of classes */
if (!Abstract) {
Node *cn = makeConstructor(CurrentClass);
appendChild(n,cn);
}
}
}
/* Emit all of the class members */ /* Emit all of the class members */
emit_children(n); emit_children(n);
@ -2132,6 +2134,18 @@ int Language::classHandler(Node *n) {
SmartPointer = 0; SmartPointer = 0;
} }
cplus_mode = PUBLIC;
int odefault = (GenerateDefault && !GetFlag(n,"feature:nodefault"));
if (!ImportMode && (GenerateDefault && !GetFlag(n,"feature:nodefault"))) {
if (!Getattr(n,"has_constructor") && !Getattr(n,"allocate:has_constructor") && (Getattr(n,"allocate:default_constructor"))) {
/* Note: will need to change this to support different kinds of classes */
if (!Abstract) {
Node *cn = makeConstructor(CurrentClass);
constructorHandler(cn);
Delete(cn);
}
}
}
if (!ImportMode && !GetFlag(n,"feature:nodefaultdtor") && odefault) { if (!ImportMode && !GetFlag(n,"feature:nodefaultdtor") && odefault) {
if (!Getattr(n,"has_destructor") && (!Getattr(n,"allocate:has_destructor")) if (!Getattr(n,"has_destructor") && (!Getattr(n,"allocate:has_destructor"))
&& (Getattr(n,"allocate:default_destructor"))) { && (Getattr(n,"allocate:default_destructor"))) {

View file

@ -85,8 +85,7 @@ static const char *usage2 = (const char*)"\
-nocontract - Turn off contract checking \n\ -nocontract - Turn off contract checking \n\
-nodefault - Do not generate constructors, destructor or copy constructors (dangerous, don't use it)\n\ -nodefault - Do not generate constructors, destructor or copy constructors (dangerous, don't use it)\n\
-nodefaultctor - Do not generate implicit default constructors\n\ -nodefaultctor - Do not generate implicit default constructors\n\
-nodefaultdctor - Do not generate implicit default destructors (dangerous, use %nodefaultdtor instead)\n\ -nodefaultdtor - Do not generate implicit default destructors (dangerous, use %nodefaultdtor instead)\n\
-nocopyctor - Do not generate implicit copy constructors\n\
-nodirprot - Do not wrap director protected members\n\ -nodirprot - Do not wrap director protected members\n\
-noexcept - Do not wrap exception specifiers\n\ -noexcept - Do not wrap exception specifiers\n\
-addextern - Add extra extern declarations\n\ -addextern - Add extra extern declarations\n\
@ -448,7 +447,7 @@ void SWIG_getoptions(int argc, char *argv[])
} else if ((strcmp(argv[i],"-no_default") == 0) || (strcmp(argv[i],"-nodefault") == 0)) { } else if ((strcmp(argv[i],"-no_default") == 0) || (strcmp(argv[i],"-nodefault") == 0)) {
GenerateDefault = 0; GenerateDefault = 0;
Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1, Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1,
"dangerous, use -nodefaultctor, -nodefaultdtor or -nocopyctor instead.\n"); "dangerous, use -nodefaultctor, -nodefaultdtor instead.\n");
Swig_mark_arg(i); Swig_mark_arg(i);
} else if ((strcmp(argv[i],"-nodefaultctor") == 0)) { } else if ((strcmp(argv[i],"-nodefaultctor") == 0)) {
SWIG_setfeature("feature:nodefaultctor","1"); SWIG_setfeature("feature:nodefaultctor","1");
@ -456,9 +455,11 @@ void SWIG_getoptions(int argc, char *argv[])
} else if ((strcmp(argv[i],"-nodefaultdtor") == 0)) { } else if ((strcmp(argv[i],"-nodefaultdtor") == 0)) {
SWIG_setfeature("feature:nodefaultdtor","1"); SWIG_setfeature("feature:nodefaultdtor","1");
Swig_mark_arg(i); Swig_mark_arg(i);
#if 0
} else if ((strcmp(argv[i],"-nocopyctor") == 0)) { } else if ((strcmp(argv[i],"-nocopyctor") == 0)) {
SWIG_setfeature("feature:nocopyctor","1"); SWIG_setfeature("feature:nocopyctor","1");
Swig_mark_arg(i); Swig_mark_arg(i);
#endif
} else if (strcmp(argv[i],"-noexcept") == 0) { } else if (strcmp(argv[i],"-noexcept") == 0) {
NoExcept = 1; NoExcept = 1;
Swig_mark_arg(i); Swig_mark_arg(i);