瀏覽代碼

Adds generic un-templatted Function class and some formatting fixes

Caleb Fangmeier 7 年之前
父節點
當前提交
33c56161a9
共有 4 個文件被更改,包括 131 次插入29 次删除
  1. 32 0
      filval/argparse.hpp
  2. 6 3
      filval/dataset.hpp
  3. 39 7
      filval/log.hpp
  4. 54 19
      filval/value.hpp

+ 32 - 0
filval/argparse.hpp

@@ -1,3 +1,35 @@
+/**
+ * @file
+ * @author  Caleb Fangmeier <caleb@fangmeier.tech>
+ * @version 0.1
+ *
+ * @section LICENSE
+ *
+ *
+ * MIT License
+ *
+ * Copyright (c) 2017 Caleb Fangmeier
+ *
+ * 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.
+ *
+ * @section DESCRIPTION
+ */
 #ifndef argparse_hpp
 #define argparse_hpp
 #include <algorithm>

+ 6 - 3
filval/dataset.hpp

@@ -3,18 +3,22 @@
 #include <iostream>
 #include "value.hpp"
 #include "container.hpp"
+#include "log.hpp"
 
 namespace filval{
 class DataSet{
     private:
         void summary(){
-            GenValue::summary();
+            INFO(GenValue::summary());
+            INFO(GenFunction::summary());
         }
+
     protected:
         ContainerSet containers;
         virtual bool load_next() = 0;
         virtual int get_events() = 0;
         virtual int get_current_event() = 0;
+
     public:
         void process(){
             int events, current_event;
@@ -26,10 +30,8 @@ class DataSet{
                 std::cout << "\rprocessing event: " << current_event+1 << "/" << events << std::flush;
                 GenValue::reset();
                 for(auto con : containers){
-                    /* std::cout << std::endl << "Filling container: " << con.first; */
                     con.second->fill();
                 }
-                /* std::cout << std::endl; */
             }
             std::cout << " Finished!" << std::endl;
         }
@@ -37,6 +39,7 @@ class DataSet{
         void add_container(GenContainer *container){
             containers[container->get_name()] = container;
         }
+
         GenContainer* get_container(std::string container_name){
             GenContainer* c = containers[container_name];
             if (c == nullptr){

+ 39 - 7
filval/log.hpp

@@ -1,3 +1,35 @@
+/**
+ * @file
+ * @author  Caleb Fangmeier <caleb@fangmeier.tech>
+ * @version 0.1
+ *
+ * @section LICENSE
+ *
+ *
+ * MIT License
+ *
+ * Copyright (c) 2017 Caleb Fangmeier
+ *
+ * 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.
+ *
+ * @section DESCRIPTION
+ */
 #ifndef log_hpp
 #define log_hpp
 
@@ -8,14 +40,14 @@
 
 namespace filval::util{
 enum LogPriority {
-    kLogEmergency = 7,   // system is unusable
-    kLogAlert     = 6,   // action must be taken immediately
-    kLogCritical  = 5,    // critical conditions
-    kLogError     = 4,     // error conditions
-    kLogWarning   = 3, // warning conditions
+    kLogEmergency = 7,  // system is unusable
+    kLogAlert     = 6,  // action must be taken immediately
+    kLogCritical  = 5,  // critical conditions
+    kLogError     = 4,  // error conditions
+    kLogWarning   = 3,  // warning conditions
     kLogNotice    = 2,  // normal, but significant, condition
-    kLogInfo      = 1,    // informational message
-    kLogDebug     = 0    // debug-level message
+    kLogInfo      = 1,  // informational message
+    kLogDebug     = 0   // debug-level message
 };
 
 #define CRITICAL(x,y) std::clog << filval::util::LogPriority::kLogCritical << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush; exit(y)

+ 54 - 19
filval/value.hpp

@@ -58,6 +58,41 @@
  */
 namespace filval{
 
+/**
+ * Parent class to all Function classes. Holds a class-level collection of all
+ * created function objects.
+ */
+class GenFunction {
+    protected:
+        std::string name;
+        std::string impl;
+
+        /**
+         * Static mapping of functions from their name to the object wrapper of
+         * the function.
+         */
+        inline static std::map<std::string, GenFunction*> function_registry;
+
+    public:
+        GenFunction(const std::string& name, const std::string& impl)
+          :name("func::"+name),impl(impl){
+            function_registry[name] = this;
+        }
+
+        std::string& get_name(){
+            return name;
+        }
+
+        static std::string& summary(){
+            std::stringstream ss;
+            for(auto p : function_registry){
+                ss << p.first << std::endl;
+                ss << p.second->impl << std::endl;
+                ss << "*****************************************" << std::endl;
+            }
+        }
+};
+
 /**
  * In order to enable proper provenance tracking, and at the same time keep
  * the ability to embed functions into values, the Function class should be
@@ -71,23 +106,23 @@ namespace filval{
  */
 template<typename> class Function; // undefined
 template <typename R, typename... ArgTypes>
-class Function<R(ArgTypes...)> {
-    std::string name;
-    std::function<R(ArgTypes...)> f;
+class Function<R(ArgTypes...)> : public GenFunction {
+    private:
+        std::function<R(ArgTypes...)> f;
 
     public:
+        Function(const std::string& name, const std::string& impl, std::function<R(ArgTypes...)> f)
+          :GenFunction(name, impl), f(f){ }
         Function(const std::string& name, std::function<R(ArgTypes...)> f)
-          :name("func::"+name),f(f){ }
-
-    std::string& get_name(){
-        return name;
-    }
+          :Function(name, "N/A", f){ }
 
-    R operator()(ArgTypes ...args){
-        return f(args...);
-    }
+        R operator()(ArgTypes ...args){
+            return f(args...);
+        }
 };
 
+#define FUNC(t, n, f) Function<t>(n, #f, f)
+
 /**
  * A type-agnostic value.
  * It is necessary to create a type-agnostic parent class to Value so that
@@ -236,7 +271,7 @@ class ObservedValue : public Value<T>{
  * A DerivedValue is generally defined as some function of other Value objects.
  * For example, a Pair is a function of two other Value objects that makes a
  * pair of them. Note that these other Value objects are free to be either
- * ObservedValues or other DerivedValues. 
+ * ObservedValues or other DerivedValues.
  *
  * It is desireable from a performance standpoint that each DerivedValue be
  * calculated no more than once per observation. Therefore, when a get_value is
@@ -418,7 +453,7 @@ template <typename T>
 class Max : public Reduce<T>{
     public:
         Max(const std::string& v_name)
-          :Reduce<T>(Function<T(std::vector<T>)>("max", [](std::vector<T> vec){ 
+          :Reduce<T>(Function<T(std::vector<T>)>("max", [](std::vector<T> vec){
                          return *std::max_element(vec.begin(), vec.end());}),
                      v_name) { }
 };
@@ -430,7 +465,7 @@ template <typename T>
 class Min : public Reduce<T>{
     public:
         Min(const std::string& v_name)
-          :Reduce<T>(Function<T(std::vector<T>)>("min", [](std::vector<T> vec){ 
+          :Reduce<T>(Function<T(std::vector<T>)>("min", [](std::vector<T> vec){
                          return *std::min_element(vec.begin(), vec.end());}),
                      v_name) { }
 };
@@ -450,7 +485,7 @@ class Mean : public Reduce<T>{
 };
 
 /**
- * Extract the element at a specific index from a vector. 
+ * Extract the element at a specific index from a vector.
  */
 template <typename T>
 class ElementOf : public Reduce<T>{
@@ -491,7 +526,7 @@ template <typename T>
 class MaxIndex : public ReduceIndex<T>{
     public:
         MaxIndex(const std::string& v_name)
-          :ReduceIndex<T>(Function<T(std::vector<T>)>("maxIndex", [](std::vector<T> vec){ 
+          :ReduceIndex<T>(Function<T(std::vector<T>)>("maxIndex", [](std::vector<T> vec){
                               auto elptr = std::max_element(vec.begin(), vec.end());
                               return std::pair<T,int>(*elptr, int(elptr-vec.begin()));}),
                           v_name) { }
@@ -504,7 +539,7 @@ template <typename T>
 class MinIndex : public ReduceIndex<T>{
     public:
         MinIndex(const std::string& v_name)
-          :ReduceIndex<T>(Function<T(std::vector<T>)>("minIndex", [](std::vector<T> vec){ 
+          :ReduceIndex<T>(Function<T(std::vector<T>)>("minIndex", [](std::vector<T> vec){
                               auto elptr = std::min_element(vec.begin(), vec.end());
                               return std::pair<T,int>(*elptr, int(elptr-vec.begin()));}),
                           v_name) { }
@@ -512,7 +547,7 @@ class MinIndex : public ReduceIndex<T>{
 
 
 /**
- * A variadic 
+ * A variadic
  */
 /* template <typename R, typename... T> */
 /* class MultiFunc : public DerivedValue<R>{ */
@@ -559,7 +594,7 @@ class PointerValue : public DerivedValue<T*>{
         void update_value(){ }
     public:
         PointerValue(const std::string& name, T* ptr)
-          :DerivedValue<T*>(name){ 
+          :DerivedValue<T*>(name){
             this->value = ptr;
         }
 };