Skip to content

Commit

Permalink
style: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Dec 22, 2023
1 parent d72b8ed commit 9eaf867
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 196 deletions.
2 changes: 1 addition & 1 deletion Ndapi/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public string QueryDataSourceName
public QueryDataSourceType QueryDataSourceType
{
get => GetNumberProperty<QueryDataSourceType>(NdapiConstants.D2FP_QRY_DAT_SRC_TYP);
set => SetNumberProperty<QueryDataSourceType>(NdapiConstants.D2FP_QRY_DAT_SRC_TYP, value);
set => SetNumberProperty(NdapiConstants.D2FP_QRY_DAT_SRC_TYP, value);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Ndapi/Core/PropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ndapi.Core
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Property)]
internal sealed class PropertyAttribute : Attribute
{
internal int PropertyId { get; }
Expand Down
14 changes: 2 additions & 12 deletions Ndapi/FormModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public new static FormModule Open(string filename)
#if FORMS_6
var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out var form, filename, false);
#else
var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out form, filename);
var status = NativeMethods.d2ffmdld_Load(NdapiContext.GetContext(), out var form, filename);
#endif
Ensure.Success(status);

Expand Down Expand Up @@ -443,23 +443,13 @@ public override void CompileObjects()
Ensure.Success(status);
}

/// <summary>
/// Gets the version of the last Form Builder that loaded the module.
/// </summary>
/// <param name="file">Form module location (.fmb file)</param>
/// <returns>The Form Builder version</returns>
public static int GetFileVersion(string file)
{
return GetFileVersion(file, false);
}

/// <summary>
/// Gets the version of the last Form Builder that loaded the module.
/// </summary>
/// <param name="file">Form module location (.fmb file)</param>
/// <param name="loadFromDb">Module should be loaded from database.</param>
/// <returns>The Form Builder version</returns>
public static int GetFileVersion(string file, bool loadFromDb)
public static int GetFileVersion(string file, bool loadFromDb = false)
{
#if FORMS_6
var status = NativeMethods.d2ffmdfv_FileVersion(NdapiContext.GetContext(), file, loadFromDb, out var version);
Expand Down
2 changes: 1 addition & 1 deletion Ndapi/LibraryModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override void Save(string path)
/// <summary>
/// Program unit libraries does not support saving.
/// </summary>
public override void Save(string path = null, bool saveInDatabase = false)
public override void Save(string path, bool saveInDatabase)
{
throw new NotSupportedException("Library module does not support saving");
}
Expand Down
12 changes: 1 addition & 11 deletions Ndapi/MenuModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,13 @@ public override void CompileObjects()
Ensure.Success(status);
}

/// <summary>
/// Gets the version of the last Form Builder that loaded the module.
/// </summary>
/// <param name="file">Menu module location (.mmb file)</param>
/// <returns>The Form Builder version</returns>
public static int GetFileVersion(string file)
{
return GetFileVersion(file, false);
}

/// <summary>
/// Gets the version of the last Form Builder that loaded the module.
/// </summary>
/// <param name="file">Menu module location (.mmb file)</param>
/// <param name="loadFromDb">Module should be loaded from database.</param>
/// <returns>The Form Builder version</returns>
public static int GetFileVersion(string file, bool loadFromDb)
public static int GetFileVersion(string file, bool loadFromDb = false)
{
#if FORMS_6
var status = NativeMethods.d2fmmdfv_FileVersion(NdapiContext.GetContext(), file, loadFromDb, out var version);
Expand Down
9 changes: 3 additions & 6 deletions Ndapi/Metadata/NdapiMetaProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ internal static NdapiMetaProperty GetOrCreate(int propertyId, string name, bool

private Dictionary<int, string> LoadAllowedValues()
{
if (AcceptConstants)
{
return Enum.GetValues(RawPropertyType).Cast<int>().ToDictionary(e => e, e => Enum.GetName(RawPropertyType, e));
}

return new Dictionary<int, string>();
return AcceptConstants ?
Enum.GetValues(RawPropertyType).Cast<int>().ToDictionary(e => e, e => Enum.GetName(RawPropertyType, e)) :
new Dictionary<int, string>();
}

/// <summary>
Expand Down
46 changes: 23 additions & 23 deletions Ndapi/NdapiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,30 @@ public sealed class NdapiContext : IDisposable

internal static ContextSafeHandle GetContext()
{
if (_context == null)
if (_context != null)
{
var context_attributes = new D2fContextAttributes();
context_attributes.mask_d2fctxa = D2FCTXAMCALLS;
context_attributes.d2fmalc_d2fctxa = allocateMemory;
context_attributes.d2fmrlc_d2fctxa = reallocateMemory;
context_attributes.d2fmfre_d2fctxa = freeMemory;
D2fErrorCode status;
try
{
status = NativeMethods.d2fctxcr_Create(out _context, ref context_attributes);
}
catch (DllNotFoundException)
{
throw new NdapiException($"Could not found the {NativeMethods.formsLib} from Oracle Forms installation. " +
"Please check if this version of Oracle Forms is installed.");
}
Ensure.Success(status);
return _context;
}

var contextAttributes = new D2fContextAttributes
{
mask_d2fctxa = D2FCTXAMCALLS,
d2fmalc_d2fctxa = allocateMemory,
d2fmrlc_d2fctxa = reallocateMemory,
d2fmfre_d2fctxa = freeMemory
};

D2fErrorCode status;
try
{
status = NativeMethods.d2fctxcr_Create(out _context, ref contextAttributes);
}
catch (DllNotFoundException)
{
throw new NdapiException($"Could not found the {NativeMethods.formsLib} from Oracle Forms installation. " +
"Please check if this version of Oracle Forms is installed.");
}
Ensure.Success(status);
return _context;
}

Expand All @@ -72,12 +77,7 @@ private static IntPtr AllocateMemory(ref IntPtr context, IntPtr size)

private static IntPtr ReallocateMemory(ref IntPtr context, IntPtr ptr, IntPtr newsize)
{
if (ptr == IntPtr.Zero)
{
return AllocateMemory(ref context, newsize);
}

return Marshal.ReAllocHGlobal(ptr, newsize);
return ptr == IntPtr.Zero ? AllocateMemory(ref context, newsize) : Marshal.ReAllocHGlobal(ptr, newsize);
}

private static void FreeMemory(ref IntPtr context, IntPtr ptr)
Expand Down
12 changes: 1 addition & 11 deletions Ndapi/NdapiException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.Serialization;

using Ndapi.Enums;

Expand All @@ -9,7 +8,7 @@ namespace Ndapi
/// The exception that is thrown when an error occurs during application execution.
/// </summary>
[Serializable]
public class NdapiException : Exception
public sealed class NdapiException : Exception
{
/// <summary>
/// Gets the internal Forms API error code.
Expand Down Expand Up @@ -40,15 +39,6 @@ public NdapiException(string message, Exception inner) : base(message, inner)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NdapiException"/> class with a serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected NdapiException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

internal NdapiException(string message, D2fErrorCode code) : this(message)
{
ErrorCode = code;
Expand Down
10 changes: 4 additions & 6 deletions Ndapi/NdapiMenuItemRoleList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ namespace Ndapi
public class NdapiMenuItemRoleList : IEnumerable<string>
{
private readonly MenuItem _menuItem;
private readonly int _count;

internal NdapiMenuItemRoleList(MenuItem menuitem)
{
_menuItem = menuitem;
_count = menuitem.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT);
Count = menuitem.GetNumberProperty(NdapiConstants.D2FP_OBJ_COUNT);
}

public int Count => _count;
public int Count { get; }

public IEnumerator<string> GetEnumerator() => new Enumerator(this);

Expand All @@ -24,7 +23,6 @@ public sealed class Enumerator : IEnumerator<string>
{
private readonly NdapiMenuItemRoleList _ndapiMenuItemRoleList;
private int _position;
private string _current;

internal Enumerator(NdapiMenuItemRoleList ndapiMenuItemRoleList)
{
Expand All @@ -39,14 +37,14 @@ public bool MoveNext()
return false;
}

_current = _ndapiMenuItemRoleList._menuItem.GetRoleAt(_position);
Current = _ndapiMenuItemRoleList._menuItem.GetRoleAt(_position);
_position++;
return true;
}

public void Reset() => _position = 1;

public string Current => _current;
public string Current { get; private set; }

object IEnumerator.Current => Current;

Expand Down
58 changes: 17 additions & 41 deletions Ndapi/NdapiObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public int ParentModuleType
public ModuleStorageType ParentModuleStorage
{
get => GetNumberProperty<ModuleStorageType>(NdapiConstants.D2FP_PAR_MODSTR);
set => SetNumberProperty<ModuleStorageType>(NdapiConstants.D2FP_PAR_MODSTR, value);
set => SetNumberProperty(NdapiConstants.D2FP_PAR_MODSTR, value);
}
#endif

Expand Down Expand Up @@ -257,12 +257,7 @@ public void SetBooleanProperty(int property, bool value)
var status = NativeMethods.d2fobgo_GetObjProp(NdapiContext.GetContext(), _handle, property, out var handle);
Ensure.Success(status);

if (handle.IsInvalid)
{
return null;
}

return Create<T>(handle);
return handle.IsInvalid ? null : Create<T>(handle);
}

/// <summary>
Expand Down Expand Up @@ -406,7 +401,7 @@ public bool HasInheritedProperty(int property)
/// <returns>The property state.</returns>
public PropertyState GetPropertyState(int property)
{
var state = PropertyState.Unknown;
PropertyState state;
if (HasInheritedProperty(property))
{
state = PropertyState.Inherited;
Expand Down Expand Up @@ -436,24 +431,14 @@ public void Reattach()
Ensure.Success(status);
}

/// <summary>
/// Change the subclassing parent of an object to the parent object.
/// This will cause the property values to be overriden for all properties which are defined on the parent object.
/// </summary>
/// <param name="parent">Object to subclass.</param>
public void Subclass(NdapiObject parent)
{
Subclass(parent, false);
}

/// <summary>
/// Change the subclassing parent of an object to the parent object.
/// This will cause the property values to be overriden for all properties which are defined on the parent object.
/// </summary>
/// <param name="parent">Object to subclass.</param>
/// <param name="keepPath">Indicates whether the system should refer to the parent object's module by filename or by path+filename.
/// The recommended choice is false in most cases.</param>
public void Subclass(NdapiObject parent, bool keepPath)
public void Subclass(NdapiObject parent, bool keepPath = false)
{
var status = NativeMethods.d2fobsc_SubClass(NdapiContext.GetContext(), _handle, parent._handle, keepPath);
Ensure.Success(status);
Expand Down Expand Up @@ -487,7 +472,7 @@ public virtual void Destroy()
var instance = Activator.CreateInstance(objectType,
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] { handle },
new object[] { handle },
null);
return (T)instance;
}
Expand All @@ -499,19 +484,21 @@ public virtual void Destroy()
public override string ToString() => Name;

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
private bool disposedValue; // To detect redundant calls

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (disposedValue)
{
if (disposing)
{
_handle?.Dispose();
}
return;
}

disposedValue = true;
if (disposing)
{
_handle?.Dispose();
}

disposedValue = true;
}

public void Dispose()
Expand All @@ -526,7 +513,7 @@ public void Dispose()
/// </summary>
public abstract class NdapiObject<T> : NdapiObject where T : NdapiObject
{
internal NdapiObject() : base() { }
internal NdapiObject() { }
internal NdapiObject(ObjectType type) : base(type) { }
internal NdapiObject(string name, ObjectType type, NdapiObject parent = null) : base(name, type, parent) { }
internal NdapiObject(ObjectSafeHandle handle) : base(handle) { }
Expand All @@ -548,18 +535,7 @@ public abstract class NdapiObject<T> : NdapiObject where T : NdapiObject
/// <param name="newName">Name of the new object.</param>
public T Clone(string newName)
{
return Clone(newName, null, true);
}

/// <summary>
/// Creates a new object with the given name and owner.
/// The new object is an exact copy of the original object, with all the same property values.
/// </summary>
/// <param name="newName">Name of the new object.</param>
/// /// <param name="newOwner">New owner of the object. If null, the object will be owned by the same parent of the current object.</param>
public T Clone(string newName, NdapiObject newOwner)
{
return Clone(newName, newOwner, true);
return Clone(newName, null);
}

/// <summary>
Expand All @@ -570,7 +546,7 @@ public T Clone(string newName, NdapiObject newOwner)
/// <param name="newOwner">New owner of the object. If null, the object will be owned by the same parent of the current object.</param>
/// <param name="keepSubclassingInfo">If false, the sublassing info is discarded and the inherited properties are flattened into local values in the new object.</param>
/// <returns>The new object.</returns>
public T Clone(string newName, NdapiObject newOwner, bool keepSubclassingInfo)
public T Clone(string newName, NdapiObject newOwner, bool keepSubclassingInfo = true)
{
var parentHandle = newOwner?._handle ?? Owner._handle;

Expand Down

0 comments on commit 9eaf867

Please sign in to comment.