Restricting access to module procedures from other module procedures - fortran

I have recently discovered the new feature of Fortran 2008, i.e., SUBMODULEs.
Please have a look at my minimum working example down the question. After compilation, it puts the following on the terminal:
Accessed sub0
Accessed sub1
Accessed sub2
That is, as it should, module procedures of sub1 and sub2 can CALL each other and everything is OK.
Because of reasons like code architecture and maintenance, I need to restrict this access somehow. That is, module procedures (sub1 and sub2) be invisible to each other. Can I do so?
MODULE parent
PRIVATE
PUBLIC :: sub0
INTERFACE
MODULE SUBROUTINE sub1 ()
END SUBROUTINE
MODULE SUBROUTINE sub2 ()
END SUBROUTINE
END INTERFACE
CONTAINS
SUBROUTINE sub0 ()
PRINT *, 'Accessed sub0'
CALL sub1 ()
END SUBROUTINE
END MODULE
SUBMODULE ( parent ) submod1
CONTAINS
MODULE PROCEDURE sub1
PRINT *, 'Accessed sub1'
CALL sub2 ()
END SUBROUTINE
END SUBMODULE
SUBMODULE ( parent ) submod2
CONTAINS
MODULE PROCEDURE sub2
PRINT *, 'Accessed sub2'
END PROCEDURE
END SUBMODULE
PROGRAM driver
USE parent
CALL sub0 ()
END PROGRAM

Not really.
Both sub1 and sub2 are accessed by sub0, which means that either (or some combination):
sub and sub2 have to be known at the same level of the module/submodule hierarchy as sub0, as in the example. The subprograms for sub1 and sub2 have to be either at the same level as sub0 or below, in which case host association makes the knowledge of the sub1 or sub2 procedure available to the other procedure.
sub1 and sub2 need to be a public entity of some other two modules. But in this case the subprogram of sub1 or sub2 can always just directly reference the module that defines the other.
sub1 and sub2 are external procedures. Again, the subprogram of sub1 or sub2 can directly access the other external procedure.
Entities in a host can be hidden from child scopes if there is a name in the child scope that shadows the name in the host entity (or by use of the expanded capabilities of the import statement in the F2015 draft standard). You could put a dummy declaration of something with the same name as the name of the procedure that you want to block out from a particular scope, but this is rather artificial.

Related

Passing Configuration Envirnoment in OCaml

I'm trying to understand and/or find examples of how robust OCaml applications deal with a configuration environment. I'm coming from a world of Haskell, where I would use the Reader monad to solve this problem. In particular, I want to define a top-level configuration which I may pass throughout my application, but only some of the functions in my application will need to use the configuration.
To motivate this, consider a simple executable which queries a database. I would want to define a top-level main file which might do things like setup a connection pool, or create a logger which would be shared throughout the application.
I'm assuming OCaml has some pattern for dealing with this, but I cannot find any great examples. I'd prefer not define my own Reader monad and functorize the entire application if I don't have to.
That being said, this is also ugly
main.mli
module Config : sig
type t = {
fooConfig : Foo.Config.t;
dbClientConfig : DB_client.Config.t;
}
val make : Foo.Config.t -> DB_client.Config.t -> t
end
val main : Config.t -> unit
foo.mli
module Config : sig
type t = {
dbConfig : DB_client.Config.t;
}
end
(** fooFunc will need to pass the dbConfig, but it does not actually explicitly need anything in the config **)
val fooFunc : Config.t -> string -> unit
db_client.mli
module Config : sig
type t = {
connPool : SomeConnPool
}
end
(** Finally, I need to use the config to grab a connection out of the pool **)
val writeToDb : Config.t -> string -> (string, string) Lwt.result
I don't want to make the explicit arguments of function "higher in the execution stack" depend explicitly on a configuration they will never use.
Is there a nice functional pattern to deal with this nested dependency coupling? I'd appreciate any code examples that someone can point towards so I can study a better approach.

Can I create a common setup routine for all fixtures in a namespace in dunitx?

