⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.h

📁 C++编写的机器学习算法 Lemga is a C++ package which consists of classes for several learning models and gener
💻 H
字号:
// -*- C++ -*-#ifndef __COMMON_TYPES_OBJECT_H__#define __COMMON_TYPES_OBJECT_H__/** @file *  @brief Object class, _register_creator, and other common types. * *  $Id: object.h 2537 2006-01-08 08:40:36Z ling $ */#ifdef size_ttypedef size_t UINT;#elsetypedef unsigned long UINT;#endiftypedef double REAL;#define INFINITESIMAL   (8e-16) ///< almost equal to 0, 1+x/2-1>0#define EPSILON         (1e-9)  ///< accumulated numerical error#undef  INFINITY/// Should avoid using INFINITY since it is a lazy trick.#define INFINITY        (5e30)#ifdef __cplusplus#include <stdio.h>#include <iostream>#include <string>#include <vector>/** @brief Stop the program and warn when an undefined function is called. * *  Some member functions may be @em optional for child classes. There *  are two cases for an optional function: *   -# It is declared as an interface; Some child classes may implement *      it but others may not. This macro should be called in the interface *      class. See NNLayer::train for an example. *   -# It is defined and implemented but may be inappropriate for some *      child classes. To prevent misuse of this function, use this macro *      to redefine it in those child classes. * *  This macro will stop the program with a ``function undefined" error. * *  @note This approach differs from defining functions as pure virtual, *  since using the latter way, a child class cannot be instantiated without *  implementing all pure virtual functions, while some of them may not *  be desired. * *  @todo A better macro name; Auto-extraction of function name (could be *  done by __PRETTY_FUNCTION__ but is not compatible between compilers) */#define OBJ_FUNC_UNDEFINED(fun)    \    std::cerr<<__FILE__":"<<__LINE__<<": "<<id()<<"::" fun " undefined.\n"; \    exit(-99);/** @brief The root (ancestor) of all classes. * *  Object class collects some very common and useful features. It has *  a static string for identification and several funtional interfaces. */class Object {    friend std::istream& operator>> (std::istream&, Object&);    friend std::ostream& operator<< (std::ostream&, const Object&);public:    virtual ~Object ()  {}    typedef std::string id_t;    /** @return Class ID string (class name) */    virtual const id_t& id () const = 0;    /** @brief Create a new object using the default constructor.     *     *  The code for a derived class Derived is always     *  @code return new Derived(); @endcode     */    virtual Object* create () const = 0;    /// Create a new object from an input stream.    static Object* create (std::istream&);    /** @brief Create a new object by replicating itself.     *  @return A pointer to the new copy.     *     *  The code for a derived class Derived is always     *  @code return new Derived(*this); @endcode     *  Though seemingly redundant, it helps to copy an object without     *  knowing the real type of the object.     *  @sa C++ FAQ Lite 20.6     */    virtual Object* clone () const = 0;protected:    static const id_t empty_id;    typedef UINT ver_t;    typedef std::vector<ver_t> ver_list;    /// Serialize the object to an output stream.    virtual bool serialize (std::ostream&, ver_list&) const;    /// Unserialize from an input stream.    virtual bool unserialize (std::istream&, ver_list&,                              const id_t& = empty_id) { return true; }};inline std::ostream& operator<< (std::ostream& os, const Object& obj) {    Object::ver_list vl_empty;    obj.serialize(os, vl_empty);    return os;}typedef Object* (*_creator_t) ();/** Unserialization from an input stream will create an object of a now *  unknown class. To facilitate this process, each instantiable derived *  class (that is, a class with all pure virtual functions implemented) *  must have a creator and add it to this map. The way is to declare a *  static _register_creator object in the .cpp file. *  @code static const _register_creator _("Derived", _creator); @endcode *  @sa C++ FAQ Lite 35.8 */struct _register_creator {    _register_creator(const Object::id_t&, _creator_t);};#define REGISTER_CREATOR(cls) \    static const Object::id_t _id_ = #cls; \    static Object* _c_ () { return new cls; } \    const Object::id_t& cls::id () const { return ::_id_; } \    static const _register_creator _(_id_, _c_)/** @brief Serialize parents (ancestors) part. *  @param pcls Parent class. *  @param os Output stream. *  @param def_ver Current (default) version. */#define SERIALIZE_PARENT(pcls,os,vl,def_ver) \    vl.push_back(def_ver); pcls::serialize(os, vl)/** @brief Unserialize parents and set current format number @a ver. *  @param pcls Parent class (input). *  @param is Input stream (input). *  @param ver Version number for this class (output). */#define UNSERIALIZE_PARENT(pcls,is,vl,def_ver,ver) \    if (!pcls::unserialize(is, vl)) return false; \    const ver_t ver = vl.back(); \    if (ver > def_ver) { \        std::cerr << this->id() \                  << ": unserialize: format newer than the program\n"; \        return false; \    } \    if (ver > 0) vl.pop_back()#endif // __cplusplus#ifdef  __OBJECT_H__#warning "This header file may conflict with another `object.h' file."#endif#define __OBJECT_H__#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -