text:
/******************************************************************************
**
** Copyright (C) 2009-2011 Kyle Lutz <kyle.r.lutz@gmail.com>
** All rights reserved.
**
** This file is a part of the chemkit project. For more information
** see <http://www.chemkit.org>.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in the
**     documentation and/or other materials provided with the distribution.
**   * Neither the name of the chemkit project nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
******************************************************************************/

#include "pluginmanager.h"

#include <map>
#include <cstdlib>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/case_conv.hpp>

#include "plugin.h"
#include "foreach.h"
#include "dynamiclibrary.h"

namespace chemkit {

// === PluginManagerPrivate ================================================ //
class PluginManagerPrivate
{
public:
    std::vector<Plugin *> plugins;
    std::string errorString;
    bool defaultPluginsLoaded;
    std::map<std::string, std::map<std::string, PluginManager::Function> > pluginClasses;
};

// === PluginManager ======================================================= //
/// \class PluginManager pluginmanager.h chemkit/pluginmanager.h
/// \ingroup chemkit
/// \brief The PluginManager class manages the loading and unloading
///        of plugins.
///
/// \see Plugin

// --- Construction and Destruction ---------------------------------------- //
PluginManager::PluginManager()
    : d(new PluginManagerPrivate)
{
    d->defaultPluginsLoaded = false;
}

PluginManager::~PluginManager()
{
    d->pluginClasses.clear();

    foreach(Plugin *plugin, d->plugins){
        DynamicLibrary *library = plugin->library();
        delete plugin;
        delete library;
    }

    delete d;
}

// --- Properties ---------------------------------------------------------- //
/// Returns the plugin with \p name. Returns \c 0 if no plugin with
// \p name is loaded.
Plugin* PluginManager::plugin(const std::string &name) const
{
    foreach(Plugin *plugin, d->plugins){
        if(plugin->name() == name){
            return plugin;
        }
    }

    return 0;
}

/// Returns a list of all the loaded plugins.
const std::vector<Plugin *>& PluginManager::plugins() const
{
    return d->plugins;
}

/// Returns the number of loaded plugins.
size_t PluginManager::pluginCount() const
{
    return d->plugins.size();
}

// --- Plugin Loading ------------------------------------------------------ //
/// Loads a plugin from \p fileName. Returns \c false if an error
/// occurs.
bool PluginManager::loadPlugin(const std::string &fileName)
{
    DynamicLibrary *library = new DynamicLibrary;
    library->setFileName(fileName);
    bool ok = library->open();
    if(!ok){
        std::cerr << "PluginManager: Error: Failed to load plugin: (" << fileName << "):"
                  << library->errorString() << std::endl;

        return false;
    }

    typedef Plugin* (*InitFunction)();
    InitFunction initFunction = reinterpret_cast<InitFunction>(library->resolveFunction("chemkit_plugin_init"));
    if(!initFunction){
        std::cerr << "PluginManager: Error: Failed to load plugin: (" << fileName << "):"
                  << "Plugin contains no init() function." << std::endl;

        return false;
    }

    Plugin *plugin = initFunction();
    if(!plugin){
        std::cerr << "PluginManager: Error: Failed to load plugin: (" << fileName << "):"
                  << "Calling the plugin's init() function failed." << std::endl;

        return false;
    }

    plugin->setLibrary(library);

    d->plugins.push_back(plugin);

    return true;
}

/// Loads all plugins from \p directory.
void PluginManager::loadPlugins(const std::string &directory)
{
    boost::filesystem::path dir(directory);

    if(!boost::filesystem::exists(dir)){
        return;
    }

    for(boost::filesystem::directory_iterator iter(dir); iter != boost::filesystem::directory_iterator(); ++iter){
        std::string fileName = boost::filesystem::path(iter->path().filename()).string();

        if(DynamicLibrary::isLibrary(fileName)){
            loadPlugin(iter->path().string());
        }
    }
}

void PluginManager::loadDefaultPlugins()
{
    if(d->defaultPluginsLoaded){
        return;
    }

    // list of directories to load plugins from
    std::vector<std::string> directories;

    // add default plugin directory
#if defined(CHEMKIT_OS_UNIX)
    directories.push_back(CHEMKIT_INSTALL_PREFIX "/lib/chemkit/plugins/");
#endif

    // add directory from the CHEMKIT_PLUGIN_PATH environment variable
    const char *path = getenv("CHEMKIT_PLUGIN_PATH");
    if(path){
        directories.push_back(path);
    }

    // load plugins from each directory
    foreach(const std::string &directory, directories){
        loadPlugins(directory);
    }

    d->defaultPluginsLoaded = true;
}

/// Unloads the plugin.
bool PluginManager::unloadPlugin(Plugin *plugin)
{
    if(!plugin){
        return false;
    }

    d->plugins.erase(std::remove(d->plugins.begin(), d->plugins.end(), plugin));
    DynamicLibrary *library = plugin->library();
    delete plugin;
    delete library;

    return true;
}

/// Unloads the plugin with \p name.
bool PluginManager::unloadPlugin(const std::string &name)
{
    return unloadPlugin(plugin((name)));
}

// --- Error Handling ------------------------------------------------------ //
void PluginManager::setErrorString(const std::string &errorString)
{
    d->errorString = errorString;
}

/// Returns a string describing the last error that occurred.
std::string PluginManager::errorString() const
{
    return d->errorString;
}

// --- Static Methods ------------------------------------------------------ //
/// Returns the instance of the plugin manager.
PluginManager* PluginManager::instance()
{
    static PluginManager singleton;

    return &singleton;
}

// --- Internal Methods ---------------------------------------------------- //
/// Registers a new plugin function for \p className and
/// \p pluginName.
bool PluginManager::registerPluginClass(const std::string &className, const std::string &pluginName, boost::function<void* ()> function)
{
    std::map<std::string, Function> &classPlugins = d->pluginClasses[className];

    // use lower case plugin name
    std::string lowerCasePluginName = boost::algorithm::to_lower_copy(pluginName);

    // prevent overwriting of previously registered plugins
    if(classPlugins.find(lowerCasePluginName) != classPlugins.end()){
        return false;
    }

    // add plugin class
    classPlugins[lowerCasePluginName] = function;

    return true;
}

/// Unregisters a plugin function for \p className and \p pluginName.
bool PluginManager::unregisterPluginClass(const std::string &className, const std::string &pluginName)
{
    std::map<std::string, Function> &classPlugins = d->pluginClasses[className];

    // use lower case plugin name
    std::string lowerCasePluginName = boost::algorithm::to_lower_copy(pluginName);

    // remove plugin class
    return classPlugins.erase(lowerCasePluginName) > 0;
}

/// Returns a vector of strings containing the names of registered
/// plugins for \p className.
std::vector<std::string> PluginManager::pluginClassNames(const std::string &className) const
{
    // ensure default plugins are loaded
    const_cast<PluginManager *>(this)->loadDefaultPlugins();

    const std::map<std::string, Function> &classPlugins = d->pluginClasses[className];

    std::vector<std::string> names;
    for(std::map<std::string, Function>::const_iterator i = classPlugins.begin(); i != classPlugins.end(); ++i){
        names.push_back(i->first);
    }

    return names;
}

/// Returns the registered function for the given \p className and \p pluginName.
boost::function<void* ()> PluginManager::pluginClassFunction(const std::string &className, const std::string &pluginName) const
{
    // ensure default plugins are loaded
    const_cast<PluginManager *>(this)->loadDefaultPlugins();

    // use lower case plugin name
    std::string lowerCasePluginName = boost::algorithm::to_lower_copy(pluginName);

    const std::map<std::string, Function> &classPlugins = d->pluginClasses[className];

    std::map<std::string, Function>::const_iterator location = classPlugins.find(lowerCasePluginName);
    if(location == classPlugins.end()){
        return 0;
    }

    return location->second;
}

} // end chemkit namespace