Is it possible to use namespaces in dunitx in such a way that all test fixtures under a namespace are enclosed by one pair of setup/teardown routines?
(Similar to the SetupFixture attribute in nunit, http://www.nunit.org/index.php?p=setupFixture&r=2.5.5).
I tried to use the following unit names/namespaces:
Tests.MyFixture.pas for the TMyFixtureInitializer class with the common setup and teardown methods
Tests.MyFixture.MyTestUnit1.pas and Tests.MyFixture.MyTestUnit2.pas for the actual test classes.
I ran into the following problems:
TMyFixtureInitializer.SetupFixture and TeardownFixture are not executed when the class itself does not contain any test routine.
After adding a dummy test in TMyFixtureInitializer, the SetupFixture and TeardownFixture routines are called, but after the tests in Tests.MyFixture.MyTestUnit1 and Tests.MyFixture.MyTestUnit2.
When I set the SetupFixture attribute on the constructor, and the TeardownFixture attribute on the destructor of TMyFixtureInitializer, they are executed before and after all tests, ignoring the namespaces altogether.
Tests.MyFixture.pas:
unit Tests.MyFixture;
interface
uses
DUnitX.TestFramework;
type
[TestFixture]
TMyFixtureInitializer = class(TObject)
public
[SetupFixture]
procedure SetupMyFixture;
[TeardownFixture]
procedure TeardownMyFixture;
end;
implementation
{ TMyFixtureInitializer }
procedure TMyFixtureInitializer.SetupMyFixture;
begin
Self.Log('initialize a lot of stuff ...');
end;
procedure TMyFixtureInitializer.TeardownMyFixture;
begin
Self.Log('cleanup a lot of stuff ...');
end;
initialization
TDUnitX.RegisterTestFixture(TMyFixtureInitializer);
end.
Tests.MyFixture.MyTestUnit1.pas:
unit Tests.MyFixture.MyTestUnit1;
interface
uses
DUnitX.TestFramework;
type
[TestFixture]
TestClass1 = class(TObject)
public
[Test]
procedure Test;
end;
implementation
{ TestClass1 }
procedure TestClass1.Test;
begin
end;
initialization
TDUnitX.RegisterTestFixture(TestClass1);
end.
(Source of Tests.MyFixture.MyTestUnit2.pas is analogous).
Does anyone have an example of how to use namespaces to organize initialization and cleanup?
Imho, it does not make sense to define a [TestFixture] without any tests...
And: as far as I know, there is no support for namespaces in DUnitX.
DUnitX just runs all the fixtures (and the tests) in a loop.
If you only need methods that run before all tests / after all tests, you could just do the following (instead):
Tests.MyFixture.pas:
unit Tests.MyFixture;
interface
// uses
//DUnitX.TestFramework;
type
TMyFixtureInitializer = class(TObject)
public
class procedure SetupMyFixture;
class procedure TeardownMyFixture;
end;
implementation
{ TMyFixtureInitializer }
class procedure TMyFixtureInitializer.SetupMyFixture;
begin
//Self.Log('initialize a lot of stuff ...');
end;
class procedure TMyFixtureInitializer.TeardownMyFixture;
begin
//Self.Log('cleanup a lot of stuff ...');
end;
initialization
TMyFixtureInitializer.SetupMyFixture;
finalization
TMyFixtureInitializer.TeardownMyFixture;
end.
But possibly, this solution is just too simple for your problem... I think, you want to have more than one namespace... to kind-of group them.
Sorry, no idea for this.

Unit test main function: imports itself error

I'm trying to write a (small) executable, setup using Cabal, unit tested using HSpec. Almost all of my code is in a separate module, Library, including the body of main, which I import into my Main module as the run function
-- In src/Hecho.hs
module Main where
import Library(run)
main :: IO ()
main = run
Although the main function is now as short as I think it can be, is there a way I can write a test for it, say to check that it equals the run function, or maybe to test it some other way? The issue is that my spec file defines another Main module, and I don't seem to be able (or at least I can't figure out how) to import anything into it from the other Main module.
For example, if I try the following
-- In test/HechoSpec.hs
module Main where
import Library
import Main
import Test.Hspec
main :: IO ()
main = hspec $ do
-- Test definitions
Then I get the error:
Module imports form a cycle:
module `Main' (test/HechoSpec.hs) imports itself
Is there a way to test the main function?
Updated answer: Apparently the question is how to make sure Library.run is the same as main.
The answer is that it's not possible to do that. main is a value of type IO () and there is no Eq defined for IO actions. For instance, this program does not type check:
main = print "Hello"
foo = main
fooEqualsMain = foo == main

How to tell child modules to use specific logger

I have created two loggers in my logging module like logger1,logger2 and my application has two submodules module1 and module2.I want to configure/tell module1 should use logger1 and module2 should use logger2 only?
Assign logger1 to some variable in module1 and let functions in that module use that variable to call correct logger. And remeber to check in functions whether variable is not None.

Implement VB.NET Interface Class in C++.NET (2010)

I wonder how I can import an Interface Class in C++.NET which was written in VB.NET.
The Dll contains only a Interface class. If I write "Implement iInterface" in VB.NET, it'll load all Functions and Methods from the Interface.dll.
But how can I do this in C++.NET?
This is what the Interface.dll looks like:
Public Interface IPlugin
Function CreateInstance( _
ByRef pntMemory As MemoryArbiter, _
ByRef pntMessageQueue As clsMessageQueue, _
ByRef pntGPIO As clsGPIO, _
ByRef pntProgramSettings As Types, _
ByRef pntDisplayDriver As DisplayDriver _
) As Boolean
Function DeleteInstance() As Boolean
Sub Main_Loop()
Sub ForceUnload()
Sub Interrupt()
End Interface
This structure has to be imported to the c++ code.
In VB.NET it looks like this:
Imports System.ComponentModel.Composition
Imports System.Windows.Forms.Application
Imports Plugin_Interface
Imports SharedLibrary
Imports SharedLibrary.DisplayDriver
<Export(GetType(IPlugin))>
Public Class cPlugin
Implements IPlugin
'.... Class functions come here
I, however, just need a tanslation of the file above so I can import the C++ DLL via MEF in my VB.NET 2010 Application.
Hope someone knows how to handle this ... :)