Problems with reading into buffer using boost::asio::async_read - boost

Good day.
I have a Types.hpp file in my project. And within it i have:
....
namespace RC
{
.....
.....
struct ViewSettings
{
....
};
.....
}
In the Server.cpp file I'm including this Types.hpp file, and i have there:
class Session
{
.....
RC::ViewSettings tmp;
boost::asio::async_read(socket_, boost::asio::buffer(&tmp, sizeof(RC::ViewSettings)),
boost::bind(&Session::Finish_Reading_Data, shared_from_this(), boost::asio::placeholders::error));
.....
}
And during the compilation i have an errors:
error C2825: 'F': must be a class or namespace when followed by '::'
: see reference to class template instantiation 'boost::_bi::result_traits<R,F>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void)
]
: see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void),
L=boost::_bi::list2<boost::_bi::value<boost::shared_ptr<Session>>,boost::arg<1>>
]
error C2039: 'result_type' : is not a member of '`global namespace''
And the code like this works in proper way:
int w;
boost::asio::async_read(socket_, boost::asio::buffer(&w, sizeof(int)),
boost::bind(&Session::Handle_Read_Width, shared_from_this(), boost::asio::placeholders::error));
Please, help. What's the problem here?
Thanks in advance.

You are trying to transfer a struct, not just a simple type. That's why it works with int, and won't work with the struct.
I think this is in the area of serialization, so i think you will need boost.serialization.
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/examples.html
Check this serialization example, maybe that will help you out.

You need to inherit Session from boost::enable_shared_from_this<Session>.

Related

how to use pointer of one namespace in another namespace nad both the namespace are used in another class

Below is my sample code where we are facing the issue
file1.h
namespace name1
{
extern name1::test* _test;
}
file2.h
namespace name2
{
class toolbox
{
int fun()
{
return _test->test();
}
}
}
newfile.cpp
using namespace name1;
using namespace name2;
int newclass::new()
{
return _test->test();
}
With above I will get error :
undefined reference to name2::_test
If I add extern in name2 I get error :
_test ambiguous
The problem you have are conflicting symbols between the namespaces that you are using.
This is a typical example used in styleguides which forbid using namespace.
Instead of including the whole namespace, you could prefix the identifiers with NS::, for example: name1::_test.
Alternatively, it is possible to selectively use elements from the namespace by using: using NS::Identifier. However, I never found myself in a situation where I needed on a global, so don't know if this works.
A piece of advice: don't use global variables, it is much more maintainable to pass the pointers or references as arguments for your functions.

Using Concurrency::concurrent_queue together with std::unique_ptr

I want to use the Concurrency library of Visual Studio 2010 to pass actions between threads.
I have my class SimpleAction and pointers to it are stored in the Concurrency::concurrent_queue.
Using this definition, and 'consumption' logic it works:
typedef Concurrency::concurrent_queue<SimpleAction *> ActionQueue;
while (true)
{
SimpleAction *action = nullptr;
while (m_queue.try_pop(action))
{
action->process();
delete action;
}
Sleep(100);
}
However, when I change this to an std::unique_ptr, like this:
typedef Concurrency::concurrent_queue<std::unique_ptr<SimpleAction>> ActionQueue;
while (true)
{
std::unique_ptr<SimpleAction> action;
while (m_queue.try_pop(action))
{
action->process();
}
Sleep(100);
}
The compiler gives the following error message:
F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(366) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
with
[
_Ty=`anonymous-namespace'::SimpleAction
]
F:\DevStudio\Vs2010\VC\INCLUDE\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
with
[
_Ty=`anonymous-namespace'::SimpleAction
]
F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(365) : while compiling class template member function 'void Concurrency::concurrent_queue<_Ty>::_Copy_item(Concurrency::details::_Concurrent_queue_base_v4::_Page &,size_t,const void *)'
with
[
_Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction>
]
test.cpp(138) : see reference to class template instantiation 'Concurrency::concurrent_queue<_Ty>' being compiled
with
[
_Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction>
]
It seems the compiler does not like this construction in concurrent_queue:
/*override*/ virtual void _Copy_item( _Page& _Dst, size_t _Index, const void* _Src )
{
new( &_Get_ref(_Dst,_Index) ) _Ty(*static_cast<const _Ty*>(_Src));
}
Which seems logical (we don't want an std::unique_ptr to be copied (it must be moved instead).
Questions:
Is this a known problem/limitation/feature of the Concurrency/PPL library of Visual Studio 2010?
Is this problem solved in Visual Studio 2012?
Or am I doing something wrong?
thanks,
Patrick

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
}

