Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some basic examples needed #63

Open
tamboril opened this issue Jun 28, 2016 · 4 comments
Open

Some basic examples needed #63

tamboril opened this issue Jun 28, 2016 · 4 comments

Comments

@tamboril
Copy link

Thanks for the work on this library. I really need a few examples of the API in use. The unit tests aren't much help, and the Doxygen doesn't show any usage examples.

For instance, what's unwrapped mean? And am I supposed to just downcast config_value to the proper type in a big switch ?

@Magisus
Copy link
Contributor

Magisus commented Jul 6, 2016

The unwrapped method returns the value stored in a config_value as a C++ type, in this case a boost::variant (for info on this type see http://www.boost.org/doc/libs/1_61_0/doc/html/variant.html). This variant can hold any of the following types: boost::blank (used to represent a null or empty value), string, int64_t, double, int, bool, std::vector, std::unordered_map. The vector and unordered_map contain the same type of boost::variant and represent a HOCON list and HOCON object respectively.

What you do with these values is up to you. The best approach is probably to define a static_visitor as described in the boost::variant docs, instead of a switch statement. For an example of such a visitor, see config_value_factory.cc, whose job it is to take these boost::variants and return corresponding config_values (the opposite of the unwrapped methods).

If you need to manipulate config_values without unwrapping them, and need the concrete types, unfortunately you will have to resort to casting. The library code itself does that all over the place. We have tried to keep our structure and public API as consistent as possible with the original Java version of this project, and they have some additional tests and examples you might want to take a look at: https://github.com/typesafehub/config. Bear in mind our port is still a work in progress, so not all features specified there have been completely implemented in the C++ yet.

We definitely plan to get some proper examples written up soon. We are just at the point where we are going to start using this project internally, and as part of that process we will be cleaning up the documentation and public API, as well as fleshing out some examples.

@MikaelSmith
Copy link
Contributor

MikaelSmith commented Jul 6, 2016

I basic tutorial on using boost::variant are present at http://www.boost.org/doc/libs/1_61_0/doc/html/variant/tutorial.html#variant.tutorial.basic. You can use boost::get<Type>(v) if you expect a value matching Type; that request will throw an exception if the type stored in v is not Type.

@tamboril
Copy link
Author

Thank you both for the explanations (I just saw this). I'm successfully using the library as-is, and I look forward to the merge of the pending pull requests.

@tamboril
Copy link
Author

tamboril commented Sep 1, 2016

In the way of documentation, here is what I have to do to build cpp-hocon on CentOS 7 with a local Boost 1.61 without touching any system directories.:

  • Build Leatherman and install locally:

    cmake \
      -DBOOST_ROOT=~/local/boost \
      -DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG -w" \
      -DLEATHERMAN_GETTEXT=OFF \
      -DCMAKE_INSTALL_PREFIX=~/local <leatherman_src_dir>
    
  • Build cpp-hocon and install locally:

    cmake \
      -DBOOST_ROOT=~/local/boost \
      -DCMAKE_PREFIX_PATH=~/local \
      -DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG -w" \
      -DCMAKE_INSTALL_PREFIX=~/local <cpp-hocon_src_dir>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants