Forráskód Böngészése

refactor: flatten together Scope/ContextScope into a single Property

Sam Jaffe 2 éve
szülő
commit
614eaccccd

+ 3 - 3
include/reflection/context.h

@@ -13,7 +13,7 @@
 
 #include "reflection/forward.h"
 #include "reflection/object.h"
-#include "reflection/scope.h"
+#include "reflection/property.h"
 
 namespace reflection {
 class Context {
@@ -30,9 +30,9 @@ public:
   Object const & get(std::string_view id) const { return data_.at(id); }
   bool has(std::string_view id) const { return data_.count(id); }
 
-  Object get(ContextScope const &scope) const {
+  Object get(Property const &scope) const {
     if (scope.begin() == scope.end()) { return get(scope.context()); }
-    return get(scope.context()).get(static_cast<Scope const &>(scope));
+    return get(scope.context()).get(scope);
   }
 
 private:

+ 1 - 2
include/reflection/forward.h

@@ -17,9 +17,8 @@
 
 namespace reflection {
 class Context;
-class ContextScope;
 class Object;
-class Scope;
+class Property;
 template <typename Obj, typename = void> class Reflection;
 template <typename T> class Proxy;
 template <typename T> class TypeCast;

+ 5 - 3
include/reflection/object.h

@@ -13,7 +13,7 @@
 #include <utility>
 
 #include "reflection/forward.h"
-#include "reflection/scope.h"
+#include "reflection/property.h"
 
 namespace reflection {
 class Object {
@@ -52,8 +52,10 @@ public:
   Object get(std::string_view id) const & { return (this->*get_)(id); }
   Object get(std::string_view id) && { return (this->*get_)(id).own(); }
     
-  Object get(Scope const & scope) const {
-    if (type_name() != scope.type_name()) { throw std::bad_cast(); }
+  Object get(Property const & scope) const {
+    if (type_name() != scope.type_name() && !scope.type_name().empty()) {
+      throw std::bad_cast();
+    }
     return get(*this, scope.begin(), scope.end());
   }
 

+ 20 - 21
include/reflection/scope.h

@@ -1,5 +1,5 @@
 //
-//  scope.h
+//  property.h
 //  reflection
 //
 //  Created by Sam Jaffe on 2/20/23.
@@ -12,39 +12,38 @@
 #include <string_view>
 #include <vector>
 
+#include "reflection/forward.h"
+
 namespace reflection {
-class Scope {
+class Property {
 private:
+  std::string context_;
   std::string type_name_;
   std::vector<std::string> path_;
   
 public:
+  explicit Property(std::string const &context = "") : context_(context) {}
+  
   template <typename C, typename = std::enable_if_t<IS_SCOPE_CONTAINER(C)>>
-  Scope(std::string const & type_name, C const & path)
-      : type_name_(type_name), path_(path.begin(), path.end()) {}
+  Property(std::string const &context, std::string const & type_name, C const & path)
+      : context_(context), type_name_(type_name),
+        path_(path.begin(), path.end()) {}
+  
+  template <typename C, typename = std::enable_if_t<IS_SCOPE_CONTAINER(C)>>
+  Property(std::string const & type_name, C const & path)
+      : Property("", type_name, path) {}
+
+  std::string_view context() const { return context_; }
   
   std::string_view type_name() const { return type_name_; }
+  
   auto begin() const { return path_.begin(); }
   auto end() const { return path_.end(); }
   
 protected:
-  Scope() = default;
-};
-
-class ContextScope : public Scope {
-private:
-  std::string context_;
-  
-public:
-//  ContextScope() = default;
-  explicit ContextScope(std::string const & type_name) : context_(type_name) {}
-
-  template <typename C, typename = std::enable_if_t<IS_SCOPE_CONTAINER(C)>>
-  ContextScope(std::string const & context, std::string const & type_name,
-               C const & path)
-      : context_(context), Scope(type_name, path) {}
-
-  std::string_view context() const { return context_; }
+  friend bool operator<(Property const &lhs, Property const &rhs) {
+    return lhs.path_ < rhs.path_;
+  }
 };
 
 }

+ 4 - 4
reflection.xcodeproj/project.pbxproj

@@ -10,7 +10,7 @@
 		CD2FF9072310BE8500ABA548 /* reflection_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD5535251EEC689700108F81 /* reflection_test.cxx */; };
 		CD2FF9092310BE9200ABA548 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD2FF8F32310BE6D00ABA548 /* GoogleMock.framework */; };
 		CD86DE33289FED0C00647685 /* context_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD86DE32289FED0C00647685 /* context_test.cxx */; };
-		CDA8565129A3EFA6007493E8 /* scope.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA8565029A3EFA6007493E8 /* scope.h */; };
+		CDA8565129A3EFA6007493E8 /* property.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA8565029A3EFA6007493E8 /* property.h */; };
 		CDA9561B28726949006ACEC2 /* reflection in Headers */ = {isa = PBXBuildFile; fileRef = CDA95611287266E5006ACEC2 /* reflection */; settings = {ATTRIBUTES = (Public, ); }; };
 		CDA9561E28731972006ACEC2 /* object_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA9561D28731972006ACEC2 /* object_test.cxx */; };
 		CDA956D9287493FE006ACEC2 /* typecast_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA956D8287493FE006ACEC2 /* typecast_test.cxx */; };
@@ -54,7 +54,7 @@
 		CD5535251EEC689700108F81 /* reflection_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = reflection_test.cxx; sourceTree = "<group>"; };
 		CD86DE2D289F55A100647685 /* context.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = context.h; sourceTree = "<group>"; };
 		CD86DE32289FED0C00647685 /* context_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = context_test.cxx; sourceTree = "<group>"; };
-		CDA8565029A3EFA6007493E8 /* scope.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = scope.h; sourceTree = "<group>"; };
+		CDA8565029A3EFA6007493E8 /* property.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = property.h; sourceTree = "<group>"; };
 		CDA9560A28726665006ACEC2 /* reflection.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = reflection.h; sourceTree = "<group>"; };
 		CDA9560F287266C9006ACEC2 /* forward.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = forward.h; sourceTree = "<group>"; };
 		CDA95610287266CE006ACEC2 /* object.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = object.h; sourceTree = "<group>"; };
@@ -162,7 +162,7 @@
 				CD86DE2D289F55A100647685 /* context.h */,
 				CDA9561228726723006ACEC2 /* proxy.h */,
 				CDA95610287266CE006ACEC2 /* object.h */,
-				CDA8565029A3EFA6007493E8 /* scope.h */,
+				CDA8565029A3EFA6007493E8 /* property.h */,
 				CDA956D32873E1C7006ACEC2 /* typecast.h */,
 			);
 			path = reflection;
@@ -176,7 +176,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				CDA9561B28726949006ACEC2 /* reflection in Headers */,
-				CDA8565129A3EFA6007493E8 /* scope.h in Headers */,
+				CDA8565129A3EFA6007493E8 /* property.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};