0

I want to make a Wrapper class for ActiveMQ which will have only send and get functions. I want to wrap them in a static class. Users can use this client and send, get messages to the activemq instance.

I want this process to be transparent. There are two classes in this link

My only handicap is, i need to this in c++ and not sure where to start. I havent used c++ for ages and now not sure how I can create this wrapper class.

I m giving it a try as follows:

// .h file
#include <stdlib.h>
#include <iostream>

using namespace std;

class ActiveMQWrapper
{
        public:
                static void send(std::string message);
                static std::string get();

};

// .cpp file
#include<string>
#include<iostream>

#include "ActiveMQWrapper.h"

void ActiveMQWrapper::send(std::string message){
        std::cout<<message;
}

std::string ActiveMQWrapper::get(){
        return "test";
}

// test file
#include <string>
#include <iostream>
#include "ActiveMQWrapper.h"


int main() {
        std::string foo ="test";
        ActiveMQWrapper::send(foo);
        std::cout<<ActiveMQWrapper::get();
        return 1;
}

When I added the following to .h file, hell breaks loose. Do you think I should seperate this impl to a factory and initialize and instance and return to the wrapper above? How do i deal with all the dependencies?

private:

    Connection* connection;
    Session* session;
    Destination* destination;
    MessageProducer* producer;
    int numMessages;
    bool useTopic;
    bool sessionTransacted;
    std::string brokerURI;

and the header files, i get several messages as errors, which complains about the path.

How can i get this correct? I eventually want to build a Factory, get an instance and send or get the messages to the queue.

is there a code sample i can look into to get this right? essential i want to use the functionality of only this producer and consumer.

Edit: I understand there is no such thing as static class in C++ . This is my reference.

Community
  • 1
  • 1
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • 2
    There is no such thing as a "static class" in C++. – Kerrek SB Nov 15 '11 at 00:08
  • this is my reference. http://stackoverflow.com/questions/9321/how-do-you-create-a-static-class-in-c – DarthVader Nov 15 '11 at 00:11
  • 1
    Hm, that's just an SO question by someone equally confused; I'm not sure that constitutes a "reference" :-) (Especially given that another answer to it says that there is no "static class" in C++!) – Kerrek SB Nov 15 '11 at 00:23
  • @KerrekSB you are not being constructive. can you just leave it alone? if you wanna be helpful, you can comment on the question, please!!. i dont need another argument of such can be a reference or not etc. please!!! – DarthVader Nov 15 '11 at 00:25
  • Can you post the error messages? That would help understand what's going on. And where do `Connection`, `Session`, `Destination` and `MessageProducer` come from? Are you sure you `#include` their respective headers? – R. Martinho Fernandes Nov 15 '11 at 00:52
  • that s what i want to do. i d like to make all those transparent to the client. if you look at the question. I want to build a wrapper class around those which client would say `ActiveMQWrapper::send("message")` and `ActiveMQWrapper::get()`. – DarthVader Nov 15 '11 at 01:10
  • 1
    Right, but if you want to give people a chance of helping you, you really need to replace "hell breaks loose" and "several messages as errors" by the *actual error messages*. – R. Martinho Fernandes Nov 15 '11 at 01:14

2 Answers2

2

First, there is no such thing as a "static class" in C++. So the first requirement is meaningless mumbo jumbo. A namespace is probably what you're looking for: you can put set() and get() in a wrapper namespace.

That said, your problems with "all hell breaks loose" may conceivably be fixed by placing …

#pragma once

at the top of your header file.

Note that this is a just a de facto standard supported by all compilers you would ever want to use. No doubt others will try to explain how you can do the same more complicated and error prone within the standard language, using so called header guards. But try it.

Also, never place using namespace std; in the global namespace in a header file. using namespace simply pulls in the entire namespace, even the parts you don't use. When people #include your header, they won't expect to get that much bloat.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

First, let's consider what static class means in languages that support it. It is says that the class has no object-oriented value, and that it exists solely for organization.

Because C++ doesn't have to shoehorn everything into the OO paradigm, it provides proper constructs to express this: namespaces. A namespace behaves just like a static class in other languages. It contains a number of functions, variables or even classes. What I would do, then, is put your get() and send() functions in a namespace.

namespace activeMQWrapper
{
    void send(std::string message)
    {
        // Put your code here.
    }

    std::string get()
    {
        // And here.
    }
}

I don't know anything about ActiveMQ, so I'm afraid I won't be able to help with implementation issues.

As for the path errors, it's hard to tell without actually seeing them. I'd guess that your compiler options are set incorrectly, or, as Alf P. Steinbach suggests, your headers are being included more than once.

Community
  • 1
  • 1
Maxpm
  • 24,113
  • 33
  • 111
  • 170