Serial Ports in a .DLL file

I have created .dll file in Visual C++.
It includes a function which takes two arguments and send data through serial port. Next i wanted to include this .dll in my application and call these functions. But i am unable to call these functions. Kindly Help.
Here is header file for my .dll
namespace positioncontrol
{
using namespace std;
using namespace System;
using namespace System::IO::Ports;
public ref class control
{
static int rotate(char a, String^ b);
};
}
And here is .cpp for my .dll
#include "goniometer.h"
namespace positioncontrol
{
void control::rotate(char a, String^ b)
{ SerialPort^ serialPort = gcnew SerialPort(L"COM5",9600,Parity::None,1,StopBits::One);
int inp_rotation;
array<unsigned char>^ inp_c = gcnew array<unsigned char>(2);
String^ inp_string;
inp_c[0] = a;
inp_string= b;
inp_rotation=Int32::Parse(inp_string);
inp_c[1] = (unsigned char)inp_rotation;
serialPort->Write(inp_c,0,2);
}
}
I am using this .dll in a desktop application. I have include the header file
#ifndef goniometer_h
#define goniometer_h
#include "goniometer.h"
#endif
Added the path for include directories and added .dll as a reference.
Now i am using the function defined in .dll for a click event
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
char dir;
dir = 0x42;
String^ inp_string;
inp_string=enter_degree->Text;
positioncontrol::control::rotate(dir,inp_string);
}
Now when i build my desktop application i am getting following error
1>C:\Users\DELL\Desktop\Final\Motor_Dual_API\Debug\goniometer.h(10): error C2011: 'positioncontrol::control' : 'class' type redefinition
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C3861: 'rotate': identifier not found
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C3861: 'rotate': identifier not found
Kindly help me in figuring out error.
Thanks and Regards
In C++, the namespace separator is :: rather than .. Check your using statement.
Your code does not define serialPort. Add the definition to the global namespace, to the namespace positionControl or as an auto variable in your function
From the semantic point of view, serialPort is a pointer. So must also create an instance of the object, where serialPort points to.

Error C2248 in Fstream.h

This is the error message I'm getting:
error C2248:
'std::basic_ios<_Elem,_Traits>::basic_ios'
: cannot access private member
declared in class
'std::basic_ios<_Elem,_Traits>' C:\Program
Files\Microsoft Visual Studio
10.0\VC\include\fstream
this is the line it says the error is
template<class _Elem,
class _Traits> inline
void swap(basic_ofstream<_Elem, _Traits>& _Left,
basic_ofstream<_Elem, _Traits>& _Right)
{ // swap _Left and _Right basic_ofstreams
_Left.swap(_Right);
}
this is the code where I access fstream
char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(saveFileNameString).ToPointer();
ofstream sessionFile;
sessionFile.open(stringPointer, std::ios_base::in);
Marshal::FreeHGlobal(IntPtr(stringPointer));`
Thank you in advance!!!!
The problem is not on Fstream.h but on your code.
Check if your not passing a private member variable of a class to ofstream or some other method.
I just run into that problem, and thanks to Luron's comment I figured it out.
I post the explanation in case someone would have the same problem:
There is no copy constructor for stream objects (ostream, istream and derivates), and passing one of these objects to a function will call the copy constructor and so will cause the error.

Resources