Browse Source

Added extension method SetPropertyAsReadOnly to ASTContext.

The Property.HasGetter and HasSetter method will take the GenerationKind flag of the field/method pair into account.
pull/233/head
Elias Holzer 11 years ago
parent
commit
b83b5029b0
  1. 12
      src/AST/Property.cs
  2. 15
      src/Generator.Tests/Passes/TestPasses.cs
  3. 20
      src/Generator/Library.cs
  4. 7
      tests/Native/Passes.h

12
src/AST/Property.cs

@ -72,7 +72,10 @@ namespace CppSharp.AST
{ {
get get
{ {
return (GetMethod != null) || (Field != null); return (GetMethod != null &&
GetMethod.GenerationKind != GenerationKind.None) ||
(Field != null &&
Field.GenerationKind != GenerationKind.None);
} }
} }
@ -80,8 +83,11 @@ namespace CppSharp.AST
{ {
get get
{ {
return (SetMethod != null) || return (SetMethod != null &&
(Field != null && !Field.QualifiedType.Qualifiers.IsConst); SetMethod.GenerationKind != GenerationKind.None) ||
(Field != null &&
!Field.QualifiedType.Qualifiers.IsConst &&
Field.GenerationKind != GenerationKind.None);
} }
} }

15
src/Generator.Tests/Passes/TestPasses.cs

@ -108,5 +108,20 @@ namespace CppSharp.Generator.Tests.Passes
Assert.IsFalse(AstContext.FindClass("Foo").First().Methods.Find( Assert.IsFalse(AstContext.FindClass("Foo").First().Methods.Find(
m => m.Name == "toIgnore").IsGenerated); m => m.Name == "toIgnore").IsGenerated);
} }
[Test]
public void TestSetPropertyAsReadOnly()
{
const string className = "TestReadOnlyProperties";
passBuilder.AddPass(new FieldToPropertyPass());
passBuilder.AddPass(new GetterSetterToPropertyPass());
passBuilder.RunPasses(pass => pass.VisitLibrary(AstContext));
AstContext.SetPropertyAsReadOnly(className, "readOnlyProperty");
Assert.IsFalse(AstContext.FindClass(className).First().Properties.Find(
m => m.Name == "readOnlyProperty").HasSetter);
AstContext.SetPropertyAsReadOnly(className, "ReadOnlyPropertyMethod");
Assert.IsFalse(AstContext.FindClass(className).First().Properties.Find(
m => m.Name == "ReadOnlyPropertyMethod").HasSetter);
}
} }
} }

20
src/Generator/Library.cs

@ -204,6 +204,26 @@ namespace CppSharp
} }
} }
public static void SetPropertyAsReadOnly(this ASTContext context, string className, string propertyName)
{
var properties = context.FindClass(className)
.SelectMany(c => c.Properties.Where(p => p.Name == propertyName && p.HasSetter));
foreach (var property in properties)
if (property.SetMethod != null)
property.SetMethod.GenerationKind = GenerationKind.None;
else
{
var field = property.Field;
var quals = field.QualifiedType.Qualifiers;
quals.IsConst = true;
var qualType = field.QualifiedType;
qualType.Qualifiers = quals;
field.QualifiedType = qualType;
}
}
/// <summary> /// <summary>
/// Sets the parameter usage for a function parameter. /// Sets the parameter usage for a function parameter.
/// </summary> /// </summary>

7
tests/Native/Passes.h

@ -26,6 +26,13 @@ struct TestRename
int lowerCaseField; int lowerCaseField;
}; };
struct TestReadOnlyProperties
{
int readOnlyProperty;
int getReadOnlyPropertyMethod() { return 0; }
void setReadOnlyPropertyMethod(int value) { }
};
#define TEST_ENUM_ITEM_NAME_0 0 #define TEST_ENUM_ITEM_NAME_0 0
#define TEST_ENUM_ITEM_NAME_1 1 #define TEST_ENUM_ITEM_NAME_1 1
#define TEST_ENUM_ITEM_NAME_2 2 #define TEST_ENUM_ITEM_NAME_2 2

Loading…
Cancel
Save