max_stars_repo_name: quizzmaster/chemkit

max_issues_repo_name: soplwang/chemkit

max_forks_repo_name: soplwang/chemkit


text:
/*
  [auto_generated]
  boost/numeric/odeint/external/thrust/thrust.hpp

  [begin_description]
  includes all headers required for using odeint with thrust
  [end_description]

  Copyright 2013 Karsten Ahnert
  Copyright 2013 Mario Mulansky

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE_1_0.txt or
  copy at http://www.boost.org/LICENSE_1_0.txt)
*/

#ifndef BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_HPP_DEFINED
#define BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_HPP_DEFINED

#include <boost/numeric/odeint/external/thrust/thrust_algebra.hpp>
#include <boost/numeric/odeint/external/thrust/thrust_operations.hpp>
#include <boost/numeric/odeint/external/thrust/thrust_algebra_dispatcher.hpp>
#include <boost/numeric/odeint/external/thrust/thrust_operations_dispatcher.hpp>
#include <boost/numeric/odeint/external/thrust/thrust_resize.hpp>

#endif // BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_HPP_DEFINED

ext: hpp

max_stars_repo_name: rudylee/expo

max_issues_repo_name: rudylee/expo

max_forks_repo_name: rudylee/expo


text:
#include <ostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main()
{
    io::stream_buffer<io::file_sink> buf("log.txt");
    std::ostream out(&buf);

    out << "Write to log file." << std::endl;
}

