EzDevInfo.com

pod

Git push deploy for Node.js

Is there really no better way to document perl code than POD?

I'm a Perl programmer for a long time, but I always have problems with documentation in POD.

When I use POD comments in the code, the code is difficult to read. When I use POD comments at the end of file, there is the danger that the documentation is not in sync with the code.

I miss a documentation style similar to Java.

/**
 * @description
 * ...
 */

I look for an easier and more intuitive documentation style. Is there such a thing?


Source: (StackOverflow)

C++: POD Pros\Cons

  • What are the pros and cons of using Plain Old Data (POD) structs\classes in C++?
  • In what cases should one prefer using them over non-PODs?
  • Specifically, do PODs have advantages while working with serialization frameworks? Perhaps when working cross-platform and cross-language?

Source: (StackOverflow)

Advertisements

Perl documentation (POD) browsers?

I'm looking for is a good on-screen POD reading experience.

For years, I've used perldoc or man running in an xterm to read Perl documentation on screen, and a small custom program built around Pod::LaTeX to print it. The printed version is good: the script does a nice job. However, on-screen reading is painful:

  • You can search, but good luck finding e.g., the as section in DBIx::Class::ResultSet. Less doesn't know anything about the structure of the document, and searches for common English words are pointless.
  • I fear every time the document tells me see section so-and-so. Great. Now I have to find that. And then getting back to where I was won't be trivial (though that may be my less ignorance showing)
  • See document so-and-so is worse. !perldoc foo works, but switching between the two documents is then hard. You can mostly get around this by suspending less and running perldoc from the shell, but that's more keystrokes and I'm lazy.
  • Formatting leaves a lot to be desired.

I want to be able to click a table of contents, and go to that section. I want to be able to click that "see other" and go there. And so on. So far, I know of two possibilities:

I'm running Debian GNU/Linux, both Lenny and Squeeze. But please suggest programs for all platforms to make this as useful as possible.


Source: (StackOverflow)

Which Perl module do I use to convert Pod to HTML?

I need to convert Pod to HTML. There are number of Pod::HTML and Pod::Simple::* modules. Which one is the one I should use?


Source: (StackOverflow)

How to expose STL list over DLL boundary?

I have a DLL which needs to access data stored in STL containers in the host application. Because C++ has no standard ABI, and I want to support different compilers, the interface between the application and DLL basically has to remain plain-old-data.

For vectors this is relatively straightforward. You can simply return the memory block of the vector, because it is guaranteed to be contigious:

// To return vector<int> data
virtual void GetVectorData(const int*& ptr, size_t& count) const
{
    if (!vec.empty())
        ptr = &(vec.front());

    count = vec.size();
}

Now the DLL can have safe read-only access to the vector's data via that interface. The DLL can also wrap this to copy the contents in to a vector for itself as well.

What about STL lists (and deques) though? Is there another straightforward way to allow access via a DLL boundary? Or will I have to resort to some kind of GetFirst()/GetNext() interface? I might need to do this for a lot of lists, so it'd be nice to have a solution as simple as vector's.


Source: (StackOverflow)

How to compare objects of POD types

This example :

#include <iostream>
#include <cstring>

struct A
{
    int  a;
    bool b;
};

bool foo( const A a1, const A a2 )
{
    return ( 0 == std::memcmp( &a1, &a2, sizeof( A ) ) );
}

int main()
{
    A a1 = A();
    a1.a = 5;a1.b = true;
    A a2 = A();
    a2.a = 5;a2.b = true;

    std::cout<<std::boolalpha << foo( a1, a2 ) << std::endl;
}

is going to produce false, because of padding.

I do not have access to the foo function, and I can not change the way the comparison is done.

Assuming a bool occupies 1 byte (that is true on my system), if I change the struct A to this :

struct A
{
  int a;
  bool b;
  char dummy[3];
};

then it works fine on my system (the output is true).

Is there anything else I could do to fix the above problem (get the true output)?


Source: (StackOverflow)

C++ POD struct inheritance? Are there any guarantees about the memory layout of derived members

Let's say, I have a struct RGB and I want to create struct RGBA, which inherits RGB:

struct RGB {
    unsigned char r;
    unsigned char g;
    unsigned char b;
};

struct RGBA: RGB {
    unsigned char a;
};

Both will be used for reading uncompressed image data:

RGBA *pixel=static_cast<RGBA *>(image->uncompressed_data);

Question: Is this safe, regarding the memory layout of struct RGBA? Does anyone guarantee, that:

  • unsigned char a comes after the RGB struct (not before)
  • There is no padding between struct RGB and the a parameter from struct RGBA?

will #pragma pack help here? It's all about memory layout during inheritance.


Source: (StackOverflow)

How are objects stored in memory in C++?

How are objects stored in memory in C++?

For a regular class such as

class Object
    {
public:
    int i1;
    int i2;
    char i3;
    int i4;
private:
    };

Using a pointer of Object as an array can be used to access i1 as follows?

((Object*)&myObject)[0] === i1?

Other questions on SO seem to suggest that casting a struct to a pointer will point to the first member for POD-types. How is this different for classes with constructors if at all? Also in what way is it different for non-POD types?

Edit:

In memory therefore would the above class be laid out like the following?

