How to call c++ program in Python - visual-studio-2005

I have written simple HelloWorld class. and write a boost python wrapper.and debug the code as DLL.My question is how can i expose this code in python and use greet function.I tried by giving the path in sys.path.insert. but not able to get greet function. The code i treid is below.
Thanks for help.
#include<boost/python.hpp>
using namespace std;
using namespace boost::python;
class World
{
public:
string msg;
void set(string msg)
{
this->msg=msg;
}
string greet()
{
return msg;
}
};
BOOST_PYTHON_MODULE(ExpsoingClasses)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}

At least on my system, I had to rename the library file from ExpsoingClasses.dll to ExpsoingClasses.pyd before I could import it in Python. Once you do that, this should work:
import ExpsoingClasses
retVal = ExpsoingClasses.World()
retVal.set('hello world')
print retVal.greet()

Related

Is it possible to include code only inside one class?

I hope I can explain myself.
Supose I have next:
File "A.h":
#include "C.h"
public class A{
// Some code...
}
File "B.h":
#include "A.h"
public class B{
A a = new A(); //With this line I mean I'm using one instance of "A" inside "B.h"
//Some code...
}
Is it possible to include "C.h" ONLY inside "A.h"?
My problem is that the code I've included is giving me a lot of conflicts with usual functions. It's not an option to correct conflicts one by one, because there is a huge set of them. Also, my "C.h" code included is only a test code: after some tests, I will delete the include line.
Is there any way of 'bubbling' my include?
Thank you in advance.
EDIT: A.h and B.h are on the same namespace.
Is it possible to include "C.h" ONLY inside "A.h"?
No. Not to my knowledge.
If you have name conflicts, just include C.h within an other namespace, as #user202729 proposed. This can help.
But I guess you use C in A for tests and you cannot use it in C in A without the implementation which is not compatible to C++Cli or content from B.h.
We used the pimpl ideom (pointer to implementation).
Example:
c++/clr currently does not allow do be included directly and that's why sometimes you cannot use libraries you want to use (like C.h), because they do rely on the support of .
This is my C.h ( used by all the other headers)
struct LockImpl; // forward declaration of C.
class C
{
public:
C();
virtual ~C();
public:
void Lock() const;
void Unlock() const;
LockImpl* _Lock;
};
This is my C.cpp (compiled without /clr )
#include <mutex>
struct LockImpl
{
std::mutex mutex;
};
C::C() : _Lock(new LockImpl()) {}
C::~C() { delete _Lock; }
void C::Lock() const
{
_Lock->mutex.lock();
}
void C::Unlock() const
{
_Lock->mutex.unlock();
}
A.h
#include "C.h"
public class A{
C c;
void someMethod()
{
c.Lock() // I used another template for a RAII pattern class.
c.Unlock()
}
}

How to call a method added to the windows native module of Appelerator Platform

We write to ask how to correctly call a method of Titanium Appelerator native module.
Our development environment is as the following:
Titanium SDK :5.2.0
Appcelerator CLI: 5.2.0
TabletPC:Windows10pro
Our module has been developed with the following procedures;
(1) we created a module referring to the document of https://github.com/appcelerator/titanium_mobile_windows#module-development
An additional method aaa() was added to the module.
cd MY_WORKSPACE
ti create -p windows -t module //create a module project
cd MY_MODULE_NAME/windows
ti build -p windows -T ws-local //build my module project
(2) A windows’ application was created to call the above module, and there was no error while we call the module from the application. However, while we call the method aaa(), Titanium error message as the following occurred;
“aaa() method does not exist”.
In the Titanium windows platform for windows10, the dll for module was created correctly. Then, we are just wondering which parts of our source are not correct;
(1) The definition of a method aaa()is not correct.
(2) The source code to call the native module is not correct.
(3) The source code to call the aaa() method is not correct.
We attached our source code as the following and thank you in advance for your comment and advice.
The code is as below.
windows/src/JpNativeModuleExample.cpp
#include "JpNativeModuleExample.hpp"
#include "Titanium/detail/TiImpl.hpp"
namespace Jp
{
NativeModuleExample::NativeModuleExample(const JSContext& js_context) TITANIUM_NOEXCEPT
: JSExportObject(js_context)
{
TITANIUM_LOG_DEBUG("NativeModuleExample::ctor Initialize");
}
void NativeModuleExample::postInitialize(JSObject& js_object)
{
}
void NativeModuleExample::postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments)
{
}
//add new method
void NativeModuleExample::aaa() TITANIUM_NOEXCEPT
{
}
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
}
}
windows/include/JpNativeModuleExample.hpp
#ifndef _JPNATIVEMODULEEXAMPLE_HPP_
#define _JPNATIVEMODULEEXAMPLE_HPP_
#include "JpNativeModuleExample_EXPORT.h"
#include "Titanium/detail/TiBase.hpp"
#include "Titanium/Module.hpp"
namespace Jp
{
using namespace HAL;
class JPNATIVEMODULEEXAMPLE_EXPORT NativeModuleExample : public JSExportObject, public JSExport<NativeModuleExample>
{
public:
NativeModuleExample(const JSContext&) TITANIUM_NOEXCEPT;
void aaa() TITANIUM_NOEXCEPT;
virtual void postInitialize(JSObject& js_object) override;
virtual void postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) override;
virtual void aaa(); //add new method
virtual ~NativeModuleExample() = default;
NativeModuleExample(const NativeModuleExample&) = default;
NativeModuleExample& operator=(const NativeModuleExample&) = default;
#ifdef TITANIUM_MOVE_CTOR_AND_ASSIGN_DEFAULT_ENABLE
NativeModuleExample(NativeModuleExample&&) = default;
NativeModuleExample& operator=(NativeModuleExample&&) = default;
#endif
static void JSExportInitialize();
};
}
#endif // _JPNATIVEMODULEEXAMPLE_HPP_
sample code
var nativemoduleexample = require('jp.NativeModuleExample');
Ti.API.info("module is => " + nativemoduleexample); //no problem
var aaa = nativemoduleexample.aaa(); //titanium error is displayed
$.index.open();
In order to enable your function, you need to register it using TITANIUM_ADD_FUNCTION at JSExportInitialize(). You might want to check out working examples under TitaniumKit such as Ti.UI.Button and Ti.UI.Window.
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
TITANIUM_ADD_FUNCTION(NativeModuleExample, aaa);
}
And then use TITANIUM_FUNCION to define function like this...
TITANIUM_FUNCTION(NativeModuleExample, aaa)
{
aaa();
return get_context().CreateUndefined()
}

Use LLVM/Clang to find fclose() calls in an Xcode project

I would like to learn how I might programmatically integrate with LLVM/Clang to find all of the fclose() calls in my Xcode project. I realize I can accomplish this via normal text searching but this is just the first step in a more detailed problem.
You can write function pass and find the name of the function as below:
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Hello : public FunctionPass {
static char ID;
Hello() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
errs() << "Hello: ";
errs().write_escaped(F.getName()) << '\n';
return false;
}
};
}
char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
Call this pass from opt using opt -hello input.ll and you will get the names of all functions printed. Change the logic in the above code to find your required function. See the following link for more details on writing passes:
http://llvm.org/docs/WritingAnLLVMPass.html

Replacement of Poco::AutoPtr with boost

I am trying to replace Poco::AutoPtr with some alternative in boost. Here is what I have discovered so far:
What I have: below classess are being used with Poco::AutoPtr. They need to implement reference counted method with implementing duplicate() and release() methods.
I am using above referece_counted.h and Poco::AutoPtr in a complex class hierarchy with multiple inheritance and c++ diamond problems.
A simplified version of classes would look something like this
class A : virtual public ReferenceCounted
{
...
}
class B : public A
{
...
}
class C : public A
{
...
}
class D : public A, B
{
...
}
and the list goes on for few more level deep. I know that this needs to be refactored with a simplified hierarchy but I wanna remove Poco::AutoPtr first with proper replacement in boost:
What I have found so far:
I have found that boost::intrusive_ptr is the closest smart pointer that can be a good replacement of Poco::AutoPtr.
I am however not able to implement the proper solution with this because the intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release methods created specifically for each class with which I need to use the pointer. I tried using templates but still not having proper solution at hand.
Also one more issue is that I need to typecast from base to derived class many times.
is intrusive_ptr is the correct smart pointer for this usage ? and if yes.. can anybody give me suggestion regarding how to use the same ?
I am however not able to implement the proper solution with this
because the intrusive_ptr requires intrusive_ptr_add_ref and
intrusive_ptr_release methods created specifically for each class with
which I need to use the pointer.
No-no. It is should not be hard. As Boost documentation says:
On compilers that support argument-dependent lookup,
intrusive_ptr_add_ref and intrusive_ptr_release should be defined in
the namespace that corresponds to their parameter; otherwise, the
definitions need to go in namespace boost.
Try this: main.cpp (built ok with "g++ main.cpp -o main -lboost_system")
#include <boost/intrusive_ptr.hpp>
class MyObject
{
public:
void duplicate(){
// ...
}
void release(){
// ...
}
};
namespace boost {
template <class T>
void intrusive_ptr_add_ref( T * po ) {
po->duplicate(); // your internal realization
}
template <class T>
void intrusive_ptr_release( T * po ) {
po->release();
}
}
int main(int argc, char **argv)
{
// ...
boost::intrusive_ptr<MyObject> ptr( new MyObject );
boost::intrusive_ptr<MyObject> ptr2 = ptr; // should work
}

Selenium RC User Defined Functions

Trying to do something simple -
I have a set of statements to clear browser cookies:
public void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("10000");
selenium.deleteAllVisibleCookies();
}
Now, if I use this function in a test script (using TestNG), calls to this work perfectly. However, if I moved this function to a separate class and change the declaration to include "static", the "selenium" keyword is not recognized.
In a configuration class (say configClass),
public static void clearCookies () {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}
Now, in my test script, if I call configClass.clearCookies();, I get a runtime error
I tried declaring DefaultSelenium selenium = new DefaultSelenium(null);, in the clearCookies() function, but that too results in a runtime error.
I do have the import com.thoughtworks.selenium.*; import in my configClass.
Any pointers would be appreciated. Thanks.
You can do two things.
Refer to the same selenium object in both the classes i.e. in configClass and the class you are calling configClass.clearCookies().
or else
send selenium object to the clearCookies. So the code would be like this
public static void clearCookies (DefaultSelenium selenium) {
selenium.open("http://www.myurl.com");
selenium.waitForPageToLoad("30000");
selenium.deleteAllVisibleCookies();
}

Resources