ext: cpp

max_stars_repo_name: Toxe/test-cpp-boost

max_issues_repo_name: Toxe/test-cpp-boost

max_forks_repo_name: Toxe/test-cpp-boost


text:
#include <boost/filesystem.hpp>

#include <vtkXMLImageDataWriter.h>
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkPointData.h>

#include "VolumeOutput.h"

VolumeOutput::VolumeOutput(
		const RootSettings &settings,
		const PhysicsCoefficients &coefs) :
	PostprocessorBase::PostprocessorBase(
		settings.postprocessor.volume_output,
		settings.algorithm.general.results_folder_name,
		coefs) {}

void VolumeOutput::apply(BulkOpticalFieldCollection &bulk_optical_fields) {

	std::string filename = format_global_path() + ".vti";
	boost::filesystem::path path(filename);

	std::cout << "Exporting bulk optical field to " << path.filename().string() << std::endl;

	auto writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
	writer->SetFileName(filename.c_str());
	writer->SetDataModeToAppended();
	writer->EncodeAppendedDataOff();
	writer->SetInputData(bulk_optical_fields.get_vti_data());
	writer->Write();
}


text:
//Copyright (c) 2015 Zachary Kann
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

// ---
// Author: Zachary Kann

// Class for histogramming data

#include <fstream>
#include <string>
#include <armadillo>

#ifndef _Z_HISTOGRAM_HPP_
#define _Z_HISTOGRAM_HPP_

class Histogram {
 public:

  inline Histogram (const int number_of_bins, const double bin_width,
               const bool centered = false)
      : number_of_bins_(number_of_bins), bin_width_(bin_width),
        centered_(centered){
    array_ = arma::zeros<arma::rowvec>(number_of_bins_);
    counts_ = arma::zeros<arma::irowvec>(number_of_bins_);
    if (centered)
      min_ = -static_cast<double>(number_of_bins/2)*bin_width_;
    else
      min_ = 0.0;
  }

  // A wrapper for Print that first sets the filename.
  inline void Print(const std::string& filename, const bool normalize = false) {
    filename_ = filename;
    Print(normalize);
  }

  // Adds a a (optionally weighted) value
  inline void Add(const double x_value, const double weight = 1.0,
                  const bool increment = true) {
    int bin_number = FindBinNumber(x_value);
    if (bin_number < 0 || bin_number >= number_of_bins_) return;
    array_(bin_number) += weight;
    if (increment)
      counts_(bin_number)++;
  }

  inline void Multiply(const double rhs) {
    array_ *= rhs;
  }

  // Weights histogram by volume of spherical shell
  inline void WeightByShellVolume() {
    double sphere_factor = 4.0*M_PI/3.0*bin_width_*bin_width_*bin_width_;
    for (int i = 0; i < number_of_bins_; ++i) {
      array_(i) *= 1.0/sphere_factor/(((i+1)*(i+1)*(i+1)-i*i*i));
    }
  }

  inline void PrintFraction(const std::string& filename) {
    filename_ = filename;
    PrintFraction();
  }

 private:
  inline int FindBinNumber (const double x_value) {
    int bin_number;
    if (centered_) {
      bin_number =
          static_cast<int>(floor(x_value/bin_width_)) + number_of_bins_/2;
    } else {
      bin_number = static_cast<int>((x_value-min_)*bin_width_);
    }
    return bin_number;
  }

  // Prints the histogram to the preset file.
  void Print(const bool normalize);

  void PrintFraction();

  int number_of_bins_, total_;
  double bin_width_;
  double min_;
  arma::rowvec array_;
  arma::irowvec counts_;
  std::string filename_;
  std::ofstream file_;
  bool centered_;

};

#endif