[i1 - 4bytes][i2 - 4bytes][i3 - 1byte][padding - 3bytes][i4 - 4bytes]

Source: (StackOverflow)

Is it safe to delete a POD object by a pointer to its base?

Actually I am thinking about trivially destructible objects, not only about POD (I am not sure POD can have base class).

When I read this explanation for is_trivially_destructible from cppreference I notice this:

Storage occupied by trivially destructible objects may be reused without calling the destructor.

So, it is safe to do that:

struct A {
  int a;
};
struct B : A {
  int b;
};
int main() {
  A* a = new B;
  delete a;
}

B::~B() won't be called - and AFAIK (please correct if I am wrong) the entire memory will be freed. And B::~B() for sure is trivial.

I know this code smells badly, but my question is only about safeness of this code...


Source: (StackOverflow)

Is this struct POD in C++11?

Is this struct a POD in C++11?

struct B
{
  int a;
  B(int aa) : a(aa) {}
  B() = default;
};

Note that this question is explicit about C++11. I know that this class is not a POD in C++98 nor C++03.

For an explanation of POD in C++11, see trivial vs. standard layout vs. POD

(Inspired by this question: Is there a compile-time func/macro to determine if a C++0x struct is POD? )


Source: (StackOverflow)

Can I memcpy() any type which has a trivial destructor?

I do realize is_pod is a sufficient condition for a type to be memcpy-able, but is has_trivial_destructor also sufficient for this purpose? If not, why?


Source: (StackOverflow)

Do I need to make a type a POD to persist it with a memory-mapped file?

Pointers cannot be persisted directly to file, because they point to absolute addresses. To address this issue I wrote a relative_ptr template that holds an offset instead of an absolute address.

Based on the fact that only trivially copyable types can be safely copied bit-by-bit, I made the assumption that this type needed to be trivially copyable to be safely persisted in a memory-mapped file and retrieved later on.

This restriction turned out to be a bit problematic, because the compiler generated copy constructor does not behave in a meaningful way. I found nothing that forbid me from defaulting the copy constructor and making it private, so I made it private to avoid accidental copies that would lead to undefined behaviour.

Later on, I found boost::interprocess::offset_ptr whose creation was driven by the same needs. However, it turns out that offset_ptr is not trivially copyable because it implements its own custom copy constructor.

Is my assumption that the smart pointer needs to be trivially copyable to be persisted safely wrong?

If there's no such restriction, I wonder if I can safely do the following as well. If not, exactly what are the requirements a type must fulfill to be usable in the scenario I described above?

struct base {
    int x;
    virtual void f() = 0;
    virtual ~base() {} // virtual members!
};

struct derived : virtual base {
    int x;
    void f() { std::cout << x; }
};

using namespace boost::interprocess;

void persist() {
    file_mapping file("blah");
    mapped_region region(file, read_write, 128, sizeof(derived));
    // create object on a memory-mapped file
    derived* d = new (region.get_address()) derived();
    d.x = 42;
    d->f();
    region.flush();
}

void retrieve() {
    file_mapping file("blah");
    mapped_region region(file, read_write, 128, sizeof(derived));
    derived* d = region.get_address();
    d->f();
}

int main() {
    persist();
    retrieve();
}

Thanks to all those that provided alternatives. It's unlikely that I will be using something else any time soon, because as I explained, I already have a working solution. And as you can see from the use of question marks above, I'm really interested in knowing why Boost can get away without a trivially copyable type, and how far can you go with it: it's quite obvious that classes with virtual members will not work, but where do you draw the line?


Source: (StackOverflow)

Standard Layout c++

I was going through great articles on C++ POD, Trivial and Standard Layout classes One property I haven't clearly understood about standard layout is the following:-

 A standard layout has no base classes of the same type as the first 
    non-static data member

So the following will not be a Standard Layout as it has the first member same as the base class

struct NonStandardLayout3 : StandardLayout1 {
    StandardLayout1 x; // first member cannot be of the same type as base
};

But performance-wise and property-wise how is the above struct any different than

struct StandardLayout5 : StandardLayout1 {
    int x;
    StandardLayout1 y; // can have members of base type if they're not the first   
};

which is the correction of the one above this.


Source: (StackOverflow)

Can't C++ POD type have any constructor?

I have a class and a const variable.

struct A 
{
    int b;
};

A const a;

The class A is POD and can be initialized like this.

A const a = { 3 };

IMHO, it looks fine to have a constructor like this.

struct A 
{
    int b;

    A(int newB) : b(newB)
    {
    }
};

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?


I modified question to present my original meaning. I had wrote the struct as class by mistake, and sorry for @Johannes about confusing :)


Source: (StackOverflow)

Default initialization in C++

I was asking myself something this morning, and I can't find the words to properly "google" for it:

Lets say I have:

struct Foo
{
  int bar;
};

struct Foo2
{
   int bar;
   Foo2() {}
};

struct Foo3
{
   int bar;
   Foo3() : bar(0) {}
};

Now if I default instantiate Foo, Foo2 and Foo3:

Foo foo;
Foo2 foo2;
Foo3 foo3;

In which case(s) is the bar member properly initialized ?

(Well Foo3 obviously explicitely initialize it and is only showed here to explicit the difference with Foo2 so the question is mainly about the first two.)

Thank you ! :)


Source: (StackOverflow)